]>
Commit | Line | Data |
---|---|---|
7a938933 ILT |
1 | // Copyright 2010 The Go Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style | |
3 | // license that can be found in the LICENSE file. | |
4 | ||
5 | // Represents JSON data structure using native Go types: booleans, floats, | |
6 | // strings, arrays, and maps. | |
7 | ||
8 | package json | |
9 | ||
10 | import ( | |
bae90c98 | 11 | "bytes" |
f038dae6 | 12 | "encoding" |
5133f00e | 13 | "encoding/base64" |
2fd401c8 | 14 | "errors" |
df1304ee | 15 | "fmt" |
7a938933 ILT |
16 | "reflect" |
17 | "runtime" | |
18 | "strconv" | |
7a938933 | 19 | "unicode" |
9c63abc9 ILT |
20 | "unicode/utf16" |
21 | "unicode/utf8" | |
7a938933 ILT |
22 | ) |
23 | ||
24 | // Unmarshal parses the JSON-encoded data and stores the result | |
25 | // in the value pointed to by v. | |
26 | // | |
d8f41257 | 27 | // Unmarshal uses the inverse of the encodings that |
7a938933 ILT |
28 | // Marshal uses, allocating maps, slices, and pointers as necessary, |
29 | // with the following additional rules: | |
30 | // | |
d8f41257 ILT |
31 | // To unmarshal JSON into a pointer, Unmarshal first handles the case of |
32 | // the JSON being the JSON literal null. In that case, Unmarshal sets | |
33 | // the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into | |
34 | // the value pointed at by the pointer. If the pointer is nil, Unmarshal | |
35 | // allocates a new value for it to point to. | |
36 | // | |
be47d6ec ILT |
37 | // To unmarshal JSON into a struct, Unmarshal matches incoming object |
38 | // keys to the keys used by Marshal (either the struct field name or its tag), | |
39 | // preferring an exact match but also accepting a case-insensitive match. | |
40 | // | |
f038dae6 | 41 | // To unmarshal JSON into an interface value, |
d8f41257 | 42 | // Unmarshal stores one of these in the interface value: |
7a938933 ILT |
43 | // |
44 | // bool, for JSON booleans | |
45 | // float64, for JSON numbers | |
46 | // string, for JSON strings | |
47 | // []interface{}, for JSON arrays | |
48 | // map[string]interface{}, for JSON objects | |
49 | // nil for JSON null | |
50 | // | |
51 | // If a JSON value is not appropriate for a given target type, | |
52 | // or if a JSON number overflows the target type, Unmarshal | |
53 | // skips that field and completes the unmarshalling as best it can. | |
54 | // If no more serious errors are encountered, Unmarshal returns | |
55 | // an UnmarshalTypeError describing the earliest such error. | |
56 | // | |
be47d6ec ILT |
57 | // When unmarshaling quoted strings, invalid UTF-8 or |
58 | // invalid UTF-16 surrogate pairs are not treated as an error. | |
59 | // Instead, they are replaced by the Unicode replacement | |
60 | // character U+FFFD. | |
61 | // | |
2fd401c8 | 62 | func Unmarshal(data []byte, v interface{}) error { |
be47d6ec | 63 | // Check for well-formedness. |
7a938933 ILT |
64 | // Avoids filling out half a data structure |
65 | // before discovering a JSON syntax error. | |
be47d6ec | 66 | var d decodeState |
7a938933 ILT |
67 | err := checkValid(data, &d.scan) |
68 | if err != nil { | |
69 | return err | |
70 | } | |
71 | ||
be47d6ec | 72 | d.init(data) |
7a938933 ILT |
73 | return d.unmarshal(v) |
74 | } | |
75 | ||
76 | // Unmarshaler is the interface implemented by objects | |
77 | // that can unmarshal a JSON description of themselves. | |
fabcaa8d ILT |
78 | // The input can be assumed to be a valid encoding of |
79 | // a JSON value. UnmarshalJSON must copy the JSON data | |
7a938933 ILT |
80 | // if it wishes to retain the data after returning. |
81 | type Unmarshaler interface { | |
2fd401c8 | 82 | UnmarshalJSON([]byte) error |
7a938933 ILT |
83 | } |
84 | ||
7a938933 ILT |
85 | // An UnmarshalTypeError describes a JSON value that was |
86 | // not appropriate for a value of a specific Go type. | |
87 | type UnmarshalTypeError struct { | |
88 | Value string // description of JSON value - "bool", "array", "number -5" | |
89 | Type reflect.Type // type of Go value it could not be assigned to | |
90 | } | |
91 | ||
2fd401c8 | 92 | func (e *UnmarshalTypeError) Error() string { |
7a938933 ILT |
93 | return "json: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String() |
94 | } | |
95 | ||
96 | // An UnmarshalFieldError describes a JSON object key that | |
97 | // led to an unexported (and therefore unwritable) struct field. | |
d6f2922e | 98 | // (No longer used; kept for compatibility.) |
7a938933 ILT |
99 | type UnmarshalFieldError struct { |
100 | Key string | |
9ff56c95 | 101 | Type reflect.Type |
7a938933 ILT |
102 | Field reflect.StructField |
103 | } | |
104 | ||
2fd401c8 | 105 | func (e *UnmarshalFieldError) Error() string { |
7a938933 ILT |
106 | return "json: cannot unmarshal object key " + strconv.Quote(e.Key) + " into unexported field " + e.Field.Name + " of type " + e.Type.String() |
107 | } | |
108 | ||
109 | // An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. | |
110 | // (The argument to Unmarshal must be a non-nil pointer.) | |
111 | type InvalidUnmarshalError struct { | |
112 | Type reflect.Type | |
113 | } | |
114 | ||
2fd401c8 | 115 | func (e *InvalidUnmarshalError) Error() string { |
7a938933 ILT |
116 | if e.Type == nil { |
117 | return "json: Unmarshal(nil)" | |
118 | } | |
119 | ||
9ff56c95 | 120 | if e.Type.Kind() != reflect.Ptr { |
7a938933 ILT |
121 | return "json: Unmarshal(non-pointer " + e.Type.String() + ")" |
122 | } | |
123 | return "json: Unmarshal(nil " + e.Type.String() + ")" | |
124 | } | |
125 | ||
2fd401c8 | 126 | func (d *decodeState) unmarshal(v interface{}) (err error) { |
7a938933 ILT |
127 | defer func() { |
128 | if r := recover(); r != nil { | |
129 | if _, ok := r.(runtime.Error); ok { | |
130 | panic(r) | |
131 | } | |
2fd401c8 | 132 | err = r.(error) |
7a938933 ILT |
133 | } |
134 | }() | |
135 | ||
9ff56c95 | 136 | rv := reflect.ValueOf(v) |
409a5e7e | 137 | if rv.Kind() != reflect.Ptr || rv.IsNil() { |
9ff56c95 | 138 | return &InvalidUnmarshalError{reflect.TypeOf(v)} |
7a938933 ILT |
139 | } |
140 | ||
141 | d.scan.reset() | |
409a5e7e | 142 | // We decode rv not rv.Elem because the Unmarshaler interface |
7a938933 ILT |
143 | // test must be applied at the top level of the value. |
144 | d.value(rv) | |
145 | return d.savedError | |
146 | } | |
147 | ||
4ccad563 ILT |
148 | // A Number represents a JSON number literal. |
149 | type Number string | |
150 | ||
151 | // String returns the literal text of the number. | |
152 | func (n Number) String() string { return string(n) } | |
153 | ||
154 | // Float64 returns the number as a float64. | |
155 | func (n Number) Float64() (float64, error) { | |
156 | return strconv.ParseFloat(string(n), 64) | |
157 | } | |
158 | ||
159 | // Int64 returns the number as an int64. | |
160 | func (n Number) Int64() (int64, error) { | |
161 | return strconv.ParseInt(string(n), 10, 64) | |
162 | } | |
163 | ||
7a938933 ILT |
164 | // decodeState represents the state while decoding a JSON value. |
165 | type decodeState struct { | |
166 | data []byte | |
167 | off int // read offset in data | |
168 | scan scanner | |
169 | nextscan scanner // for calls to nextValue | |
2fd401c8 | 170 | savedError error |
9d49f4d0 | 171 | tempstr string // scratch space to avoid some allocations |
4ccad563 | 172 | useNumber bool |
7a938933 ILT |
173 | } |
174 | ||
175 | // errPhase is used for errors that should not happen unless | |
176 | // there is a bug in the JSON decoder or something is editing | |
177 | // the data slice while the decoder executes. | |
2fd401c8 | 178 | var errPhase = errors.New("JSON decoder out of sync - data changing underfoot?") |
7a938933 ILT |
179 | |
180 | func (d *decodeState) init(data []byte) *decodeState { | |
181 | d.data = data | |
182 | d.off = 0 | |
183 | d.savedError = nil | |
184 | return d | |
185 | } | |
186 | ||
187 | // error aborts the decoding by panicking with err. | |
2fd401c8 | 188 | func (d *decodeState) error(err error) { |
7a938933 ILT |
189 | panic(err) |
190 | } | |
191 | ||
192 | // saveError saves the first err it is called with, | |
193 | // for reporting at the end of the unmarshal. | |
2fd401c8 | 194 | func (d *decodeState) saveError(err error) { |
7a938933 ILT |
195 | if d.savedError == nil { |
196 | d.savedError = err | |
197 | } | |
198 | } | |
199 | ||
200 | // next cuts off and returns the next full JSON value in d.data[d.off:]. | |
201 | // The next value is known to be an object or array, not a literal. | |
202 | func (d *decodeState) next() []byte { | |
203 | c := d.data[d.off] | |
204 | item, rest, err := nextValue(d.data[d.off:], &d.nextscan) | |
205 | if err != nil { | |
206 | d.error(err) | |
207 | } | |
208 | d.off = len(d.data) - len(rest) | |
209 | ||
210 | // Our scanner has seen the opening brace/bracket | |
211 | // and thinks we're still in the middle of the object. | |
212 | // invent a closing brace/bracket to get it out. | |
213 | if c == '{' { | |
214 | d.scan.step(&d.scan, '}') | |
215 | } else { | |
216 | d.scan.step(&d.scan, ']') | |
217 | } | |
218 | ||
219 | return item | |
220 | } | |
221 | ||
222 | // scanWhile processes bytes in d.data[d.off:] until it | |
223 | // receives a scan code not equal to op. | |
224 | // It updates d.off and returns the new scan code. | |
225 | func (d *decodeState) scanWhile(op int) int { | |
226 | var newOp int | |
227 | for { | |
228 | if d.off >= len(d.data) { | |
229 | newOp = d.scan.eof() | |
230 | d.off = len(d.data) + 1 // mark processed EOF with len+1 | |
231 | } else { | |
232 | c := int(d.data[d.off]) | |
233 | d.off++ | |
234 | newOp = d.scan.step(&d.scan, c) | |
235 | } | |
236 | if newOp != op { | |
237 | break | |
238 | } | |
239 | } | |
240 | return newOp | |
241 | } | |
242 | ||
243 | // value decodes a JSON value from d.data[d.off:] into the value. | |
244 | // it updates d.off to point past the decoded value. | |
245 | func (d *decodeState) value(v reflect.Value) { | |
9ff56c95 | 246 | if !v.IsValid() { |
7a938933 ILT |
247 | _, rest, err := nextValue(d.data[d.off:], &d.nextscan) |
248 | if err != nil { | |
249 | d.error(err) | |
250 | } | |
251 | d.off = len(d.data) - len(rest) | |
252 | ||
253 | // d.scan thinks we're still at the beginning of the item. | |
254 | // Feed in an empty string - the shortest, simplest value - | |
255 | // so that it knows we got to the end of the value. | |
ab61e9c4 | 256 | if d.scan.redo { |
df4aa89a ILT |
257 | // rewind. |
258 | d.scan.redo = false | |
259 | d.scan.step = stateBeginValue | |
7a938933 ILT |
260 | } |
261 | d.scan.step(&d.scan, '"') | |
262 | d.scan.step(&d.scan, '"') | |
be47d6ec ILT |
263 | |
264 | n := len(d.scan.parseState) | |
265 | if n > 0 && d.scan.parseState[n-1] == parseObjectKey { | |
266 | // d.scan thinks we just read an object key; finish the object | |
267 | d.scan.step(&d.scan, ':') | |
268 | d.scan.step(&d.scan, '"') | |
269 | d.scan.step(&d.scan, '"') | |
270 | d.scan.step(&d.scan, '}') | |
271 | } | |
272 | ||
7a938933 ILT |
273 | return |
274 | } | |
275 | ||
276 | switch op := d.scanWhile(scanSkipSpace); op { | |
277 | default: | |
278 | d.error(errPhase) | |
279 | ||
280 | case scanBeginArray: | |
281 | d.array(v) | |
282 | ||
283 | case scanBeginObject: | |
284 | d.object(v) | |
285 | ||
286 | case scanBeginLiteral: | |
287 | d.literal(v) | |
288 | } | |
289 | } | |
290 | ||
291 | // indirect walks down v allocating pointers as needed, | |
292 | // until it gets to a non-pointer. | |
293 | // if it encounters an Unmarshaler, indirect stops and returns that. | |
d8f41257 | 294 | // if decodingNull is true, indirect stops at the last pointer so it can be set to nil. |
f038dae6 | 295 | func (d *decodeState) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnmarshaler, reflect.Value) { |
adb0401d ILT |
296 | // If v is a named type and is addressable, |
297 | // start with its address, so that if the type has pointer methods, | |
298 | // we find them. | |
299 | if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() { | |
300 | v = v.Addr() | |
301 | } | |
7a938933 | 302 | for { |
08a680a8 ILT |
303 | // Load value from interface, but only if the result will be |
304 | // usefully addressable. | |
4ccad563 ILT |
305 | if v.Kind() == reflect.Interface && !v.IsNil() { |
306 | e := v.Elem() | |
08a680a8 ILT |
307 | if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) { |
308 | v = e | |
309 | continue | |
310 | } | |
7a938933 | 311 | } |
9ff56c95 | 312 | |
4ccad563 | 313 | if v.Kind() != reflect.Ptr { |
7a938933 ILT |
314 | break |
315 | } | |
9ff56c95 | 316 | |
4ccad563 ILT |
317 | if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() { |
318 | break | |
7a938933 | 319 | } |
4ccad563 ILT |
320 | if v.IsNil() { |
321 | v.Set(reflect.New(v.Type().Elem())) | |
7a938933 | 322 | } |
4ccad563 | 323 | if v.Type().NumMethod() > 0 { |
f038dae6 ILT |
324 | if u, ok := v.Interface().(Unmarshaler); ok { |
325 | return u, nil, reflect.Value{} | |
326 | } | |
327 | if u, ok := v.Interface().(encoding.TextUnmarshaler); ok { | |
328 | return nil, u, reflect.Value{} | |
4ccad563 | 329 | } |
7a938933 | 330 | } |
4ccad563 | 331 | v = v.Elem() |
7a938933 | 332 | } |
f038dae6 | 333 | return nil, nil, v |
7a938933 ILT |
334 | } |
335 | ||
336 | // array consumes an array from d.data[d.off-1:], decoding into the value v. | |
337 | // the first byte of the array ('[') has been read already. | |
338 | func (d *decodeState) array(v reflect.Value) { | |
339 | // Check for unmarshaler. | |
f038dae6 ILT |
340 | u, ut, pv := d.indirect(v, false) |
341 | if u != nil { | |
7a938933 | 342 | d.off-- |
f038dae6 | 343 | err := u.UnmarshalJSON(d.next()) |
7a938933 ILT |
344 | if err != nil { |
345 | d.error(err) | |
346 | } | |
347 | return | |
348 | } | |
f038dae6 ILT |
349 | if ut != nil { |
350 | d.saveError(&UnmarshalTypeError{"array", v.Type()}) | |
351 | d.off-- | |
352 | d.next() | |
353 | return | |
354 | } | |
355 | ||
7a938933 ILT |
356 | v = pv |
357 | ||
7a938933 | 358 | // Check type of target. |
df4aa89a | 359 | switch v.Kind() { |
d6f2922e ILT |
360 | case reflect.Interface: |
361 | if v.NumMethod() == 0 { | |
362 | // Decoding into nil interface? Switch to non-reflect code. | |
363 | v.Set(reflect.ValueOf(d.arrayInterface())) | |
364 | return | |
365 | } | |
366 | // Otherwise it's invalid. | |
367 | fallthrough | |
df4aa89a | 368 | default: |
7a938933 ILT |
369 | d.saveError(&UnmarshalTypeError{"array", v.Type()}) |
370 | d.off-- | |
371 | d.next() | |
372 | return | |
df4aa89a ILT |
373 | case reflect.Array: |
374 | case reflect.Slice: | |
375 | break | |
7a938933 ILT |
376 | } |
377 | ||
7a938933 ILT |
378 | i := 0 |
379 | for { | |
380 | // Look ahead for ] - can only happen on first iteration. | |
381 | op := d.scanWhile(scanSkipSpace) | |
382 | if op == scanEndArray { | |
383 | break | |
384 | } | |
385 | ||
386 | // Back up so d.value can have the byte we just read. | |
387 | d.off-- | |
388 | d.scan.undo(op) | |
389 | ||
390 | // Get element of array, growing if necessary. | |
df4aa89a ILT |
391 | if v.Kind() == reflect.Slice { |
392 | // Grow slice if necessary | |
393 | if i >= v.Cap() { | |
394 | newcap := v.Cap() + v.Cap()/2 | |
395 | if newcap < 4 { | |
396 | newcap = 4 | |
397 | } | |
398 | newv := reflect.MakeSlice(v.Type(), v.Len(), newcap) | |
399 | reflect.Copy(newv, v) | |
400 | v.Set(newv) | |
401 | } | |
402 | if i >= v.Len() { | |
403 | v.SetLen(i + 1) | |
7a938933 | 404 | } |
7a938933 ILT |
405 | } |
406 | ||
df4aa89a ILT |
407 | if i < v.Len() { |
408 | // Decode into element. | |
409 | d.value(v.Index(i)) | |
7a938933 ILT |
410 | } else { |
411 | // Ran out of fixed array: skip. | |
9ff56c95 | 412 | d.value(reflect.Value{}) |
7a938933 ILT |
413 | } |
414 | i++ | |
415 | ||
416 | // Next token must be , or ]. | |
417 | op = d.scanWhile(scanSkipSpace) | |
418 | if op == scanEndArray { | |
419 | break | |
420 | } | |
421 | if op != scanArrayValue { | |
422 | d.error(errPhase) | |
423 | } | |
424 | } | |
ab61e9c4 | 425 | |
df4aa89a ILT |
426 | if i < v.Len() { |
427 | if v.Kind() == reflect.Array { | |
7a938933 | 428 | // Array. Zero the rest. |
df4aa89a ILT |
429 | z := reflect.Zero(v.Type().Elem()) |
430 | for ; i < v.Len(); i++ { | |
431 | v.Index(i).Set(z) | |
7a938933 ILT |
432 | } |
433 | } else { | |
df4aa89a | 434 | v.SetLen(i) |
7a938933 ILT |
435 | } |
436 | } | |
df4aa89a ILT |
437 | if i == 0 && v.Kind() == reflect.Slice { |
438 | v.Set(reflect.MakeSlice(v.Type(), 0, 0)) | |
ab61e9c4 | 439 | } |
7a938933 ILT |
440 | } |
441 | ||
7a938933 ILT |
442 | // object consumes an object from d.data[d.off-1:], decoding into the value v. |
443 | // the first byte of the object ('{') has been read already. | |
444 | func (d *decodeState) object(v reflect.Value) { | |
445 | // Check for unmarshaler. | |
f038dae6 ILT |
446 | u, ut, pv := d.indirect(v, false) |
447 | if u != nil { | |
7a938933 | 448 | d.off-- |
f038dae6 | 449 | err := u.UnmarshalJSON(d.next()) |
7a938933 ILT |
450 | if err != nil { |
451 | d.error(err) | |
452 | } | |
453 | return | |
454 | } | |
f038dae6 ILT |
455 | if ut != nil { |
456 | d.saveError(&UnmarshalTypeError{"object", v.Type()}) | |
457 | d.off-- | |
458 | d.next() // skip over { } in input | |
459 | return | |
460 | } | |
7a938933 ILT |
461 | v = pv |
462 | ||
463 | // Decoding into nil interface? Switch to non-reflect code. | |
d6f2922e | 464 | if v.Kind() == reflect.Interface && v.NumMethod() == 0 { |
409a5e7e | 465 | v.Set(reflect.ValueOf(d.objectInterface())) |
7a938933 ILT |
466 | return |
467 | } | |
468 | ||
469 | // Check type of target: struct or map[string]T | |
9ff56c95 ILT |
470 | switch v.Kind() { |
471 | case reflect.Map: | |
d6f2922e | 472 | // map must have string kind |
9ff56c95 | 473 | t := v.Type() |
d6f2922e | 474 | if t.Key().Kind() != reflect.String { |
7a938933 ILT |
475 | d.saveError(&UnmarshalTypeError{"object", v.Type()}) |
476 | break | |
477 | } | |
409a5e7e ILT |
478 | if v.IsNil() { |
479 | v.Set(reflect.MakeMap(t)) | |
7a938933 | 480 | } |
9ff56c95 | 481 | case reflect.Struct: |
d6f2922e | 482 | |
7a938933 ILT |
483 | default: |
484 | d.saveError(&UnmarshalTypeError{"object", v.Type()}) | |
7a938933 ILT |
485 | d.off-- |
486 | d.next() // skip over { } in input | |
487 | return | |
488 | } | |
489 | ||
9ff56c95 ILT |
490 | var mapElem reflect.Value |
491 | ||
7a938933 ILT |
492 | for { |
493 | // Read opening " of string key or closing }. | |
494 | op := d.scanWhile(scanSkipSpace) | |
495 | if op == scanEndObject { | |
496 | // closing } - can only happen on first iteration. | |
497 | break | |
498 | } | |
499 | if op != scanBeginLiteral { | |
500 | d.error(errPhase) | |
501 | } | |
502 | ||
bae90c98 | 503 | // Read key. |
7a938933 ILT |
504 | start := d.off - 1 |
505 | op = d.scanWhile(scanContinue) | |
506 | item := d.data[start : d.off-1] | |
bae90c98 | 507 | key, ok := unquoteBytes(item) |
7a938933 ILT |
508 | if !ok { |
509 | d.error(errPhase) | |
510 | } | |
511 | ||
512 | // Figure out field corresponding to key. | |
513 | var subv reflect.Value | |
9d49f4d0 ILT |
514 | destring := false // whether the value is wrapped in a string to be decoded first |
515 | ||
409a5e7e ILT |
516 | if v.Kind() == reflect.Map { |
517 | elemType := v.Type().Elem() | |
9ff56c95 ILT |
518 | if !mapElem.IsValid() { |
519 | mapElem = reflect.New(elemType).Elem() | |
520 | } else { | |
521 | mapElem.Set(reflect.Zero(elemType)) | |
522 | } | |
523 | subv = mapElem | |
7a938933 | 524 | } else { |
4ccad563 | 525 | var f *field |
409a5e7e | 526 | fields := cachedTypeFields(v.Type()) |
4ccad563 ILT |
527 | for i := range fields { |
528 | ff := &fields[i] | |
bae90c98 | 529 | if bytes.Equal(ff.nameBytes, key) { |
4ccad563 ILT |
530 | f = ff |
531 | break | |
d8f41257 | 532 | } |
bae90c98 | 533 | if f == nil && ff.equalFold(ff.nameBytes, key) { |
4ccad563 | 534 | f = ff |
7a938933 | 535 | } |
7a938933 | 536 | } |
4ccad563 | 537 | if f != nil { |
409a5e7e | 538 | subv = v |
4ccad563 ILT |
539 | destring = f.quoted |
540 | for _, i := range f.index { | |
541 | if subv.Kind() == reflect.Ptr { | |
542 | if subv.IsNil() { | |
543 | subv.Set(reflect.New(subv.Type().Elem())) | |
544 | } | |
545 | subv = subv.Elem() | |
546 | } | |
547 | subv = subv.Field(i) | |
548 | } | |
7a938933 ILT |
549 | } |
550 | } | |
551 | ||
552 | // Read : before value. | |
553 | if op == scanSkipSpace { | |
554 | op = d.scanWhile(scanSkipSpace) | |
555 | } | |
556 | if op != scanObjectKey { | |
557 | d.error(errPhase) | |
558 | } | |
559 | ||
560 | // Read value. | |
9d49f4d0 ILT |
561 | if destring { |
562 | d.value(reflect.ValueOf(&d.tempstr)) | |
df1304ee | 563 | d.literalStore([]byte(d.tempstr), subv, true) |
bae90c98 | 564 | d.tempstr = "" // Zero scratch space for successive values. |
9d49f4d0 ILT |
565 | } else { |
566 | d.value(subv) | |
567 | } | |
d6f2922e | 568 | |
7a938933 ILT |
569 | // Write value back to map; |
570 | // if using struct, subv points into struct already. | |
409a5e7e | 571 | if v.Kind() == reflect.Map { |
d6f2922e ILT |
572 | kv := reflect.ValueOf(key).Convert(v.Type().Key()) |
573 | v.SetMapIndex(kv, subv) | |
7a938933 ILT |
574 | } |
575 | ||
576 | // Next token must be , or }. | |
577 | op = d.scanWhile(scanSkipSpace) | |
578 | if op == scanEndObject { | |
579 | break | |
580 | } | |
581 | if op != scanObjectValue { | |
582 | d.error(errPhase) | |
583 | } | |
584 | } | |
585 | } | |
586 | ||
587 | // literal consumes a literal from d.data[d.off-1:], decoding into the value v. | |
588 | // The first byte of the literal has been read already | |
589 | // (that's how the caller knows it's a literal). | |
590 | func (d *decodeState) literal(v reflect.Value) { | |
591 | // All bytes inside literal return scanContinue op code. | |
592 | start := d.off - 1 | |
593 | op := d.scanWhile(scanContinue) | |
594 | ||
595 | // Scan read one byte too far; back up. | |
596 | d.off-- | |
597 | d.scan.undo(op) | |
7a938933 | 598 | |
df1304ee | 599 | d.literalStore(d.data[start:d.off], v, false) |
9d49f4d0 ILT |
600 | } |
601 | ||
4ccad563 ILT |
602 | // convertNumber converts the number literal s to a float64 or a Number |
603 | // depending on the setting of d.useNumber. | |
604 | func (d *decodeState) convertNumber(s string) (interface{}, error) { | |
605 | if d.useNumber { | |
606 | return Number(s), nil | |
607 | } | |
608 | f, err := strconv.ParseFloat(s, 64) | |
609 | if err != nil { | |
610 | return nil, &UnmarshalTypeError{"number " + s, reflect.TypeOf(0.0)} | |
611 | } | |
612 | return f, nil | |
613 | } | |
614 | ||
615 | var numberType = reflect.TypeOf(Number("")) | |
616 | ||
9d49f4d0 | 617 | // literalStore decodes a literal stored in item into v. |
df1304ee ILT |
618 | // |
619 | // fromQuoted indicates whether this literal came from unwrapping a | |
620 | // string from the ",string" struct tag option. this is used only to | |
621 | // produce more helpful error messages. | |
622 | func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool) { | |
7a938933 | 623 | // Check for unmarshaler. |
08a680a8 ILT |
624 | if len(item) == 0 { |
625 | //Empty string given | |
626 | d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
627 | return | |
628 | } | |
7a938933 | 629 | wantptr := item[0] == 'n' // null |
f038dae6 ILT |
630 | u, ut, pv := d.indirect(v, wantptr) |
631 | if u != nil { | |
632 | err := u.UnmarshalJSON(item) | |
633 | if err != nil { | |
634 | d.error(err) | |
635 | } | |
636 | return | |
637 | } | |
638 | if ut != nil { | |
639 | if item[0] != '"' { | |
640 | if fromQuoted { | |
641 | d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
642 | } else { | |
643 | d.saveError(&UnmarshalTypeError{"string", v.Type()}) | |
644 | } | |
645 | } | |
646 | s, ok := unquoteBytes(item) | |
647 | if !ok { | |
648 | if fromQuoted { | |
649 | d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
650 | } else { | |
651 | d.error(errPhase) | |
652 | } | |
653 | } | |
654 | err := ut.UnmarshalText(s) | |
7a938933 ILT |
655 | if err != nil { |
656 | d.error(err) | |
657 | } | |
658 | return | |
659 | } | |
f038dae6 | 660 | |
7a938933 ILT |
661 | v = pv |
662 | ||
663 | switch c := item[0]; c { | |
664 | case 'n': // null | |
9ff56c95 | 665 | switch v.Kind() { |
506cf9aa | 666 | case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice: |
9ff56c95 | 667 | v.Set(reflect.Zero(v.Type())) |
fabcaa8d | 668 | // otherwise, ignore null for primitives/string |
7a938933 | 669 | } |
7a938933 ILT |
670 | case 't', 'f': // true, false |
671 | value := c == 't' | |
9ff56c95 | 672 | switch v.Kind() { |
7a938933 | 673 | default: |
df1304ee ILT |
674 | if fromQuoted { |
675 | d.saveError(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
676 | } else { | |
677 | d.saveError(&UnmarshalTypeError{"bool", v.Type()}) | |
678 | } | |
9ff56c95 ILT |
679 | case reflect.Bool: |
680 | v.SetBool(value) | |
681 | case reflect.Interface: | |
d6f2922e ILT |
682 | if v.NumMethod() == 0 { |
683 | v.Set(reflect.ValueOf(value)) | |
684 | } else { | |
685 | d.saveError(&UnmarshalTypeError{"bool", v.Type()}) | |
686 | } | |
7a938933 ILT |
687 | } |
688 | ||
689 | case '"': // string | |
5133f00e | 690 | s, ok := unquoteBytes(item) |
7a938933 | 691 | if !ok { |
df1304ee ILT |
692 | if fromQuoted { |
693 | d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
694 | } else { | |
695 | d.error(errPhase) | |
696 | } | |
7a938933 | 697 | } |
9ff56c95 | 698 | switch v.Kind() { |
7a938933 ILT |
699 | default: |
700 | d.saveError(&UnmarshalTypeError{"string", v.Type()}) | |
9ff56c95 | 701 | case reflect.Slice: |
5133f00e ILT |
702 | if v.Type() != byteSliceType { |
703 | d.saveError(&UnmarshalTypeError{"string", v.Type()}) | |
704 | break | |
705 | } | |
706 | b := make([]byte, base64.StdEncoding.DecodedLen(len(s))) | |
707 | n, err := base64.StdEncoding.Decode(b, s) | |
708 | if err != nil { | |
709 | d.saveError(err) | |
710 | break | |
711 | } | |
9ff56c95 ILT |
712 | v.Set(reflect.ValueOf(b[0:n])) |
713 | case reflect.String: | |
714 | v.SetString(string(s)) | |
715 | case reflect.Interface: | |
d6f2922e ILT |
716 | if v.NumMethod() == 0 { |
717 | v.Set(reflect.ValueOf(string(s))) | |
718 | } else { | |
719 | d.saveError(&UnmarshalTypeError{"string", v.Type()}) | |
720 | } | |
7a938933 ILT |
721 | } |
722 | ||
723 | default: // number | |
724 | if c != '-' && (c < '0' || c > '9') { | |
df1304ee ILT |
725 | if fromQuoted { |
726 | d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
727 | } else { | |
728 | d.error(errPhase) | |
729 | } | |
7a938933 ILT |
730 | } |
731 | s := string(item) | |
9ff56c95 | 732 | switch v.Kind() { |
7a938933 | 733 | default: |
4ccad563 ILT |
734 | if v.Kind() == reflect.String && v.Type() == numberType { |
735 | v.SetString(s) | |
736 | break | |
737 | } | |
df1304ee ILT |
738 | if fromQuoted { |
739 | d.error(fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into %v", item, v.Type())) | |
740 | } else { | |
741 | d.error(&UnmarshalTypeError{"number", v.Type()}) | |
742 | } | |
9ff56c95 | 743 | case reflect.Interface: |
4ccad563 | 744 | n, err := d.convertNumber(s) |
7a938933 | 745 | if err != nil { |
4ccad563 | 746 | d.saveError(err) |
7a938933 ILT |
747 | break |
748 | } | |
d6f2922e ILT |
749 | if v.NumMethod() != 0 { |
750 | d.saveError(&UnmarshalTypeError{"number", v.Type()}) | |
751 | break | |
752 | } | |
9ff56c95 | 753 | v.Set(reflect.ValueOf(n)) |
7a938933 | 754 | |
9ff56c95 | 755 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
d5363590 | 756 | n, err := strconv.ParseInt(s, 10, 64) |
9ff56c95 | 757 | if err != nil || v.OverflowInt(n) { |
7a938933 ILT |
758 | d.saveError(&UnmarshalTypeError{"number " + s, v.Type()}) |
759 | break | |
760 | } | |
9ff56c95 | 761 | v.SetInt(n) |
7a938933 | 762 | |
9ff56c95 | 763 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
d5363590 | 764 | n, err := strconv.ParseUint(s, 10, 64) |
9ff56c95 | 765 | if err != nil || v.OverflowUint(n) { |
7a938933 ILT |
766 | d.saveError(&UnmarshalTypeError{"number " + s, v.Type()}) |
767 | break | |
768 | } | |
9ff56c95 | 769 | v.SetUint(n) |
7a938933 | 770 | |
9ff56c95 | 771 | case reflect.Float32, reflect.Float64: |
d5363590 | 772 | n, err := strconv.ParseFloat(s, v.Type().Bits()) |
9ff56c95 | 773 | if err != nil || v.OverflowFloat(n) { |
7a938933 ILT |
774 | d.saveError(&UnmarshalTypeError{"number " + s, v.Type()}) |
775 | break | |
776 | } | |
9ff56c95 | 777 | v.SetFloat(n) |
7a938933 ILT |
778 | } |
779 | } | |
780 | } | |
781 | ||
782 | // The xxxInterface routines build up a value to be stored | |
783 | // in an empty interface. They are not strictly necessary, | |
784 | // but they avoid the weight of reflection in this common case. | |
785 | ||
786 | // valueInterface is like value but returns interface{} | |
787 | func (d *decodeState) valueInterface() interface{} { | |
788 | switch d.scanWhile(scanSkipSpace) { | |
789 | default: | |
790 | d.error(errPhase) | |
be47d6ec | 791 | panic("unreachable") |
7a938933 ILT |
792 | case scanBeginArray: |
793 | return d.arrayInterface() | |
794 | case scanBeginObject: | |
795 | return d.objectInterface() | |
796 | case scanBeginLiteral: | |
797 | return d.literalInterface() | |
798 | } | |
7a938933 ILT |
799 | } |
800 | ||
801 | // arrayInterface is like array but returns []interface{}. | |
802 | func (d *decodeState) arrayInterface() []interface{} { | |
be47d6ec | 803 | var v = make([]interface{}, 0) |
7a938933 ILT |
804 | for { |
805 | // Look ahead for ] - can only happen on first iteration. | |
806 | op := d.scanWhile(scanSkipSpace) | |
807 | if op == scanEndArray { | |
808 | break | |
809 | } | |
810 | ||
811 | // Back up so d.value can have the byte we just read. | |
812 | d.off-- | |
813 | d.scan.undo(op) | |
814 | ||
adb0401d | 815 | v = append(v, d.valueInterface()) |
7a938933 ILT |
816 | |
817 | // Next token must be , or ]. | |
818 | op = d.scanWhile(scanSkipSpace) | |
819 | if op == scanEndArray { | |
820 | break | |
821 | } | |
822 | if op != scanArrayValue { | |
823 | d.error(errPhase) | |
824 | } | |
825 | } | |
826 | return v | |
827 | } | |
828 | ||
829 | // objectInterface is like object but returns map[string]interface{}. | |
830 | func (d *decodeState) objectInterface() map[string]interface{} { | |
831 | m := make(map[string]interface{}) | |
832 | for { | |
833 | // Read opening " of string key or closing }. | |
834 | op := d.scanWhile(scanSkipSpace) | |
835 | if op == scanEndObject { | |
836 | // closing } - can only happen on first iteration. | |
837 | break | |
838 | } | |
839 | if op != scanBeginLiteral { | |
840 | d.error(errPhase) | |
841 | } | |
842 | ||
843 | // Read string key. | |
844 | start := d.off - 1 | |
845 | op = d.scanWhile(scanContinue) | |
846 | item := d.data[start : d.off-1] | |
847 | key, ok := unquote(item) | |
848 | if !ok { | |
849 | d.error(errPhase) | |
850 | } | |
851 | ||
852 | // Read : before value. | |
853 | if op == scanSkipSpace { | |
854 | op = d.scanWhile(scanSkipSpace) | |
855 | } | |
856 | if op != scanObjectKey { | |
857 | d.error(errPhase) | |
858 | } | |
859 | ||
860 | // Read value. | |
861 | m[key] = d.valueInterface() | |
862 | ||
863 | // Next token must be , or }. | |
864 | op = d.scanWhile(scanSkipSpace) | |
865 | if op == scanEndObject { | |
866 | break | |
867 | } | |
868 | if op != scanObjectValue { | |
869 | d.error(errPhase) | |
870 | } | |
871 | } | |
872 | return m | |
873 | } | |
874 | ||
7a938933 ILT |
875 | // literalInterface is like literal but returns an interface value. |
876 | func (d *decodeState) literalInterface() interface{} { | |
877 | // All bytes inside literal return scanContinue op code. | |
878 | start := d.off - 1 | |
879 | op := d.scanWhile(scanContinue) | |
880 | ||
881 | // Scan read one byte too far; back up. | |
882 | d.off-- | |
883 | d.scan.undo(op) | |
884 | item := d.data[start:d.off] | |
885 | ||
886 | switch c := item[0]; c { | |
887 | case 'n': // null | |
888 | return nil | |
889 | ||
890 | case 't', 'f': // true, false | |
891 | return c == 't' | |
892 | ||
893 | case '"': // string | |
894 | s, ok := unquote(item) | |
895 | if !ok { | |
896 | d.error(errPhase) | |
897 | } | |
898 | return s | |
899 | ||
900 | default: // number | |
901 | if c != '-' && (c < '0' || c > '9') { | |
902 | d.error(errPhase) | |
903 | } | |
4ccad563 | 904 | n, err := d.convertNumber(string(item)) |
7a938933 | 905 | if err != nil { |
4ccad563 | 906 | d.saveError(err) |
7a938933 ILT |
907 | } |
908 | return n | |
909 | } | |
7a938933 ILT |
910 | } |
911 | ||
912 | // getu4 decodes \uXXXX from the beginning of s, returning the hex value, | |
913 | // or it returns -1. | |
506cf9aa | 914 | func getu4(s []byte) rune { |
7a938933 ILT |
915 | if len(s) < 6 || s[0] != '\\' || s[1] != 'u' { |
916 | return -1 | |
917 | } | |
d5363590 | 918 | r, err := strconv.ParseUint(string(s[2:6]), 16, 64) |
7a938933 ILT |
919 | if err != nil { |
920 | return -1 | |
921 | } | |
506cf9aa | 922 | return rune(r) |
7a938933 ILT |
923 | } |
924 | ||
925 | // unquote converts a quoted JSON string literal s into an actual string t. | |
926 | // The rules are different than for Go, so cannot use strconv.Unquote. | |
927 | func unquote(s []byte) (t string, ok bool) { | |
5133f00e ILT |
928 | s, ok = unquoteBytes(s) |
929 | t = string(s) | |
930 | return | |
931 | } | |
932 | ||
933 | func unquoteBytes(s []byte) (t []byte, ok bool) { | |
7a938933 ILT |
934 | if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' { |
935 | return | |
936 | } | |
5133f00e ILT |
937 | s = s[1 : len(s)-1] |
938 | ||
939 | // Check for unusual characters. If there are none, | |
940 | // then no unquoting is needed, so return a slice of the | |
941 | // original bytes. | |
942 | r := 0 | |
943 | for r < len(s) { | |
944 | c := s[r] | |
945 | if c == '\\' || c == '"' || c < ' ' { | |
946 | break | |
947 | } | |
948 | if c < utf8.RuneSelf { | |
949 | r++ | |
950 | continue | |
951 | } | |
506cf9aa ILT |
952 | rr, size := utf8.DecodeRune(s[r:]) |
953 | if rr == utf8.RuneError && size == 1 { | |
5133f00e ILT |
954 | break |
955 | } | |
956 | r += size | |
957 | } | |
958 | if r == len(s) { | |
959 | return s, true | |
960 | } | |
961 | ||
7a938933 | 962 | b := make([]byte, len(s)+2*utf8.UTFMax) |
5133f00e ILT |
963 | w := copy(b, s[0:r]) |
964 | for r < len(s) { | |
7a938933 ILT |
965 | // Out of room? Can only happen if s is full of |
966 | // malformed UTF-8 and we're replacing each | |
967 | // byte with RuneError. | |
968 | if w >= len(b)-2*utf8.UTFMax { | |
969 | nb := make([]byte, (len(b)+utf8.UTFMax)*2) | |
970 | copy(nb, b[0:w]) | |
971 | b = nb | |
972 | } | |
973 | switch c := s[r]; { | |
974 | case c == '\\': | |
975 | r++ | |
5133f00e | 976 | if r >= len(s) { |
7a938933 ILT |
977 | return |
978 | } | |
979 | switch s[r] { | |
980 | default: | |
981 | return | |
982 | case '"', '\\', '/', '\'': | |
983 | b[w] = s[r] | |
984 | r++ | |
985 | w++ | |
986 | case 'b': | |
987 | b[w] = '\b' | |
988 | r++ | |
989 | w++ | |
990 | case 'f': | |
991 | b[w] = '\f' | |
992 | r++ | |
993 | w++ | |
994 | case 'n': | |
995 | b[w] = '\n' | |
996 | r++ | |
997 | w++ | |
998 | case 'r': | |
999 | b[w] = '\r' | |
1000 | r++ | |
1001 | w++ | |
1002 | case 't': | |
1003 | b[w] = '\t' | |
1004 | r++ | |
1005 | w++ | |
1006 | case 'u': | |
1007 | r-- | |
506cf9aa ILT |
1008 | rr := getu4(s[r:]) |
1009 | if rr < 0 { | |
7a938933 ILT |
1010 | return |
1011 | } | |
1012 | r += 6 | |
506cf9aa ILT |
1013 | if utf16.IsSurrogate(rr) { |
1014 | rr1 := getu4(s[r:]) | |
1015 | if dec := utf16.DecodeRune(rr, rr1); dec != unicode.ReplacementChar { | |
7a938933 ILT |
1016 | // A valid pair; consume. |
1017 | r += 6 | |
ff5f50c5 | 1018 | w += utf8.EncodeRune(b[w:], dec) |
7a938933 ILT |
1019 | break |
1020 | } | |
1021 | // Invalid surrogate; fall back to replacement rune. | |
506cf9aa | 1022 | rr = unicode.ReplacementChar |
7a938933 | 1023 | } |
506cf9aa | 1024 | w += utf8.EncodeRune(b[w:], rr) |
7a938933 ILT |
1025 | } |
1026 | ||
1027 | // Quote, control characters are invalid. | |
1028 | case c == '"', c < ' ': | |
1029 | return | |
1030 | ||
1031 | // ASCII | |
1032 | case c < utf8.RuneSelf: | |
1033 | b[w] = c | |
1034 | r++ | |
1035 | w++ | |
1036 | ||
1037 | // Coerce to well-formed UTF-8. | |
1038 | default: | |
506cf9aa | 1039 | rr, size := utf8.DecodeRune(s[r:]) |
7a938933 | 1040 | r += size |
506cf9aa | 1041 | w += utf8.EncodeRune(b[w:], rr) |
7a938933 ILT |
1042 | } |
1043 | } | |
5133f00e | 1044 | return b[0:w], true |
7a938933 | 1045 | } |