Home / File/ json_test.go — gin Source File

json_test.go — gin Source File

Architecture documentation for json_test.go, a go file in the gin codebase. 1 imports, 0 dependents.

File go RequestBinding FormBinding 1 imports 4 functions 3 classes

Entity Profile

Dependency Diagram

graph LR
  5fc3e4c8_9ce1_a1b5_76da_8025131b9864["json_test.go"]
  0d816439_76ee_c7fb_5329_a059811a793a["io"]
  5fc3e4c8_9ce1_a1b5_76da_8025131b9864 --> 0d816439_76ee_c7fb_5329_a059811a793a
  style 5fc3e4c8_9ce1_a1b5_76da_8025131b9864 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

// Copyright 2019 Gin Core Team. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package binding

import (
	"io"
	"net/http/httptest"
	"testing"
	"time"
	"unsafe"

	"github.com/gin-gonic/gin/codec/json"
	"github.com/gin-gonic/gin/render"
	jsoniter "github.com/json-iterator/go"
	"github.com/modern-go/reflect2"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestJSONBindingBindBody(t *testing.T) {
	var s struct {
		Foo string `json:"foo"`
	}
	err := jsonBinding{}.BindBody([]byte(`{"foo": "FOO"}`), &s)
	require.NoError(t, err)
	assert.Equal(t, "FOO", s.Foo)
}

func TestJSONBindingBindBodyMap(t *testing.T) {
	s := make(map[string]string)
	err := jsonBinding{}.BindBody([]byte(`{"foo": "FOO","hello":"world"}`), &s)
	require.NoError(t, err)
	assert.Len(t, s, 2)
	assert.Equal(t, "FOO", s["foo"])
	assert.Equal(t, "world", s["hello"])
}

func TestCustomJsonCodec(t *testing.T) {
	// Restore json encoding configuration after testing
	oldMarshal := json.API
	defer func() {
		json.API = oldMarshal
	}()
	// Custom json api
	json.API = customJsonApi{}

	// test decode json
	obj := customReq{}
	err := jsonBinding{}.BindBody([]byte(`{"time_empty":null,"time_struct": "2001-12-05 10:01:02.345","time_nil":null,"time_pointer":"2002-12-05 10:01:02.345"}`), &obj)
	require.NoError(t, err)
	assert.Equal(t, zeroTime, obj.TimeEmpty)
	assert.Equal(t, time.Date(2001, 12, 5, 10, 1, 2, 345000000, time.Local), obj.TimeStruct)
	assert.Nil(t, obj.TimeNil)
	assert.Equal(t, time.Date(2002, 12, 5, 10, 1, 2, 345000000, time.Local), *obj.TimePointer)
	// test encode json
	w := httptest.NewRecorder()
	err2 := (render.PureJSON{Data: obj}).Render(w)
	require.NoError(t, err2)
// ... (157 more lines)

Subdomains

Dependencies

  • io

Frequently Asked Questions

What does json_test.go do?
json_test.go is a source file in the gin codebase, written in go. It belongs to the RequestBinding domain, FormBinding subdomain.
What functions are defined in json_test.go?
json_test.go defines 4 function(s): TestCustomJsonCodec, TestJSONBindingBindBody, TestJSONBindingBindBodyMap, init.
What does json_test.go depend on?
json_test.go imports 1 module(s): io.
Where is json_test.go in the architecture?
json_test.go is located at binding/json_test.go (domain: RequestBinding, subdomain: FormBinding, directory: binding).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free