Home / Function/ TestCustomJsonCodec() — gin Function Reference

TestCustomJsonCodec() — gin Function Reference

Architecture documentation for the TestCustomJsonCodec() function in json_test.go from the gin codebase.

Entity Profile

Dependency Diagram

graph TD
  5131ebe9_f66d_ea85_1bf6_6479f7ab926d["TestCustomJsonCodec()"]
  5fc3e4c8_9ce1_a1b5_76da_8025131b9864["json_test.go"]
  5131ebe9_f66d_ea85_1bf6_6479f7ab926d -->|defined in| 5fc3e4c8_9ce1_a1b5_76da_8025131b9864
  style 5131ebe9_f66d_ea85_1bf6_6479f7ab926d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

binding/json_test.go lines 40–63

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)
	assert.JSONEq(t, "{\"time_empty\":null,\"time_struct\":\"2001-12-05 10:01:02.345\",\"time_nil\":null,\"time_pointer\":\"2002-12-05 10:01:02.345\"}\n", w.Body.String())
	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
}

Subdomains

Frequently Asked Questions

What does TestCustomJsonCodec() do?
TestCustomJsonCodec() is a function in the gin codebase, defined in binding/json_test.go.
Where is TestCustomJsonCodec() defined?
TestCustomJsonCodec() is defined in binding/json_test.go at line 40.

Analyze Your Own Codebase

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

Try Supermodel Free