-
Notifications
You must be signed in to change notification settings - Fork 346
/
json_encode.go
418 lines (378 loc) · 10.2 KB
/
json_encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package amino
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
"github.com/gnolang/gno/tm2/pkg/errors"
)
// ----------------------------------------
// cdc.encodeReflectJSON
// This is the main entrypoint for encoding all types in json form. This
// function calls encodeReflectJSON*, and generally those functions should
// only call this one, for the disfix wrapper is only written here.
// NOTE: Unlike encodeReflectBinary, rv may be a pointer. This is because
// unlike the binary representation, in JSON there is a concrete representation
// of no value -- null. So, a nil pointer here encodes as null, whereas
// encodeReflectBinary() assumes that the pointer is already dereferenced.
// CONTRACT: rv is valid.
func (cdc *Codec) encodeReflectJSON(w io.Writer, info *TypeInfo, rv reflect.Value, fopts FieldOptions) (err error) {
if !rv.IsValid() {
panic("should not happen")
}
if printLog {
fmt.Printf("(E) encodeReflectJSON(info: %v, rv: %#v (%v), fopts: %v)\n",
info, rv.Interface(), rv.Type(), fopts)
defer func() {
fmt.Printf("(E) -> err: %v\n", err)
}()
}
// Dereference value if pointer.
if rv.Kind() == reflect.Ptr {
if rv.IsNil() {
err = writeStr(w, `null`)
return
}
rv = rv.Elem()
}
// Handle the most special case, "well known".
if info.IsJSONWellKnownType {
var ok bool
ok, err = encodeReflectJSONWellKnown(w, info, rv, fopts)
if ok || err != nil {
return
}
}
// Handle override if rv implements amino.Marshaler.
if info.IsAminoMarshaler {
// First, encode rv into repr instance.
var (
rrv reflect.Value
rinfo *TypeInfo
)
rrv, err = toReprObject(rv)
if err != nil {
return
}
rinfo = info.ReprType
// Then, encode the repr instance.
err = cdc.encodeReflectJSON(w, rinfo, rrv, fopts)
return
}
switch info.Type.Kind() {
// ----------------------------------------
// Complex
case reflect.Interface:
return cdc.encodeReflectJSONInterface(w, info, rv, fopts)
case reflect.Array, reflect.Slice:
return cdc.encodeReflectJSONList(w, info, rv, fopts)
case reflect.Struct:
return cdc.encodeReflectJSONStruct(w, info, rv, fopts)
// ----------------------------------------
// Signed, Unsigned
case reflect.Int64, reflect.Int:
_, err = fmt.Fprintf(w, `"%d"`, rv.Int()) // JS can't handle int64
return
case reflect.Uint64, reflect.Uint:
_, err = fmt.Fprintf(w, `"%d"`, rv.Uint()) // JS can't handle uint64
return
case reflect.Int32, reflect.Int16, reflect.Int8,
reflect.Uint32, reflect.Uint16, reflect.Uint8:
return invokeStdlibJSONMarshal(w, rv.Interface())
// ----------------------------------------
// Misc
case reflect.Float64, reflect.Float32:
if !fopts.Unsafe {
return errors.New("amino.JSON float* support requires `amino:\"unsafe\"`")
}
fallthrough
case reflect.Bool, reflect.String:
return invokeStdlibJSONMarshal(w, rv.Interface())
// ----------------------------------------
// Default
default:
panic(fmt.Sprintf("unsupported type %v", info.Type.Kind()))
}
}
func (cdc *Codec) encodeReflectJSONInterface(w io.Writer, iinfo *TypeInfo, rv reflect.Value,
fopts FieldOptions,
) (err error) {
if printLog {
fmt.Println("(e) encodeReflectJSONInterface")
defer func() {
fmt.Printf("(e) -> err: %v\n", err)
}()
}
// Special case when rv is nil, just write "null".
if rv.IsNil() {
err = writeStr(w, `null`)
return
}
// Get concrete non-pointer reflect value & type.
crv := rv.Elem()
_, crvIsPtr, crvIsNilPtr := maybeDerefValue(crv)
if crvIsPtr && crv.Kind() == reflect.Interface {
// See "MARKER: No interface-pointers" in codec.go
panic("should not happen")
}
if crvIsNilPtr {
panic(fmt.Sprintf("Illegal nil-pointer of type %v for registered interface %v. "+
"For compatibility with other languages, nil-pointer interface values are forbidden.", crv.Type(), iinfo.Type))
}
crt := crv.Type()
// Get *TypeInfo for concrete type.
var cinfo *TypeInfo
cinfo, err = cdc.getTypeInfoWLock(crt)
if err != nil {
return
}
if !cinfo.Registered {
err = errors.New("cannot encode unregistered concrete type %v", crt)
return
}
// Write Value to buffer
buf := new(bytes.Buffer)
cdc.encodeReflectJSON(buf, cinfo, crv, fopts)
value := buf.Bytes()
if len(value) == 0 {
err = errors.New("JSON bytes cannot be empty")
return
}
if cinfo.IsJSONAnyValueType || (cinfo.IsAminoMarshaler && cinfo.ReprType.IsJSONAnyValueType) {
// Sanity check
if value[0] == '{' || value[len(value)-1] == '}' {
err = errors.New("unexpected JSON object %s", value)
return
}
// Write TypeURL
err = writeStr(w, _fmt(`{"@type":"%s","value":`, cinfo.TypeURL))
if err != nil {
return
}
// Write Value
err = writeStr(w, string(value))
if err != nil {
return
}
// Write closing brace.
err = writeStr(w, `}`)
return
} else {
// Sanity check
if value[0] != '{' || value[len(value)-1] != '}' {
err = errors.New("expected JSON object but got %s", value)
return
}
// Write TypeURL
err = writeStr(w, _fmt(`{"@type":"%s"`, cinfo.TypeURL))
if err != nil {
return
}
// Write Value
if len(value) > 2 {
err = writeStr(w, ","+string(value[1:]))
} else {
err = writeStr(w, `}`)
}
return
}
}
func (cdc *Codec) encodeReflectJSONList(w io.Writer, info *TypeInfo, rv reflect.Value, fopts FieldOptions) (err error) {
if printLog {
fmt.Println("(e) encodeReflectJSONList")
defer func() {
fmt.Printf("(e) -> err: %v\n", err)
}()
}
// Special case when list is a nil slice, just write "null".
// Empty slices and arrays are not encoded as "null".
if rv.Kind() == reflect.Slice && rv.IsNil() {
err = writeStr(w, `null`)
return
}
ert := info.Type.Elem()
length := rv.Len()
switch ert.Kind() {
case reflect.Uint8: // Special case: byte array
// Write bytes in base64.
// NOTE: Base64 encoding preserves the exact original number of bytes.
// Get readable slice of bytes.
var bz []byte
if rv.CanAddr() {
bz = rv.Slice(0, length).Bytes()
} else {
bz = make([]byte, length)
reflect.Copy(reflect.ValueOf(bz), rv) // XXX: looks expensive!
}
var jsonBytes []byte
jsonBytes, err = json.Marshal(bz) // base64 encode
if err != nil {
return
}
_, err = w.Write(jsonBytes)
return
default:
// Open square bracket.
err = writeStr(w, `[`)
if err != nil {
return
}
// Write elements with comma.
var einfo *TypeInfo
einfo, err = cdc.getTypeInfoWLock(ert)
if err != nil {
return
}
for i := 0; i < length; i++ {
// Get dereferenced element value and info.
erv := rv.Index(i)
if erv.Kind() == reflect.Ptr &&
erv.IsNil() {
// then
err = writeStr(w, `null`)
} else {
err = cdc.encodeReflectJSON(w, einfo, erv, fopts)
}
if err != nil {
return
}
// Add a comma if it isn't the last item.
if i != length-1 {
err = writeStr(w, `,`)
if err != nil {
return
}
}
}
// Close square bracket.
defer func() {
err = writeStr(w, `]`)
}()
return
}
}
func (cdc *Codec) encodeReflectJSONStruct(w io.Writer, info *TypeInfo, rv reflect.Value, _ FieldOptions) (err error) {
if printLog {
fmt.Println("(e) encodeReflectJSONStruct")
defer func() {
fmt.Printf("(e) -> err: %v\n", err)
}()
}
// Part 1.
err = writeStr(w, `{`)
if err != nil {
return
}
// Part 2.
defer func() {
if err == nil {
err = writeStr(w, `}`)
}
}()
writeComma := false
for _, field := range info.Fields {
finfo := field.TypeInfo
// Get dereferenced field value and info.
frv, _, frvIsNil := maybeDerefValue(rv.Field(field.Index))
// If frv is empty and omitempty, skip it.
// NOTE: Unlike Amino:binary, we don't skip null fields unless "omitempty".
if field.JSONOmitEmpty && isJSONEmpty(frv, field.ZeroValue) {
continue
}
// Now we know we're going to write something.
// Add a comma if we need to.
if writeComma {
err = writeStr(w, `,`)
if err != nil {
return
}
writeComma = false //nolint:ineffassign
}
// Write field JSON name.
err = invokeStdlibJSONMarshal(w, field.JSONName)
if err != nil {
return
}
// Write colon.
err = writeStr(w, `:`)
if err != nil {
return
}
// Write field value.
if frvIsNil {
err = writeStr(w, `null`)
} else {
err = cdc.encodeReflectJSON(w, finfo, frv, field.FieldOptions)
}
if err != nil {
return
}
writeComma = true
}
return err
}
// ----------------------------------------
// Misc.
func invokeStdlibJSONMarshal(w io.Writer, v interface{}) error {
// Note: Please don't stream out the output because that adds a newline
// using json.NewEncoder(w).Encode(data)
// as per https://golang.org/pkg/encoding/json/#Encoder.Encode
blob, err := json.Marshal(v)
if err != nil {
return err
}
_, err = w.Write(blob)
return err
}
func writeStr(w io.Writer, s string) (err error) {
_, err = w.Write([]byte(s))
return
}
func _fmt(s string, args ...interface{}) string {
return fmt.Sprintf(s, args...)
}
// For json:",omitempty".
// Returns true for zero values, but also non-nil zero-length slices and strings.
func isJSONEmpty(rv reflect.Value, zrv reflect.Value) bool {
if !rv.IsValid() {
return true
}
if reflect.DeepEqual(rv.Interface(), zrv.Interface()) {
return true
}
switch rv.Kind() {
case reflect.Slice, reflect.Array, reflect.String:
if rv.Len() == 0 {
return true
}
}
return false
}
func isJSONAnyValueType(rt reflect.Type) bool {
if isJSONWellKnownType(rt) {
// All well known types are to be encoded as "{@type,value}" in
// JSON. Some of these may be structs/objects, such as
// gAnyType, but nevertheless they must be encoded as
// {@type,value}, the latter specifically
// {@type:"/google.protobuf.Any",value:{@type,value}).
return true
}
// Otherwise, it depends on the kind.
switch rt.Kind() {
case
reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64,
// Primitive types get special {@type,value} treatment. In
// binary form, most of these types would be encoded
// wrapped in an implicit struct, except for lists (both of
// bytes and of anything else), and for strings...
reflect.Array, reflect.Slice, reflect.String:
// ...which are all non-objects that must be encoded as
// {@type,value}.
return true
default:
return false
}
}