| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
stdjson "encoding/json" |
| 7 |
"errors" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/goccy/go-json" |
| 11 |
) |
| 12 |
|
| 13 |
// The methods of the marshalers are called directly by their code, with the data word of the interface value |
| 14 |
// as the receiver. Every kind of a receiver must be encoded as encoding/json does. |
| 15 |
|
| 16 |
type valueMarshaler struct{ N int } |
| 17 |
|
| 18 |
func (v valueMarshaler) MarshalJSON() ([]byte, error) { return []byte(`{"value":1}`), nil } |
| 19 |
|
| 20 |
type ptrMarshaler struct{ N int } |
| 21 |
|
| 22 |
func (v *ptrMarshaler) MarshalJSON() ([]byte, error) { return []byte(`{"ptr":1}`), nil } |
| 23 |
|
| 24 |
// a value of the size of a pointer whose method is on the pointer: it is copied before the call. |
| 25 |
type wordPtrMarshaler int64 |
| 26 |
|
| 27 |
func (v *wordPtrMarshaler) MarshalJSON() ([]byte, error) { return []byte(`"word"`), nil } |
| 28 |
|
| 29 |
type mapMarshaler map[string]int |
| 30 |
|
| 31 |
func (m mapMarshaler) MarshalJSON() ([]byte, error) { |
| 32 |
if m == nil { |
| 33 |
return []byte(`"nil map"`), nil |
| 34 |
} |
| 35 |
return []byte(`"map"`), nil |
| 36 |
} |
| 37 |
|
| 38 |
// a struct of a single pointer is stored directly in an interface value: the method on the value takes the |
| 39 |
// pointer word itself. |
| 40 |
type directMarshaler struct{ P *int } |
| 41 |
|
| 42 |
func (d directMarshaler) MarshalJSON() ([]byte, error) { |
| 43 |
if d.P == nil { |
| 44 |
return []byte(`"direct nil"`), nil |
| 45 |
} |
| 46 |
return []byte(`"direct"`), nil |
| 47 |
} |
| 48 |
|
| 49 |
// the method is promoted from an embedded interface. |
| 50 |
type embeddedMarshaler struct { |
| 51 |
stdjson.Marshaler |
| 52 |
N int |
| 53 |
} |
| 54 |
|
| 55 |
type textMarshaler struct{ S string } |
| 56 |
|
| 57 |
func (t textMarshaler) MarshalText() ([]byte, error) { return []byte("text:" + t.S), nil } |
| 58 |
|
| 59 |
type ptrTextMarshaler struct{ S string } |
| 60 |
|
| 61 |
func (t *ptrTextMarshaler) MarshalText() ([]byte, error) { return []byte("ptrtext:" + t.S), nil } |
| 62 |
|
| 63 |
type failingMarshaler struct{} |
| 64 |
|
| 65 |
func (failingMarshaler) MarshalJSON() ([]byte, error) { return nil, errors.New("fails") } |
| 66 |
|
| 67 |
type contextMarshaler struct{} |
| 68 |
|
| 69 |
func (contextMarshaler) MarshalJSON(ctx context.Context) ([]byte, error) { |
| 70 |
if ctx == nil { |
| 71 |
return nil, errors.New("no context") |
| 72 |
} |
| 73 |
return []byte(`"context"`), nil |
| 74 |
} |
| 75 |
|
| 76 |
func TestMarshalerCalledDirectly(t *testing.T) { |
| 77 |
var nilPtr *ptrMarshaler |
| 78 |
var nilMap mapMarshaler |
| 79 |
for _, test := range []struct { |
| 80 |
name string |
| 81 |
v any |
| 82 |
}{ |
| 83 |
{"value receiver", valueMarshaler{1}}, |
| 84 |
{"pointer to value receiver", &valueMarshaler{1}}, |
| 85 |
{"pointer receiver", &ptrMarshaler{1}}, |
| 86 |
{"pointer receiver on a value", ptrMarshaler{1}}, |
| 87 |
{"nil pointer", nilPtr}, |
| 88 |
{"word with pointer receiver", wordPtrMarshaler(1)}, |
| 89 |
{"pointer to word", func() *wordPtrMarshaler { w := wordPtrMarshaler(1); return &w }()}, |
| 90 |
{"map with value receiver", mapMarshaler{"a": 1}}, |
| 91 |
{"direct struct with value receiver", directMarshaler{P: new(int)}}, |
| 92 |
{"direct struct with a nil pointer", directMarshaler{}}, |
| 93 |
{"pointer to direct struct", &directMarshaler{P: new(int)}}, |
| 94 |
{"promoted from an embedded interface", embeddedMarshaler{Marshaler: valueMarshaler{1}}}, |
| 95 |
{"pointer to promoted", &embeddedMarshaler{Marshaler: &ptrMarshaler{1}}}, |
| 96 |
{"nil map with value receiver", nilMap}, |
| 97 |
{"text", textMarshaler{"a"}}, |
| 98 |
{"pointer to text", &textMarshaler{"a"}}, |
| 99 |
{"pointer receiver text", &ptrTextMarshaler{"a"}}, |
| 100 |
{"pointer receiver text on a value", ptrTextMarshaler{"a"}}, |
| 101 |
{"fields", struct { |
| 102 |
V valueMarshaler |
| 103 |
P ptrMarshaler |
| 104 |
PP *ptrMarshaler |
| 105 |
NP *ptrMarshaler |
| 106 |
W wordPtrMarshaler |
| 107 |
M mapMarshaler |
| 108 |
NM mapMarshaler |
| 109 |
T textMarshaler |
| 110 |
PT ptrTextMarshaler |
| 111 |
}{PP: &ptrMarshaler{1}, M: mapMarshaler{}}}, |
| 112 |
{"omitempty", struct { |
| 113 |
NP *ptrMarshaler `json:",omitempty"` |
| 114 |
PP *ptrMarshaler `json:",omitempty"` |
| 115 |
V valueMarshaler `json:",omitempty"` |
| 116 |
}{PP: &ptrMarshaler{1}}}, |
| 117 |
{"slice", []valueMarshaler{{1}, {2}}}, |
| 118 |
{"slice of pointer receiver", []ptrMarshaler{{1}, {2}}}, |
| 119 |
{"map value", map[string]ptrMarshaler{"a": {1}}}, |
| 120 |
{"interface", []any{valueMarshaler{1}, &ptrMarshaler{1}, nilPtr, textMarshaler{"x"}}}, |
| 121 |
} { |
| 122 |
t.Run(test.name, func(t *testing.T) { |
| 123 |
expected, err := stdjson.Marshal(test.v) |
| 124 |
if err != nil { |
| 125 |
t.Fatal(err) |
| 126 |
} |
| 127 |
got, err := json.Marshal(test.v) |
| 128 |
if err != nil { |
| 129 |
t.Fatal(err) |
| 130 |
} |
| 131 |
if !bytes.Equal(expected, got) { |
| 132 |
t.Fatalf("expected %s but got %s", expected, got) |
| 133 |
} |
| 134 |
expected, err = stdjson.MarshalIndent(test.v, "", " ") |
| 135 |
if err != nil { |
| 136 |
t.Fatal(err) |
| 137 |
} |
| 138 |
got, err = json.MarshalIndent(test.v, "", " ") |
| 139 |
if err != nil { |
| 140 |
t.Fatal(err) |
| 141 |
} |
| 142 |
if !bytes.Equal(expected, got) { |
| 143 |
t.Fatalf("indent: expected %s but got %s", expected, got) |
| 144 |
} |
| 145 |
}) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestMarshalerCalledDirectlyError(t *testing.T) { |
| 150 |
_, err := json.Marshal(struct{ F failingMarshaler }{}) |
| 151 |
var merr *json.MarshalerError |
| 152 |
if !errors.As(err, &merr) { |
| 153 |
t.Fatalf("expected a MarshalerError but got %v", err) |
| 154 |
} |
| 155 |
if merr.Type.Name() != "failingMarshaler" { |
| 156 |
t.Fatalf("unexpected type in the error: %v", merr.Type) |
| 157 |
} |
| 158 |
b, err := json.MarshalContext(context.Background(), []contextMarshaler{{}}) |
| 159 |
if err != nil || string(b) != `["context"]` { |
| 160 |
t.Fatalf("context marshaler: %s, %v", b, err) |
| 161 |
} |
| 162 |
b, err = json.Marshal([]contextMarshaler{{}}) |
| 163 |
if err != nil || string(b) != `["context"]` { |
| 164 |
t.Fatalf("context marshaler without a context: %s, %v", b, err) |
| 165 |
} |
| 166 |
} |