go icon indicating copy to clipboard operation
go copied to clipboard

Marshal should compact the output of custom MarshalJSON funcs

Open sbueringer opened this issue 4 years ago • 0 comments

I've tried to use json-iterator/go instead of encoding/json and found the following incompatibility.

When using a custom MarshalJSON func which returns non-compacted JSON encoding/json compacts the output and json-iterator/go does not.

Example:

	pd := partialDoc{
		fields: map[string][]byte{
			"a": []byte(`"aValue"`),
			"b": []byte(`"bValue"`),
		},
	}

	out, _ := json.Marshal(&pd)
	// encoding/json.marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) is running
	// compact after calling the custom MarshalJSON func
	jsonIterOut, _ := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(&pd)

	g.Expect(out).To(Equal(jsonIterOut))

Output:

        Expected (encoding/json)
            <[]uint8 | len:27, cap:32>: {"a":"aValue","b":"bValue"}
        to equal (json-iterator/go)
            <[]uint8 | len:30, cap:30>: {"a": "aValue", "b": "bValue"}

(full code to reproduce in: https://github.com/sbueringer/json-iterator-tests/blob/master/conformance_marshal_compact_test.go#L12)

sbueringer avatar Nov 09 '21 08:11 sbueringer