Advertisement
kacejot

Realy large comment

May 17th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.84 KB | None | 0 0
  1. // Unmarshal parses the JSON-encoded data and stores the result
  2. // in the value pointed to by v. If v is nil or not a pointer,
  3. // Unmarshal returns an InvalidUnmarshalError.
  4. //
  5. // Unmarshal uses the inverse of the encodings that
  6. // Marshal uses, allocating maps, slices, and pointers as necessary,
  7. // with the following additional rules:
  8. //
  9. // To unmarshal JSON into a pointer, Unmarshal first handles the case of
  10. // the JSON being the JSON literal null. In that case, Unmarshal sets
  11. // the pointer to nil. Otherwise, Unmarshal unmarshals the JSON into
  12. // the value pointed at by the pointer. If the pointer is nil, Unmarshal
  13. // allocates a new value for it to point to.
  14. //
  15. // To unmarshal JSON into a value implementing the Unmarshaler interface,
  16. // Unmarshal calls that value's UnmarshalJSON method, including
  17. // when the input is a JSON null.
  18. // Otherwise, if the value implements encoding.TextUnmarshaler
  19. // and the input is a JSON quoted string, Unmarshal calls that value's
  20. // UnmarshalText method with the unquoted form of the string.
  21. //
  22. // To unmarshal JSON into a struct, Unmarshal matches incoming object
  23. // keys to the keys used by Marshal (either the struct field name or its tag),
  24. // preferring an exact match but also accepting a case-insensitive match. By
  25. // default, object keys which don't have a corresponding struct field are
  26. // ignored (see Decoder.DisallowUnknownFields for an alternative).
  27. //
  28. // To unmarshal JSON into an interface value,
  29. // Unmarshal stores one of these in the interface value:
  30. //
  31. //  bool, for JSON booleans
  32. //  float64, for JSON numbers
  33. //  string, for JSON strings
  34. //  []interface{}, for JSON arrays
  35. //  map[string]interface{}, for JSON objects
  36. //  nil for JSON null
  37. //
  38. // To unmarshal a JSON array into a slice, Unmarshal resets the slice length
  39. // to zero and then appends each element to the slice.
  40. // As a special case, to unmarshal an empty JSON array into a slice,
  41. // Unmarshal replaces the slice with a new empty slice.
  42. //
  43. // To unmarshal a JSON array into a Go array, Unmarshal decodes
  44. // JSON array elements into corresponding Go array elements.
  45. // If the Go array is smaller than the JSON array,
  46. // the additional JSON array elements are discarded.
  47. // If the JSON array is smaller than the Go array,
  48. // the additional Go array elements are set to zero values.
  49. //
  50. // To unmarshal a JSON object into a map, Unmarshal first establishes a map to
  51. // use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal
  52. // reuses the existing map, keeping existing entries. Unmarshal then stores
  53. // key-value pairs from the JSON object into the map. The map's key type must
  54. // either be a string, an integer, or implement encoding.TextUnmarshaler.
  55. //
  56. // If a JSON value is not appropriate for a given target type,
  57. // or if a JSON number overflows the target type, Unmarshal
  58. // skips that field and completes the unmarshaling as best it can.
  59. // If no more serious errors are encountered, Unmarshal returns
  60. // an UnmarshalTypeError describing the earliest such error. In any
  61. // case, it's not guaranteed that all the remaining fields following
  62. // the problematic one will be unmarshaled into the target object.
  63. //
  64. // The JSON null value unmarshals into an interface, map, pointer, or slice
  65. // by setting that Go value to nil. Because null is often used in JSON to mean
  66. // ``not present,'' unmarshaling a JSON null into any other Go type has no effect
  67. // on the value and produces no error.
  68. //
  69. // When unmarshaling quoted strings, invalid UTF-8 or
  70. // invalid UTF-16 surrogate pairs are not treated as an error.
  71. // Instead, they are replaced by the Unicode replacement
  72. // character U+FFFD.
  73. //
  74. func Unmarshal(data []byte, v interface{}) error {
  75.     // Check for well-formedness.
  76.     // Avoids filling out half a data structure
  77.     // before discovering a JSON syntax error.
  78.     var d decodeState
  79.     err := checkValid(data, &d.scan)
  80.     if err != nil {
  81.         return err
  82.     }
  83.  
  84.     d.init(data)
  85.     return d.unmarshal(v)
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement