| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
stdjson "encoding/json" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/goccy/go-json" |
| 9 |
) |
| 10 |
|
| 11 |
// The fields of an embedded struct are written as the fields of the struct which embeds it. |
| 12 |
|
| 13 |
type embeddedTagTarget struct { |
| 14 |
Bar string `json:"bar"` |
| 15 |
} |
| 16 |
|
| 17 |
// omitempty of an embedded struct has nothing to omit. |
| 18 |
type embeddedPtrWithOmitEmpty struct { |
| 19 |
Foo string `json:"foo"` |
| 20 |
*embeddedTagTarget `json:",omitempty"` |
| 21 |
} |
| 22 |
|
| 23 |
type embeddedValueWithOmitEmpty struct { |
| 24 |
Foo string `json:"foo"` |
| 25 |
embeddedTagTarget `json:",omitempty"` |
| 26 |
} |
| 27 |
|
| 28 |
// The name of an embedded struct is not a key, so it doesn't hide a field of the same name. |
| 29 |
type EmbeddedSameName struct { |
| 30 |
EmbeddedSameName string `json:"EmbeddedSameName"` |
| 31 |
Other int `json:"Other"` |
| 32 |
} |
| 33 |
|
| 34 |
type embeddedSameNameHolder struct { |
| 35 |
EmbeddedSameName |
| 36 |
Name string `json:"Name"` |
| 37 |
} |
| 38 |
|
| 39 |
// All the fields of a struct embedded in itself are hidden by the same fields at the shallower depth. |
| 40 |
type embeddedInItself struct { |
| 41 |
ID int `json:"id"` |
| 42 |
*embeddedInItself |
| 43 |
} |
| 44 |
|
| 45 |
type embeddedInItselfThroughA struct { |
| 46 |
A int `json:"a"` |
| 47 |
*embeddedInItselfThroughB |
| 48 |
} |
| 49 |
|
| 50 |
type embeddedInItselfThroughB struct { |
| 51 |
B int `json:"b"` |
| 52 |
*embeddedInItselfThroughA |
| 53 |
} |
| 54 |
|
| 55 |
// the field of the shallower depth hides the one of an embedded struct, which is not recursive. |
| 56 |
type embeddedShadowedInner struct { |
| 57 |
ID int `json:"id"` |
| 58 |
X int `json:"x"` |
| 59 |
} |
| 60 |
|
| 61 |
type embeddedShadowedOuter struct { |
| 62 |
ID int `json:"id"` |
| 63 |
*embeddedShadowedInner |
| 64 |
} |
| 65 |
|
| 66 |
func TestEncodeEmbeddedStruct(t *testing.T) { |
| 67 |
for _, test := range []struct { |
| 68 |
name string |
| 69 |
v any |
| 70 |
}{ |
| 71 |
{"pointer with omitempty", embeddedPtrWithOmitEmpty{Foo: "f", embeddedTagTarget: &embeddedTagTarget{Bar: "b"}}}, |
| 72 |
{"nil pointer with omitempty", embeddedPtrWithOmitEmpty{Foo: "f"}}, |
| 73 |
{"value with omitempty", embeddedValueWithOmitEmpty{Foo: "f", embeddedTagTarget: embeddedTagTarget{Bar: "b"}}}, |
| 74 |
{"zero value with omitempty", &embeddedValueWithOmitEmpty{}}, |
| 75 |
{"field of the same name as the embedded struct", embeddedSameNameHolder{ |
| 76 |
EmbeddedSameName: EmbeddedSameName{EmbeddedSameName: "inside", Other: 1}, Name: "outside", |
| 77 |
}}, |
| 78 |
{"embedded in itself", embeddedInItself{ID: 99, embeddedInItself: &embeddedInItself{ID: 1}}}, |
| 79 |
{"nil embedded in itself", &embeddedInItself{ID: 99}}, |
| 80 |
{"embedded in itself through another struct", embeddedInItselfThroughA{ |
| 81 |
A: 1, embeddedInItselfThroughB: &embeddedInItselfThroughB{B: 2, embeddedInItselfThroughA: &embeddedInItselfThroughA{A: 3}}, |
| 82 |
}}, |
| 83 |
{"hidden by the shallower field", embeddedShadowedOuter{ID: 99, embeddedShadowedInner: &embeddedShadowedInner{ID: 1, X: 2}}}, |
| 84 |
} { |
| 85 |
t.Run(test.name, func(t *testing.T) { |
| 86 |
expected, err := stdjson.Marshal(test.v) |
| 87 |
if err != nil { |
| 88 |
t.Fatal(err) |
| 89 |
} |
| 90 |
for i := 0; i < 2; i++ { |
| 91 |
got, err := json.Marshal(test.v) |
| 92 |
if err != nil { |
| 93 |
t.Fatalf("call %d: %v", i, err) |
| 94 |
} |
| 95 |
if string(got) != string(expected) { |
| 96 |
t.Fatalf("call %d: expected %s but got %s", i, expected, got) |
| 97 |
} |
| 98 |
} |
| 99 |
expectedIndent, err := stdjson.MarshalIndent(test.v, "", " ") |
| 100 |
if err != nil { |
| 101 |
t.Fatal(err) |
| 102 |
} |
| 103 |
gotIndent, err := json.MarshalIndent(test.v, "", " ") |
| 104 |
if err != nil { |
| 105 |
t.Fatal(err) |
| 106 |
} |
| 107 |
if string(gotIndent) != string(expectedIndent) { |
| 108 |
t.Fatalf("indent: expected %s but got %s", expectedIndent, gotIndent) |
| 109 |
} |
| 110 |
}) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// An array of no elements is always empty. |
| 115 |
func TestEncodeOmitEmptyZeroLengthArray(t *testing.T) { |
| 116 |
v := struct { |
| 117 |
A [0]int `json:"a,omitempty"` |
| 118 |
B [0]string `json:"b,omitempty"` |
| 119 |
C [0]int `json:"c"` |
| 120 |
D [1]int `json:"d,omitempty"` |
| 121 |
E [0]f0Array `json:"e,omitempty"` |
| 122 |
}{} |
| 123 |
expected, err := stdjson.Marshal(v) |
| 124 |
if err != nil { |
| 125 |
t.Fatal(err) |
| 126 |
} |
| 127 |
got, err := json.Marshal(v) |
| 128 |
if err != nil { |
| 129 |
t.Fatal(err) |
| 130 |
} |
| 131 |
if string(got) != string(expected) { |
| 132 |
t.Fatalf("expected %s but got %s", expected, got) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
type f0Array [0]byte |
| 137 |
|
| 138 |
func (f0Array) MarshalJSON() ([]byte, error) { return []byte(`"empty"`), nil } |
| 139 |
|
| 140 |
// A number which a marshaler writes is valid JSON even if it is out of the range of float64. |
| 141 |
type numberLiteralMarshaler struct{ literal string } |
| 142 |
|
| 143 |
func (n numberLiteralMarshaler) MarshalJSON() ([]byte, error) { return []byte(n.literal), nil } |
| 144 |
|
| 145 |
func TestEncodeNumberOutOfFloat64Range(t *testing.T) { |
| 146 |
for _, literal := range []string{"1e400", "-1e400", "1e-400", "123456789012345678901234567890123456789012345678901234567890", "1.5"} { |
| 147 |
for _, v := range []any{ |
| 148 |
numberLiteralMarshaler{literal}, |
| 149 |
stdjson.RawMessage(literal), |
| 150 |
map[string]any{"n": numberLiteralMarshaler{literal}}, |
| 151 |
} { |
| 152 |
expected, err := stdjson.Marshal(v) |
| 153 |
if err != nil { |
| 154 |
t.Fatal(err) |
| 155 |
} |
| 156 |
got, err := json.Marshal(v) |
| 157 |
if err != nil { |
| 158 |
t.Fatalf("%s: %v", literal, err) |
| 159 |
} |
| 160 |
if string(got) != string(expected) { |
| 161 |
t.Fatalf("%s: expected %s but got %s", literal, expected, got) |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
// what is not a number is still an error. |
| 166 |
if _, err := json.Marshal(numberLiteralMarshaler{"1e"}); err == nil { |
| 167 |
t.Fatal("an invalid number must be an error") |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
type contextValueMarshaler struct{} |
| 172 |
|
| 173 |
type contextValueKey struct{} |
| 174 |
|
| 175 |
func (contextValueMarshaler) MarshalJSON(ctx context.Context) ([]byte, error) { |
| 176 |
if ctx == nil { |
| 177 |
return []byte(`"nil context"`), nil |
| 178 |
} |
| 179 |
if v, ok := ctx.Value(contextValueKey{}).(string); ok { |
| 180 |
return []byte(`"` + v + `"`), nil |
| 181 |
} |
| 182 |
return []byte(`"no value"`), nil |
| 183 |
} |
| 184 |
|
| 185 |
// The context of a call must not be seen by the following calls, which reuse the same runtime context. |
| 186 |
func TestEncodeContextIsNotKeptForNextCall(t *testing.T) { |
| 187 |
ctx := context.WithValue(context.Background(), contextValueKey{}, "value") |
| 188 |
for i := 0; i < 3; i++ { |
| 189 |
got, err := json.MarshalContext(ctx, contextValueMarshaler{}) |
| 190 |
if err != nil { |
| 191 |
t.Fatal(err) |
| 192 |
} |
| 193 |
if string(got) != `"value"` { |
| 194 |
t.Fatalf("with context: got %s", got) |
| 195 |
} |
| 196 |
for _, marshal := range []func(any) ([]byte, error){ |
| 197 |
json.Marshal, |
| 198 |
func(v any) ([]byte, error) { return json.MarshalIndent(v, "", " ") }, |
| 199 |
} { |
| 200 |
got, err := marshal(contextValueMarshaler{}) |
| 201 |
if err != nil { |
| 202 |
t.Fatal(err) |
| 203 |
} |
| 204 |
if string(got) != `"no value"` { |
| 205 |
t.Fatalf("without context: got %s", got) |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
} |