| 1 |
package json_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
stdjson "encoding/json" |
| 6 |
"reflect" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/goccy/go-json" |
| 10 |
) |
| 11 |
|
| 12 |
// OptimizeFieldOrder lets the encoder put the fields of the same kind together and a recursive field last: the |
| 13 |
// keys must be the same as without the option, only in another order; the values must decode back to what was |
| 14 |
// encoded; and the opcodes of a type with and without the option must not be mixed up. |
| 15 |
|
| 16 |
type orderedFields struct { |
| 17 |
Next *orderedFields `json:"next,omitempty"` |
| 18 |
A int `json:"a"` |
| 19 |
S1 string `json:"s1"` |
| 20 |
B int `json:"b"` |
| 21 |
O int `json:"o,omitempty"` |
| 22 |
F float64 `json:"f"` |
| 23 |
S2 string `json:"s2"` |
| 24 |
T bool `json:"t"` |
| 25 |
C int `json:"c"` |
| 26 |
Sub struct { |
| 27 |
X int `json:"x"` |
| 28 |
Y int `json:"y"` |
| 29 |
} `json:"sub"` |
| 30 |
F2 float64 `json:"f2"` |
| 31 |
P *int `json:"p"` |
| 32 |
U uint8 `json:"u"` |
| 33 |
} |
| 34 |
|
| 35 |
func newOrderedFields(n int) *orderedFields { |
| 36 |
var head *orderedFields |
| 37 |
for i := n; i > 0; i-- { |
| 38 |
head = &orderedFields{Next: head, A: i, S1: "s1", B: 2, O: i % 2, F: 1.5, S2: "s2", T: true, C: 3, F2: 2.5, U: 4} |
| 39 |
head.Sub.X = 1 |
| 40 |
} |
| 41 |
return head |
| 42 |
} |
| 43 |
|
| 44 |
func keysOf(t *testing.T, data []byte) []string { |
| 45 |
t.Helper() |
| 46 |
dec := stdjson.NewDecoder(bytes.NewReader(data)) |
| 47 |
if tok, err := dec.Token(); err != nil || tok != stdjson.Delim('{') { |
| 48 |
t.Fatalf("not an object: %s", data) |
| 49 |
} |
| 50 |
var keys []string |
| 51 |
for dec.More() { |
| 52 |
tok, err := dec.Token() |
| 53 |
if err != nil { |
| 54 |
t.Fatal(err) |
| 55 |
} |
| 56 |
keys = append(keys, tok.(string)) |
| 57 |
var v stdjson.RawMessage |
| 58 |
if err := dec.Decode(&v); err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
} |
| 62 |
return keys |
| 63 |
} |
| 64 |
|
| 65 |
func TestEncodeOptimizeFieldOrder(t *testing.T) { |
| 66 |
v := newOrderedFields(3) |
| 67 |
plain, err := json.Marshal(v) |
| 68 |
if err != nil { |
| 69 |
t.Fatal(err) |
| 70 |
} |
| 71 |
expected, _ := stdjson.Marshal(v) |
| 72 |
if string(plain) != string(expected) { |
| 73 |
t.Fatalf("without the option:\n got %s\nwant %s", plain, expected) |
| 74 |
} |
| 75 |
optimized, err := json.MarshalWithOption(v, json.OptimizeFieldOrder()) |
| 76 |
if err != nil { |
| 77 |
t.Fatal(err) |
| 78 |
} |
| 79 |
// the ints together where the first int is, the strings where the first string is, the floats too, and |
| 80 |
// the recursive field last: the others in their order. |
| 81 |
wantKeys := []string{"a", "b", "c", "s1", "s2", "o", "f", "f2", "t", "sub", "p", "u", "next"} |
| 82 |
if keys := keysOf(t, optimized); !reflect.DeepEqual(keys, wantKeys) { |
| 83 |
t.Fatalf("keys with the option: %v, want %v\n%s", keys, wantKeys, optimized) |
| 84 |
} |
| 85 |
var decoded orderedFields |
| 86 |
if err := stdjson.Unmarshal(optimized, &decoded); err != nil { |
| 87 |
t.Fatal(err) |
| 88 |
} |
| 89 |
if !reflect.DeepEqual(&decoded, v) { |
| 90 |
t.Fatalf("the values with the option decode to %+v, want %+v", decoded, *v) |
| 91 |
} |
| 92 |
// the option doesn't change the opcodes without it, and the other way round: the two are cached apart. |
| 93 |
for i := 0; i < 3; i++ { |
| 94 |
got, _ := json.Marshal(v) |
| 95 |
if string(got) != string(plain) { |
| 96 |
t.Fatalf("without the option after it:\n got %s\nwant %s", got, plain) |
| 97 |
} |
| 98 |
got, _ = json.MarshalWithOption(v, json.OptimizeFieldOrder()) |
| 99 |
if string(got) != string(optimized) { |
| 100 |
t.Fatalf("with the option after without:\n got %s\nwant %s", got, optimized) |
| 101 |
} |
| 102 |
} |
| 103 |
// with an indent, and in an interface value, a slice and a map. |
| 104 |
indented, err := json.MarshalIndentWithOption(v, "", " ", json.OptimizeFieldOrder()) |
| 105 |
if err != nil { |
| 106 |
t.Fatal(err) |
| 107 |
} |
| 108 |
if keys := keysOf(t, indented); !reflect.DeepEqual(keys, wantKeys) { |
| 109 |
t.Fatalf("keys with the option and an indent: %v", keys) |
| 110 |
} |
| 111 |
nested, err := json.MarshalWithOption(map[string]any{"k": []any{v}}, json.OptimizeFieldOrder()) |
| 112 |
if err != nil { |
| 113 |
t.Fatal(err) |
| 114 |
} |
| 115 |
var decodedNested map[string][]*orderedFields |
| 116 |
if err := stdjson.Unmarshal(nested, &decodedNested); err != nil { |
| 117 |
t.Fatal(err) |
| 118 |
} |
| 119 |
if !reflect.DeepEqual(decodedNested["k"][0], v) { |
| 120 |
t.Fatalf("the values in an interface value with the option decode to %+v", decodedNested["k"][0]) |
| 121 |
} |
| 122 |
// a long list: encoded in one frame with the recursive field last. |
| 123 |
long := newOrderedFields(1200) |
| 124 |
optimized, err = json.MarshalWithOption(long, json.OptimizeFieldOrder()) |
| 125 |
if err != nil { |
| 126 |
t.Fatal(err) |
| 127 |
} |
| 128 |
var decodedLong orderedFields |
| 129 |
if err := stdjson.Unmarshal(optimized, &decodedLong); err != nil { |
| 130 |
t.Fatal(err) |
| 131 |
} |
| 132 |
if !reflect.DeepEqual(&decodedLong, long) { |
| 133 |
t.Fatal("the long list with the option doesn't decode to what was encoded") |
| 134 |
} |
| 135 |
// two fields of the struct's own type: neither is moved. |
| 136 |
type twoRecursive struct { |
| 137 |
Left *twoRecursive `json:"left"` |
| 138 |
A int `json:"a"` |
| 139 |
Right *twoRecursive `json:"right"` |
| 140 |
} |
| 141 |
two, err := json.MarshalWithOption(&twoRecursive{A: 1, Right: &twoRecursive{A: 2}}, json.OptimizeFieldOrder()) |
| 142 |
if err != nil { |
| 143 |
t.Fatal(err) |
| 144 |
} |
| 145 |
if keys := keysOf(t, two); !reflect.DeepEqual(keys, []string{"left", "a", "right"}) { |
| 146 |
t.Fatalf("keys with two recursive fields: %v", keys) |
| 147 |
} |
| 148 |
} |