github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/decode_test.go (about)

     1  // +build amd64,go1.16,!go1.23
     2  
     3  /*
     4   * Copyright 2021 ByteDance Inc.
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   *     http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package sonic
    20  
    21  import (
    22      `bytes`
    23      `encoding`
    24      `encoding/json`
    25      `errors`
    26      `fmt`
    27      `image`
    28      `math`
    29      `math/big`
    30      `math/rand`
    31      `net`
    32      `reflect`
    33      `strconv`
    34      `strings`
    35      `testing`
    36      `time`
    37      `unsafe`
    38  
    39      `github.com/bytedance/sonic/decoder`
    40      `github.com/bytedance/sonic/internal/native/types`
    41      `github.com/davecgh/go-spew/spew`
    42      `github.com/stretchr/testify/assert`
    43  )
    44  
    45  type T struct {
    46      X string
    47      Y int
    48      Z int `json:"-"`
    49  }
    50  
    51  type U struct {
    52      Alphabet string `json:"alpha"`
    53  }
    54  
    55  type V struct {
    56      F1 interface{}
    57      F2 int32
    58      F3 json.Number
    59      F4 *VOuter
    60  }
    61  
    62  type VOuter struct {
    63      V V
    64  }
    65  
    66  type W struct {
    67      S SS
    68  }
    69  
    70  type P struct {
    71      PP PP
    72  }
    73  
    74  type PP struct {
    75      T  T
    76      Ts []T
    77  }
    78  
    79  type SS string
    80  
    81  func (*SS) UnmarshalJSON(_ []byte) error {
    82      return &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(SS(""))}
    83  }
    84  
    85  // ifaceNumAsFloat64/ifaceNumAsNumber are used to test unmarshaling with and
    86  // without UseNumber
    87  var ifaceNumAsFloat64 = map[string]interface{}{
    88      "k1": float64(1),
    89      "k2": "s",
    90      "k3": []interface{}{float64(1), 2.0, 3e-3},
    91      "k4": map[string]interface{}{"kk1": "s", "kk2": float64(2)},
    92  }
    93  
    94  var ifaceNumAsNumber = map[string]interface{}{
    95      "k1": json.Number("1"),
    96      "k2": "s",
    97      "k3": []interface{}{json.Number("1"), json.Number("2.0"), json.Number("3e-3")},
    98      "k4": map[string]interface{}{"kk1": "s", "kk2": json.Number("2")},
    99  }
   100  
   101  type tx struct {
   102      x int
   103  }
   104  
   105  type u8 uint8
   106  
   107  // A type that can unmarshal itself.
   108  
   109  type unmarshaler struct {
   110      T bool
   111  }
   112  
   113  func (u *unmarshaler) UnmarshalJSON(_ []byte) error {
   114      *u = unmarshaler{true} // All we need to see that UnmarshalJSON is called.
   115      return nil
   116  }
   117  
   118  type ustruct struct {
   119      M unmarshaler
   120  }
   121  
   122  type unmarshalerText struct {
   123      A, B string
   124  }
   125  
   126  // needed for re-marshaling tests
   127  func (u unmarshalerText) MarshalText() ([]byte, error) {
   128      return []byte(u.A + ":" + u.B), nil
   129  }
   130  
   131  func (u *unmarshalerText) UnmarshalText(b []byte) error {
   132      pos := bytes.IndexByte(b, ':')
   133      if pos == -1 {
   134          return errors.New("missing separator")
   135      }
   136      u.A, u.B = string(b[:pos]), string(b[pos+1:])
   137      return nil
   138  }
   139  
   140  var _ encoding.TextUnmarshaler = (*unmarshalerText)(nil)
   141  
   142  type ustructText struct {
   143      M unmarshalerText
   144  }
   145  
   146  // u8marshal is an integer type that can marshal/unmarshal itself.
   147  type u8marshal uint8
   148  
   149  func (u8 u8marshal) MarshalText() ([]byte, error) {
   150      return []byte(fmt.Sprintf("u%d", u8)), nil
   151  }
   152  
   153  var errMissingU8Prefix = errors.New("missing 'u' prefix")
   154  
   155  func (u8 *u8marshal) UnmarshalText(b []byte) error {
   156      if !bytes.HasPrefix(b, []byte{'u'}) {
   157          return errMissingU8Prefix
   158      }
   159      n, err := strconv.Atoi(string(b[1:]))
   160      if err != nil {
   161          return err
   162      }
   163      *u8 = u8marshal(n)
   164      return nil
   165  }
   166  
   167  var _ encoding.TextUnmarshaler = (*u8marshal)(nil)
   168  
   169  var (
   170      umtrue   = unmarshaler{true}
   171      umslice  = []unmarshaler{{true}}
   172      umstruct = ustruct{unmarshaler{true}}
   173  
   174      umtrueXY   = unmarshalerText{"x", "y"}
   175      umsliceXY  = []unmarshalerText{{"x", "y"}}
   176      umstructXY = ustructText{unmarshalerText{"x", "y"}}
   177  
   178      ummapXY = map[unmarshalerText]bool{{"x", "y"}: true}
   179  )
   180  
   181  // Test data structures for anonymous fields.
   182  
   183  type Point struct {
   184      Z int
   185  }
   186  
   187  type Top struct {
   188      Level0 int
   189      Embed0
   190      *Embed0a
   191      *Embed0b `json:"e,omitempty"` // treated as named
   192      Embed0c  `json:"-"`           // ignored
   193      Loop
   194      Embed0p // has Point with X, Y, used
   195      Embed0q // has Point with Z, used
   196      embed   // contains exported field
   197  }
   198  
   199  type Embed0 struct {
   200      Level1a int // overridden by Embed0a's Level1a with json tag
   201      Level1b int // used because Embed0a's Level1b is renamed
   202      Level1c int // used because Embed0a's Level1c is ignored
   203      Level1d int // annihilated by Embed0a's Level1d
   204      Level1e int `json:"x"` // annihilated by Embed0a.Level1e
   205  }
   206  
   207  type Embed0a struct {
   208      Level1a int `json:"Level1a,omitempty"`
   209      Level1b int `json:"LEVEL1B,omitempty"`
   210      Level1c int `json:"-"`
   211      Level1d int // annihilated by Embed0's Level1d
   212      Level1f int `json:"x"` // annihilated by Embed0's Level1e
   213  }
   214  
   215  type Embed0b Embed0
   216  
   217  type Embed0c Embed0
   218  
   219  type Embed0p struct {
   220      image.Point
   221  }
   222  
   223  type Embed0q struct {
   224      Point
   225  }
   226  
   227  type embed struct {
   228      Q int
   229  }
   230  
   231  type Loop struct {
   232      Loop1 int `json:",omitempty"`
   233      Loop2 int `json:",omitempty"`
   234      *Loop
   235  }
   236  
   237  // From reflect test:
   238  // The X in S6 and S7 annihilate, but they also block the X in S8.S9.
   239  type S5 struct {
   240      S6
   241      S7
   242      S8
   243  }
   244  
   245  type S6 struct {
   246      X int
   247  }
   248  
   249  type S7 S6
   250  
   251  type S8 struct {
   252      S9
   253  }
   254  
   255  type S9 struct {
   256      X int
   257      Y int
   258  }
   259  
   260  // From reflect test:
   261  // The X in S11.S6 and S12.S6 annihilate, but they also block the X in S13.S8.S9.
   262  type S10 struct {
   263      S11
   264      S12
   265      S13
   266  }
   267  
   268  type S11 struct {
   269      S6
   270  }
   271  
   272  type S12 struct {
   273      S6
   274  }
   275  
   276  type S13 struct {
   277      S8
   278  }
   279  
   280  type Ambig struct {
   281      // Given "hello", the first match should win.
   282      First  int `json:"HELLO"`
   283      Second int `json:"Hello"`
   284  }
   285  
   286  type XYZ struct {
   287      X interface{}
   288      Y interface{}
   289      Z interface{}
   290  }
   291  
   292  type byteWithMarshalJSON byte
   293  
   294  func (b byteWithMarshalJSON) MarshalJSON() ([]byte, error) {
   295      return []byte(fmt.Sprintf(`"Z%.2x"`, byte(b))), nil
   296  }
   297  
   298  func (b *byteWithMarshalJSON) UnmarshalJSON(data []byte) error {
   299      if len(data) != 5 || data[0] != '"' || data[1] != 'Z' || data[4] != '"' {
   300          return fmt.Errorf("bad quoted string")
   301      }
   302      i, err := strconv.ParseInt(string(data[2:4]), 16, 8)
   303      if err != nil {
   304          return fmt.Errorf("bad hex")
   305      }
   306      *b = byteWithMarshalJSON(i)
   307      return nil
   308  }
   309  
   310  type byteWithPtrMarshalJSON byte
   311  
   312  func (b *byteWithPtrMarshalJSON) MarshalJSON() ([]byte, error) {
   313      return byteWithMarshalJSON(*b).MarshalJSON()
   314  }
   315  
   316  func (b *byteWithPtrMarshalJSON) UnmarshalJSON(data []byte) error {
   317      return (*byteWithMarshalJSON)(b).UnmarshalJSON(data)
   318  }
   319  
   320  type byteWithMarshalText byte
   321  
   322  func (b byteWithMarshalText) MarshalText() ([]byte, error) {
   323      return []byte(fmt.Sprintf(`Z%.2x`, byte(b))), nil
   324  }
   325  
   326  func (b *byteWithMarshalText) UnmarshalText(data []byte) error {
   327      if len(data) != 3 || data[0] != 'Z' {
   328          return fmt.Errorf("bad quoted string")
   329      }
   330      i, err := strconv.ParseInt(string(data[1:3]), 16, 8)
   331      if err != nil {
   332          return fmt.Errorf("bad hex")
   333      }
   334      *b = byteWithMarshalText(i)
   335      return nil
   336  }
   337  
   338  type byteWithPtrMarshalText byte
   339  
   340  func (b *byteWithPtrMarshalText) MarshalText() ([]byte, error) {
   341      return byteWithMarshalText(*b).MarshalText()
   342  }
   343  
   344  func (b *byteWithPtrMarshalText) UnmarshalText(data []byte) error {
   345      return (*byteWithMarshalText)(b).UnmarshalText(data)
   346  }
   347  
   348  type intWithMarshalJSON int
   349  
   350  func (b intWithMarshalJSON) MarshalJSON() ([]byte, error) {
   351      return []byte(fmt.Sprintf(`"Z%.2x"`, int(b))), nil
   352  }
   353  
   354  func (b *intWithMarshalJSON) UnmarshalJSON(data []byte) error {
   355      if len(data) != 5 || data[0] != '"' || data[1] != 'Z' || data[4] != '"' {
   356          return fmt.Errorf("bad quoted string")
   357      }
   358      i, err := strconv.ParseInt(string(data[2:4]), 16, 8)
   359      if err != nil {
   360          return fmt.Errorf("bad hex")
   361      }
   362      *b = intWithMarshalJSON(i)
   363      return nil
   364  }
   365  
   366  type intWithPtrMarshalJSON int
   367  
   368  func (b *intWithPtrMarshalJSON) MarshalJSON() ([]byte, error) {
   369      return intWithMarshalJSON(*b).MarshalJSON()
   370  }
   371  
   372  func (b *intWithPtrMarshalJSON) UnmarshalJSON(data []byte) error {
   373      return (*intWithMarshalJSON)(b).UnmarshalJSON(data)
   374  }
   375  
   376  type intWithMarshalText int
   377  
   378  func (b intWithMarshalText) MarshalText() ([]byte, error) {
   379      return []byte(fmt.Sprintf(`Z%.2x`, int(b))), nil
   380  }
   381  
   382  func (b *intWithMarshalText) UnmarshalText(data []byte) error {
   383      if len(data) != 3 || data[0] != 'Z' {
   384          return fmt.Errorf("bad quoted string")
   385      }
   386      i, err := strconv.ParseInt(string(data[1:3]), 16, 8)
   387      if err != nil {
   388          return fmt.Errorf("bad hex")
   389      }
   390      *b = intWithMarshalText(i)
   391      return nil
   392  }
   393  
   394  type intWithPtrMarshalText int
   395  
   396  func (b *intWithPtrMarshalText) MarshalText() ([]byte, error) {
   397      return intWithMarshalText(*b).MarshalText()
   398  }
   399  
   400  func (b *intWithPtrMarshalText) UnmarshalText(data []byte) error {
   401      return (*intWithMarshalText)(b).UnmarshalText(data)
   402  }
   403  
   404  type mapStringToStringData struct {
   405      Data map[string]string `json:"data"`
   406  }
   407  
   408  type unmarshalTest struct {
   409      in                    string
   410      ptr                   interface{} // new(type)
   411      out                   interface{}
   412      err                   error
   413      useNumber             bool
   414      golden                bool
   415      disallowUnknownFields bool
   416      validateString           bool
   417  }
   418  
   419  type B struct {
   420      B bool `json:",string"`
   421  }
   422  
   423  type DoublePtr struct {
   424      I **int
   425      J **int
   426  }
   427  
   428  type JsonSyntaxError struct {
   429      Msg    string
   430      Offset int64
   431  }
   432  
   433  func (self *JsonSyntaxError) err() *json.SyntaxError {
   434      return (*json.SyntaxError)(unsafe.Pointer(self))
   435  }
   436  
   437  var unmarshalTests = []unmarshalTest{
   438      // basic types
   439      {in: `true`, ptr: new(bool), out: true},
   440      {in: `1`, ptr: new(int), out: 1},
   441      {in: `1.2`, ptr: new(float64), out: 1.2},
   442      {in: `-5`, ptr: new(int16), out: int16(-5)},
   443      {in: `2`, ptr: new(json.Number), out: json.Number("2"), useNumber: true},
   444      {in: `2`, ptr: new(json.Number), out: json.Number("2")},
   445      {in: `2`, ptr: new(interface{}), out: 2.0},
   446      {in: `2`, ptr: new(interface{}), out: json.Number("2"), useNumber: true},
   447      {in: `"a\u1234"`, ptr: new(string), out: "a\u1234"},
   448      {in: `"http:\/\/"`, ptr: new(string), out: "http://"},
   449      {in: `"g-clef: \uD834\uDD1E"`, ptr: new(string), out: "g-clef: \U0001D11E"},
   450      {in: `"invalid: \uD834x\uDD1E"`, ptr: new(string), out: "invalid: \uFFFDx\uFFFD"},
   451      {in: "null", ptr: new(interface{}), out: nil},
   452      {in: `{"X": [1,2,3], "Y": 4}`, ptr: new(T), out: T{Y: 4}, err: &json.UnmarshalTypeError{Value: "array", Type: reflect.TypeOf(""), Offset: 7, Struct: "T", Field: "X"}},
   453      {in: `{"X": 23}`, ptr: new(T), out: T{}, err: &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 8, Struct: "T", Field: "X"}},
   454      {in: `{"x": 1}`, ptr: new(tx), out: tx{}},
   455      {in: `{"x": 1}`, ptr: new(tx), err: fmt.Errorf("json: unknown field \"x\""), disallowUnknownFields: true},
   456      {in: `{"S": 23}`, ptr: new(W), out: W{}, err: &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(SS("")), Struct: "W", Field: "S"}},
   457      {in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: float64(1), F2: int32(2), F3: json.Number("3")}},
   458      {in: `{"F1":1,"F2":2,"F3":3}`, ptr: new(V), out: V{F1: json.Number("1"), F2: int32(2), F3: json.Number("3")}, useNumber: true},
   459      {in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsFloat64},
   460      {in: `{"k1":1,"k2":"s","k3":[1,2.0,3e-3],"k4":{"kk1":"s","kk2":2}}`, ptr: new(interface{}), out: ifaceNumAsNumber, useNumber: true},
   461  
   462      // raw values with whitespace
   463      {in: "\n true ", ptr: new(bool), out: true},
   464      {in: "\t 1 ", ptr: new(int), out: 1},
   465      {in: "\r 1.2 ", ptr: new(float64), out: 1.2},
   466      {in: "\t -5 \n", ptr: new(int16), out: int16(-5)},
   467      {in: "\t \"a\\u1234\" \n", ptr: new(string), out: "a\u1234"},
   468  
   469      // Z has a "-" tag.
   470      {in: `{"Y": 1, "Z": 2}`, ptr: new(T), out: T{Y: 1}},
   471      {in: `{"Y": 1, "Z": 2}`, ptr: new(T), err: fmt.Errorf("json: unknown field \"Z\""), disallowUnknownFields: true},
   472  
   473      {in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), out: U{Alphabet: "abc"}},
   474      {in: `{"alpha": "abc", "alphabet": "xyz"}`, ptr: new(U), err: fmt.Errorf("json: unknown field \"alphabet\""), disallowUnknownFields: true},
   475      {in: `{"alpha": "abc"}`, ptr: new(U), out: U{Alphabet: "abc"}},
   476      {in: `{"alphabet": "xyz"}`, ptr: new(U), out: U{}},
   477      {in: `{"alphabet": "xyz"}`, ptr: new(U), err: fmt.Errorf("json: unknown field \"alphabet\""), disallowUnknownFields: true},
   478  
   479      // syntax errors
   480      {in: `{"X": "foo", "Y"}`, err: (&JsonSyntaxError{"invalid character '}' after object key", 17}).err()},
   481      {in: `[1, 2, 3+]`, err: (&JsonSyntaxError{"invalid character '+' after array element", 9}).err()},
   482      {in: `{"X":12x}`, err: (&JsonSyntaxError{"invalid character 'x' after object key:value pair", 8}).err(), useNumber: true},
   483      {in: `[2, 3`, err: (&JsonSyntaxError{Msg:  "unexpected end of JSON input", Offset: 5}).err()},
   484      {in: `{"F3": -}`, ptr: new(V), out: V{F3: json.Number("-")}, err: (&JsonSyntaxError{Msg:  "invalid character '}' in numeric literal", Offset: 9}).err()},
   485  
   486      // raw value errors
   487      {in: "\x01 42", err: (&JsonSyntaxError{"invalid character '\\x01' looking for beginning of value", 1}).err()},
   488      {in: " 42 \x01", err: (&JsonSyntaxError{"invalid character '\\x01' after top-level value", 5}).err()},
   489      {in: "\x01 true", err: (&JsonSyntaxError{"invalid character '\\x01' looking for beginning of value", 1}).err()},
   490      {in: " false \x01", err: (&JsonSyntaxError{"invalid character '\\x01' after top-level value", 8}).err()},
   491      {in: "\x01 1.2", err: (&JsonSyntaxError{"invalid character '\\x01' looking for beginning of value", 1}).err()},
   492      {in: " 3.4 \x01", err: (&JsonSyntaxError{"invalid character '\\x01' after top-level value", 6}).err()},
   493      {in: "\x01 \"string\"", err: (&JsonSyntaxError{"invalid character '\\x01' looking for beginning of value", 1}).err()},
   494      {in: " \"string\" \x01", err: (&JsonSyntaxError{"invalid character '\\x01' after top-level value", 11}).err()},
   495  
   496      // array tests
   497      {in: `[1, 2, 3]`, ptr: new([3]int), out: [3]int{1, 2, 3}},
   498      {in: `[1, 2, 3]`, ptr: new([1]int), out: [1]int{1}},
   499      {in: `[1, 2, 3]`, ptr: new([5]int), out: [5]int{1, 2, 3, 0, 0}},
   500      {in: `[1, 2, 3]`, ptr: new(MustNotUnmarshalJSON), err: errors.New("MustNotUnmarshalJSON was used")},
   501  
   502      // empty array to interface test
   503      {in: `[]`, ptr: new([]interface{}), out: []interface{}{}},
   504      {in: `null`, ptr: new([]interface{}), out: []interface{}(nil)},
   505      {in: `{"T":[]}`, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": []interface{}{}}},
   506      {in: `{"T":null}`, ptr: new(map[string]interface{}), out: map[string]interface{}{"T": interface{}(nil)}},
   507  
   508      // composite tests
   509      {in: allValueIndent, ptr: new(All), out: allValue},
   510      {in: allValueCompact, ptr: new(All), out: allValue},
   511      {in: allValueIndent, ptr: new(*All), out: &allValue},
   512      {in: allValueCompact, ptr: new(*All), out: &allValue},
   513      {in: pallValueIndent, ptr: new(All), out: pallValue},
   514      {in: pallValueCompact, ptr: new(All), out: pallValue},
   515      {in: pallValueIndent, ptr: new(*All), out: &pallValue},
   516      {in: pallValueCompact, ptr: new(*All), out: &pallValue},
   517  
   518      // unmarshal interface test
   519      {in: `{"T":false}`, ptr: new(unmarshaler), out: umtrue}, // use "false" so test will fail if custom unmarshaler is not called
   520      {in: `{"T":false}`, ptr: new(*unmarshaler), out: &umtrue},
   521      {in: `[{"T":false}]`, ptr: new([]unmarshaler), out: umslice},
   522      {in: `[{"T":false}]`, ptr: new(*[]unmarshaler), out: &umslice},
   523      {in: `{"M":{"T":"x:y"}}`, ptr: new(ustruct), out: umstruct},
   524  
   525      // UnmarshalText interface test
   526      {in: `"x:y"`, ptr: new(unmarshalerText), out: umtrueXY},
   527      {in: `"x:y"`, ptr: new(*unmarshalerText), out: &umtrueXY},
   528      {in: `["x:y"]`, ptr: new([]unmarshalerText), out: umsliceXY},
   529      {in: `["x:y"]`, ptr: new(*[]unmarshalerText), out: &umsliceXY},
   530      {in: `{"M":"x:y"}`, ptr: new(ustructText), out: umstructXY},
   531  
   532      // integer-keyed map test
   533      {
   534          in:  `{"-1":"a","0":"b","1":"c"}`,
   535          ptr: new(map[int]string),
   536          out: map[int]string{-1: "a", 0: "b", 1: "c"},
   537      },
   538      {
   539          in:  `{"0":"a","10":"c","9":"b"}`,
   540          ptr: new(map[u8]string),
   541          out: map[u8]string{0: "a", 9: "b", 10: "c"},
   542      },
   543      {
   544          in:  `{"-9223372036854775808":"min","9223372036854775807":"max"}`,
   545          ptr: new(map[int64]string),
   546          out: map[int64]string{math.MinInt64: "min", math.MaxInt64: "max"},
   547      },
   548      {
   549          in:  `{"18446744073709551615":"max"}`,
   550          ptr: new(map[uint64]string),
   551          out: map[uint64]string{math.MaxUint64: "max"},
   552      },
   553      {
   554          in:  `{"0":false,"10":true}`,
   555          ptr: new(map[uintptr]bool),
   556          out: map[uintptr]bool{0: false, 10: true},
   557      },
   558  
   559      // Check that MarshalText and UnmarshalText take precedence
   560      // over default integer handling in map keys.
   561      {
   562          in:  `{"u2":4}`,
   563          ptr: new(map[u8marshal]int),
   564          out: map[u8marshal]int{2: 4},
   565      },
   566      {
   567          in:  `{"2":4}`,
   568          ptr: new(map[u8marshal]int),
   569          err: errMissingU8Prefix,
   570      },
   571  
   572      // integer-keyed map errors
   573      {
   574          in:  `{"abc":"abc"}`,
   575          ptr: new(map[int]string),
   576          err: &json.UnmarshalTypeError{Value: "number abc", Type: reflect.TypeOf(0), Offset: 2},
   577      },
   578      {
   579          in:  `{"256":"abc"}`,
   580          ptr: new(map[uint8]string),
   581          err: &json.UnmarshalTypeError{Value: "number 256", Type: reflect.TypeOf(uint8(0)), Offset: 2},
   582      },
   583      {
   584          in:  `{"128":"abc"}`,
   585          ptr: new(map[int8]string),
   586          err: &json.UnmarshalTypeError{Value: "number 128", Type: reflect.TypeOf(int8(0)), Offset: 2},
   587      },
   588      {
   589          in:  `{"-1":"abc"}`,
   590          ptr: new(map[uint8]string),
   591          err: &json.UnmarshalTypeError{Value: "number -1", Type: reflect.TypeOf(uint8(0)), Offset: 2},
   592      },
   593      {
   594          in:  `{"F":{"a":2,"3":4}}`,
   595          ptr: new(map[string]map[int]int),
   596          err: &json.UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(0), Offset: 7},
   597      },
   598      {
   599          in:  `{"F":{"a":2,"3":4}}`,
   600          ptr: new(map[string]map[uint]int),
   601          err: &json.UnmarshalTypeError{Value: "number a", Type: reflect.TypeOf(uint(0)), Offset: 7},
   602      },
   603  
   604      // Map keys can be encoding.TextUnmarshalers.
   605      {in: `{"x:y":true}`, ptr: new(map[unmarshalerText]bool), out: ummapXY},
   606      // If multiple values for the same key exists, only the most recent value is used.
   607      {in: `{"x:y":false,"x:y":true}`, ptr: new(map[unmarshalerText]bool), out: ummapXY},
   608  
   609      {
   610          in: `{
   611              "Level0": 1,
   612              "Level1b": 2,
   613              "Level1c": 3,
   614              "x": 4,
   615              "Level1a": 5,
   616              "LEVEL1B": 6,
   617              "e": {
   618                  "Level1a": 8,
   619                  "Level1b": 9,
   620                  "Level1c": 10,
   621                  "Level1d": 11,
   622                  "x": 12
   623              },
   624              "Loop1": 13,
   625              "Loop2": 14,
   626              "X": 15,
   627              "Y": 16,
   628              "Z": 17,
   629              "Q": 18
   630          }`,
   631          ptr: new(Top),
   632          out: Top{
   633              Level0: 1,
   634              Embed0: Embed0{
   635                  Level1b: 2,
   636                  Level1c: 3,
   637              },
   638              Embed0a: &Embed0a{
   639                  Level1a: 5,
   640                  Level1b: 6,
   641              },
   642              Embed0b: &Embed0b{
   643                  Level1a: 8,
   644                  Level1b: 9,
   645                  Level1c: 10,
   646                  Level1d: 11,
   647                  Level1e: 12,
   648              },
   649              Loop: Loop{
   650                  Loop1: 13,
   651                  Loop2: 14,
   652              },
   653              Embed0p: Embed0p{
   654                  Point: image.Point{X: 15, Y: 16},
   655              },
   656              Embed0q: Embed0q{
   657                  Point: Point{Z: 17},
   658              },
   659              embed: embed{
   660                  Q: 18,
   661              },
   662          },
   663      },
   664      {
   665          in:  `{"hello": 1}`,
   666          ptr: new(Ambig),
   667          out: Ambig{First: 1},
   668      },
   669  
   670      {
   671          in:  `{"X": 1,"Y":2}`,
   672          ptr: new(S5),
   673          out: S5{S8: S8{S9: S9{Y: 2}}},
   674      },
   675      {
   676          in:                    `{"X": 1,"Y":2}`,
   677          ptr:                   new(S5),
   678          err:                   fmt.Errorf("json: unknown field \"X\""),
   679          disallowUnknownFields: true,
   680      },
   681      {
   682          in:  `{"X": 1,"Y":2}`,
   683          ptr: new(S10),
   684          out: S10{S13: S13{S8: S8{S9: S9{Y: 2}}}},
   685      },
   686      {
   687          in:                    `{"X": 1,"Y":2}`,
   688          ptr:                   new(S10),
   689          err:                   fmt.Errorf("json: unknown field \"X\""),
   690          disallowUnknownFields: true,
   691      },
   692      {
   693          in:  `{"I": 0, "I": null, "J": null}`,
   694          ptr: new(DoublePtr),
   695          out: DoublePtr{I: nil, J: nil},
   696      },
   697  
   698      // invalid UTF-8 is coerced to valid UTF-8.
   699      {
   700          in:  "\"hello\xffworld\"",
   701          ptr: new(string),
   702          out: "hello\xffworld",
   703          validateString: false,
   704      },
   705      {
   706          in:  "\"hello\xc2\xc2world\"",
   707          ptr: new(string),
   708          out: "hello\xc2\xc2world",
   709          validateString: false,
   710      },
   711      {
   712          in:  "\"hello\xc2\xffworld\"",
   713          ptr: new(string),
   714          out: "hello\xc2\xffworld",
   715      },
   716      {
   717          in:  "\"hello\\ud800world\"",
   718          ptr: new(string),
   719          out: "hello\ufffdworld",
   720      },
   721      {
   722          in:  "\"hello\\ud800\\ud800world\"",
   723          ptr: new(string),
   724          out: "hello\ufffd\ufffdworld",
   725      },
   726      {
   727          in:  "\"hello\xed\xa0\x80\xed\xb0\x80world\"",
   728          ptr: new(string),
   729          out: "hello\xed\xa0\x80\xed\xb0\x80world",
   730      },
   731  
   732      // Used to be issue 8305, but time.Time implements encoding.TextUnmarshaler so this works now.
   733      {
   734          in:  `{"2009-11-10T23:00:00Z": "hello world"}`,
   735          ptr: new(map[time.Time]string),
   736          out: map[time.Time]string{time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC): "hello world"},
   737      },
   738  
   739      // issue 8305
   740      {
   741          in:  `{"2009-11-10T23:00:00Z": "hello world"}`,
   742          ptr: new(map[Point]string),
   743          err: &json.UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(map[Point]string{}), Offset: 1},
   744      },
   745      {
   746          in:  `{"asdf": "hello world"}`,
   747          ptr: new(map[unmarshaler]string),
   748          err: &json.UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(map[unmarshaler]string{}), Offset: 1},
   749      },
   750  
   751      // related to issue 13783.
   752      // Go 1.7 changed marshaling a slice of typed byte to use the methods on the byte type,
   753      // similar to marshaling a slice of typed int.
   754      // These tests check that, assuming the byte type also has valid decoding methods,
   755      // either the old base64 string encoding or the new per-element encoding can be
   756      // successfully unmarshaled. The custom unmarshalers were accessible in earlier
   757      // versions of Go, even though the custom marshaler was not.
   758      {
   759          in:  `"AQID"`,
   760          ptr: new([]byteWithMarshalJSON),
   761          out: []byteWithMarshalJSON{1, 2, 3},
   762      },
   763      {
   764          in:     `["Z01","Z02","Z03"]`,
   765          ptr:    new([]byteWithMarshalJSON),
   766          out:    []byteWithMarshalJSON{1, 2, 3},
   767          golden: true,
   768      },
   769      {
   770          in:  `"AQID"`,
   771          ptr: new([]byteWithMarshalText),
   772          out: []byteWithMarshalText{1, 2, 3},
   773      },
   774      {
   775          in:     `["Z01","Z02","Z03"]`,
   776          ptr:    new([]byteWithMarshalText),
   777          out:    []byteWithMarshalText{1, 2, 3},
   778          golden: true,
   779      },
   780      {
   781          in:  `"AQID"`,
   782          ptr: new([]byteWithPtrMarshalJSON),
   783          out: []byteWithPtrMarshalJSON{1, 2, 3},
   784      },
   785      {
   786          in:     `["Z01","Z02","Z03"]`,
   787          ptr:    new([]byteWithPtrMarshalJSON),
   788          out:    []byteWithPtrMarshalJSON{1, 2, 3},
   789          golden: true,
   790      },
   791      {
   792          in:  `"AQID"`,
   793          ptr: new([]byteWithPtrMarshalText),
   794          out: []byteWithPtrMarshalText{1, 2, 3},
   795      },
   796      {
   797          in:     `["Z01","Z02","Z03"]`,
   798          ptr:    new([]byteWithPtrMarshalText),
   799          out:    []byteWithPtrMarshalText{1, 2, 3},
   800          golden: true,
   801      },
   802  
   803      // ints work with the marshaler but not the base64 []byte case
   804      {
   805          in:     `["Z01","Z02","Z03"]`,
   806          ptr:    new([]intWithMarshalJSON),
   807          out:    []intWithMarshalJSON{1, 2, 3},
   808          golden: true,
   809      },
   810      {
   811          in:     `["Z01","Z02","Z03"]`,
   812          ptr:    new([]intWithMarshalText),
   813          out:    []intWithMarshalText{1, 2, 3},
   814          golden: true,
   815      },
   816      {
   817          in:     `["Z01","Z02","Z03"]`,
   818          ptr:    new([]intWithPtrMarshalJSON),
   819          out:    []intWithPtrMarshalJSON{1, 2, 3},
   820          golden: true,
   821      },
   822      {
   823          in:     `["Z01","Z02","Z03"]`,
   824          ptr:    new([]intWithPtrMarshalText),
   825          out:    []intWithPtrMarshalText{1, 2, 3},
   826          golden: true,
   827      },
   828  
   829      {in: `0.000001`, ptr: new(float64), out: 0.000001, golden: true},
   830      {in: `1e-7`, ptr: new(float64), out: 1e-7, golden: true},
   831      {in: `100000000000000000000`, ptr: new(float64), out: 100000000000000000000.0, golden: true},
   832      {in: `1e+21`, ptr: new(float64), out: 1e21, golden: true},
   833      {in: `-0.000001`, ptr: new(float64), out: -0.000001, golden: true},
   834      {in: `-1e-7`, ptr: new(float64), out: -1e-7, golden: true},
   835      {in: `-100000000000000000000`, ptr: new(float64), out: -100000000000000000000.0, golden: true},
   836      {in: `-1e+21`, ptr: new(float64), out: -1e21, golden: true},
   837      {in: `999999999999999900000`, ptr: new(float64), out: 999999999999999900000.0, golden: true},
   838      {in: `9007199254740992`, ptr: new(float64), out: 9007199254740992.0, golden: true},
   839      {in: `9007199254740993`, ptr: new(float64), out: 9007199254740992.0, golden: false},
   840  
   841      {
   842          in:  `{"V": {"F2": "hello"}}`,
   843          ptr: new(VOuter),
   844          err: &json.UnmarshalTypeError{
   845              Value:  "string",
   846              Struct: "V",
   847              Field:  "V.F2",
   848              Type:   reflect.TypeOf(int32(0)),
   849              Offset: 20,
   850          },
   851      },
   852      {
   853          in:  `{"V": {"F4": {}, "F2": "hello"}}`,
   854          ptr: new(VOuter),
   855          err: &json.UnmarshalTypeError{
   856              Value:  "string",
   857              Struct: "V",
   858              Field:  "V.F2",
   859              Type:   reflect.TypeOf(int32(0)),
   860              Offset: 30,
   861          },
   862      },
   863  
   864      // issue 15146.
   865      // invalid inputs in wrongStringTests below.
   866      {in: `{"B":"true"}`, ptr: new(B), out: B{true}, golden: true},
   867      {in: `{"B":"false"}`, ptr: new(B), out: B{false}, golden: true},
   868      {in: `{"B": "maybe"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "maybe" into bool`)},
   869      {in: `{"B": "tru"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "tru" into bool`)},
   870      {in: `{"B": "False"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "False" into bool`)},
   871      {in: `{"B": "null"}`, ptr: new(B), out: B{false}},
   872      {in: `{"B": "nul"}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal "nul" into bool`)},
   873      {in: `{"B": [2, 3]}`, ptr: new(B), err: errors.New(`json: invalid use of ,string struct tag, trying to unmarshal unquoted value into bool`)},
   874  
   875      // additional tests for disallowUnknownFields
   876      {
   877          in: `{
   878              "Level0": 1,
   879              "Level1b": 2,
   880              "Level1c": 3,
   881              "x": 4,
   882              "Level1a": 5,
   883              "LEVEL1B": 6,
   884              "e": {
   885                  "Level1a": 8,
   886                  "Level1b": 9,
   887                  "Level1c": 10,
   888                  "Level1d": 11,
   889                  "x": 12
   890              },
   891              "Loop1": 13,
   892              "Loop2": 14,
   893              "X": 15,
   894              "Y": 16,
   895              "Z": 17,
   896              "Q": 18,
   897              "extra": true
   898          }`,
   899          ptr:                   new(Top),
   900          err:                   fmt.Errorf("json: unknown field \"extra\""),
   901          disallowUnknownFields: true,
   902      },
   903      {
   904          in: `{
   905              "Level0": 1,
   906              "Level1b": 2,
   907              "Level1c": 3,
   908              "x": 4,
   909              "Level1a": 5,
   910              "LEVEL1B": 6,
   911              "e": {
   912                  "Level1a": 8,
   913                  "Level1b": 9,
   914                  "Level1c": 10,
   915                  "Level1d": 11,
   916                  "x": 12,
   917                  "extra": null
   918              },
   919              "Loop1": 13,
   920              "Loop2": 14,
   921              "X": 15,
   922              "Y": 16,
   923              "Z": 17,
   924              "Q": 18
   925          }`,
   926          ptr:                   new(Top),
   927          err:                   fmt.Errorf("json: unknown field \"extra\""),
   928          disallowUnknownFields: true,
   929      },
   930      // issue 26444
   931      // json.UnmarshalTypeError without field & struct values
   932      {
   933          in:  `{"data":{"test1": "bob", "test2": 123}}`,
   934          ptr: new(mapStringToStringData),
   935          err: &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 37, Struct: "mapStringToStringData", Field: "data"},
   936      },
   937      {
   938          in:  `{"data":{"test1": 123, "test2": "bob"}}`,
   939          ptr: new(mapStringToStringData),
   940          err: &json.UnmarshalTypeError{Value: "number", Type: reflect.TypeOf(""), Offset: 21, Struct: "mapStringToStringData", Field: "data"},
   941      },
   942  
   943      // trying to decode JSON arrays or objects via TextUnmarshaler
   944      {
   945          in:  `[1, 2, 3]`,
   946          ptr: new(MustNotUnmarshalText),
   947          err: &json.UnmarshalTypeError{Value: "array", Type: reflect.TypeOf(&MustNotUnmarshalText{}), Offset: 1},
   948      },
   949      {
   950          in:  `{"foo": "bar"}`,
   951          ptr: new(MustNotUnmarshalText),
   952          err: &json.UnmarshalTypeError{Value: "object", Type: reflect.TypeOf(&MustNotUnmarshalText{}), Offset: 1},
   953      },
   954      // #22369
   955      {
   956          in:  `{"PP": {"T": {"Y": "bad-type"}}}`,
   957          ptr: new(P),
   958          err: &json.UnmarshalTypeError{
   959              Value:  "string",
   960              Struct: "T",
   961              Field:  "PP.T.Y",
   962              Type:   reflect.TypeOf(0),
   963              Offset: 29,
   964          },
   965      },
   966      {
   967          in:  `{"Ts": [{"Y": 1}, {"Y": 2}, {"Y": "bad-type"}]}`,
   968          ptr: new(PP),
   969          err: &json.UnmarshalTypeError{
   970              Value:  "string",
   971              Struct: "T",
   972              Field:  "Ts.Y",
   973              Type:   reflect.TypeOf(0),
   974              Offset: 29,
   975          },
   976      },
   977      // #14702
   978      {
   979          in:  `invalid`,
   980          ptr: new(json.Number),
   981          err: (&JsonSyntaxError{
   982              Msg:    "invalid character 'i' looking for beginning of value",
   983              Offset: 1,
   984          }).err(),
   985      },
   986      {
   987          in:  `"invalid"`,
   988          ptr: new(json.Number),
   989          err: fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", `"invalid"`),
   990      },
   991      {
   992          in:  `{"A":"invalid"}`,
   993          ptr: new(struct{ A json.Number }),
   994          err: fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", `"invalid"`),
   995      },
   996      {
   997          in: `{"A":"invalid"}`,
   998          ptr: new(struct {
   999              A json.Number `json:",string"`
  1000          }),
  1001          err: fmt.Errorf("json: invalid use of ,string struct tag, trying to unmarshal %q into json.Number", `invalid`),
  1002      },
  1003      {
  1004          in:  `{"A":"invalid"}`,
  1005          ptr: new(map[string]json.Number),
  1006          err: fmt.Errorf("json: invalid number literal, trying to unmarshal %q into Number", `"invalid"`),
  1007      },
  1008      {in: `\u`, ptr: new(interface{}), err: fmt.Errorf("json: invald char"), validateString: true},
  1009      {in: `\u`, ptr: new(string), err: fmt.Errorf("json: invald char"), validateString: true},
  1010  
  1011      {in: "\"\x00\"", ptr: new(interface{}), err: fmt.Errorf("json: invald char"), validateString: true},
  1012      {in: "\"\x00\"", ptr: new(string), err: fmt.Errorf("json: invald char"), validateString: true},
  1013      {in: "\"\xff\"", ptr: new(interface{}), out: interface{}("\ufffd"), validateString: true},
  1014      {in: "\"\xff\"", ptr: new(string), out: "\ufffd", validateString: true},
  1015      {in: "\"\x00\"", ptr: new(interface{}), out: interface{}("\x00"), validateString: false},
  1016      {in: "\"\x00\"", ptr: new(string), out: "\x00", validateString: false},
  1017      {in: "\"\xff\"", ptr: new(interface{}), out: interface{}("\xff"), validateString: false},
  1018      {in: "\"\xff\"", ptr: new(string), out: "\xff", validateString: false},
  1019  }
  1020  
  1021  func trim(b []byte) []byte {
  1022      if len(b) > 20 {
  1023          return b[0:20]
  1024      }
  1025      return b
  1026  }
  1027  
  1028  func diff(t *testing.T, a, b []byte) {
  1029      for i := 0; ; i++ {
  1030          if i >= len(a) || i >= len(b) || a[i] != b[i] {
  1031              j := i - 10
  1032              if j < 0 {
  1033                  j = 0
  1034              }
  1035              t.Errorf("diverge at %d: «%s» vs «%s»", i, trim(a[j:]), trim(b[j:]))
  1036              return
  1037          }
  1038      }
  1039  }
  1040  
  1041  func TestMarshal(t *testing.T) {
  1042      b, err := Marshal(allValue)
  1043      if err != nil {
  1044          t.Fatalf("Marshal allValue: %v", err)
  1045      }
  1046      if string(b) != allValueCompact {
  1047          t.Errorf("Marshal allValueCompact")
  1048          diff(t, b, []byte(allValueCompact))
  1049          return
  1050      }
  1051  
  1052      b, err = Marshal(pallValue)
  1053      if err != nil {
  1054          t.Fatalf("Marshal pallValue: %v", err)
  1055      }
  1056      if string(b) != pallValueCompact {
  1057          t.Errorf("Marshal pallValueCompact")
  1058          diff(t, b, []byte(pallValueCompact))
  1059          return
  1060      }
  1061  }
  1062  
  1063  func TestMarshalNumberZeroVal(t *testing.T) {
  1064      var n json.Number
  1065      out, err := Marshal(n)
  1066      if err != nil {
  1067          t.Fatal(err)
  1068      }
  1069      outStr := string(out)
  1070      if outStr != "0" {
  1071          t.Fatalf("Invalid zero val for json.Number: %q", outStr)
  1072      }
  1073  }
  1074  
  1075  func TestMarshalEmbeds(t *testing.T) {
  1076      top := &Top{
  1077          Level0: 1,
  1078          Embed0: Embed0{
  1079              Level1b: 2,
  1080              Level1c: 3,
  1081          },
  1082          Embed0a: &Embed0a{
  1083              Level1a: 5,
  1084              Level1b: 6,
  1085          },
  1086          Embed0b: &Embed0b{
  1087              Level1a: 8,
  1088              Level1b: 9,
  1089              Level1c: 10,
  1090              Level1d: 11,
  1091              Level1e: 12,
  1092          },
  1093          Loop: Loop{
  1094              Loop1: 13,
  1095              Loop2: 14,
  1096          },
  1097          Embed0p: Embed0p{
  1098              Point: image.Point{X: 15, Y: 16},
  1099          },
  1100          Embed0q: Embed0q{
  1101              Point: Point{Z: 17},
  1102          },
  1103          embed: embed{
  1104              Q: 18,
  1105          },
  1106      }
  1107      b, err := Marshal(top)
  1108      if err != nil {
  1109          t.Fatal(err)
  1110      }
  1111      want := "{\"Level0\":1,\"Level1b\":2,\"Level1c\":3,\"Level1a\":5,\"LEVEL1B\":6,\"e\":{\"Level1a\":8,\"Level1b\":9,\"Level1c\":10,\"Level1d\":11,\"x\":12},\"Loop1\":13,\"Loop2\":14,\"X\":15,\"Y\":16,\"Z\":17,\"Q\":18}"
  1112      if string(b) != want {
  1113          t.Errorf("Wrong marshal result.\n got: %q\nwant: %q", b, want)
  1114      }
  1115  }
  1116  
  1117  func TestUnmarshal(t *testing.T) {
  1118      for i, tt := range unmarshalTests {
  1119          t.Log(i, tt.in)
  1120          if !json.Valid([]byte(tt.in)) {
  1121              continue
  1122          }
  1123          if tt.ptr == nil {
  1124              continue
  1125          }
  1126  
  1127          typ := reflect.TypeOf(tt.ptr)
  1128          if typ.Kind() != reflect.Ptr {
  1129              t.Errorf("#%d: unmarshalTest.ptr %T is not a pointer type", i, tt.ptr)
  1130              continue
  1131          }
  1132          typ = typ.Elem()
  1133  
  1134          // v = new(right-type)
  1135          v := reflect.New(typ)
  1136  
  1137          if !reflect.DeepEqual(tt.ptr, v.Interface()) {
  1138              // There's no reason for ptr to point to non-zero data,
  1139              // as we decode into new(right-type), so the data is
  1140              // discarded.
  1141              // This can easily mean tests that silently don't test
  1142              // what they should. To test decoding into existing
  1143              // data, see TestPrefilled.
  1144              t.Errorf("#%d: unmarshalTest.ptr %#v is not a pointer to a zero value", i, tt.ptr)
  1145              continue
  1146          }
  1147  
  1148          dec := decoder.NewDecoder(tt.in)
  1149          if tt.useNumber {
  1150              dec.UseNumber()
  1151          }
  1152          if tt.disallowUnknownFields {
  1153              dec.DisallowUnknownFields()
  1154          }
  1155          if tt.validateString {
  1156              dec.ValidateString()
  1157          }
  1158          if err := dec.Decode(v.Interface()); (err == nil) != (tt.err == nil) {
  1159              spew.Dump(tt)
  1160              t.Fatalf("#%d: %v, want %v", i, err, tt.err)
  1161              continue
  1162          } else if err != nil {
  1163              continue
  1164          }
  1165          if !reflect.DeepEqual(v.Elem().Interface(), tt.out) {
  1166              t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), tt.out)
  1167              data, _ := Marshal(v.Elem().Interface())
  1168              println(string(data))
  1169              data, _ = Marshal(tt.out)
  1170              println(string(data))
  1171              continue
  1172          }
  1173  
  1174          // Check round trip also decodes correctly.
  1175          if tt.err == nil {
  1176              enc, err := Marshal(v.Interface())
  1177              if err != nil {
  1178                  t.Errorf("#%d: error re-marshaling: %v", i, err)
  1179                  continue
  1180              }
  1181              if tt.golden && !bytes.Equal(enc, []byte(tt.in)) {
  1182                  t.Errorf("#%d: remarshal mismatch:\nhave: %s\nwant: %s", i, enc, []byte(tt.in))
  1183              }
  1184              vv := reflect.New(reflect.TypeOf(tt.ptr).Elem())
  1185              dec = decoder.NewDecoder(string(enc))
  1186              if tt.useNumber {
  1187                  dec.UseNumber()
  1188              }
  1189              if err := dec.Decode(vv.Interface()); err != nil {
  1190                  t.Errorf("#%d: error re-unmarshaling %#q: %v", i, enc, err)
  1191                  continue
  1192              }
  1193              if !reflect.DeepEqual(v.Elem().Interface(), vv.Elem().Interface()) {
  1194                  t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), vv.Elem().Interface())
  1195                  t.Errorf("     In: %q", strings.Map(noSpace, tt.in))
  1196                  t.Errorf("Marshal: %q", strings.Map(noSpace, string(enc)))
  1197                  continue
  1198              }
  1199          }
  1200      }
  1201  }
  1202  
  1203  var jsonBig []byte
  1204  
  1205  func initBig() {
  1206      n := 10000
  1207      if testing.Short() {
  1208          n = 100
  1209      }
  1210      b, err := Marshal(genValue(n))
  1211      if err != nil {
  1212          panic(err)
  1213      }
  1214      jsonBig = b
  1215  }
  1216  
  1217  func genValue(n int) interface{} {
  1218      if n > 1 {
  1219          switch rand.Intn(2) {
  1220          case 0:
  1221              return genArray(n)
  1222          case 1:
  1223              return genMap(n)
  1224          }
  1225      }
  1226      switch rand.Intn(3) {
  1227      case 0:
  1228          return rand.Intn(2) == 0
  1229      case 1:
  1230          return float64(rand.Uint64()) / 4294967296
  1231      case 2:
  1232          return genString(30)
  1233      }
  1234      panic("unreachable")
  1235  }
  1236  
  1237  func genString(stddev float64) string {
  1238      n := int(math.Abs(rand.NormFloat64()*stddev + stddev/2))
  1239      c := make([]rune, n)
  1240      for i := range c {
  1241          f := math.Abs(rand.NormFloat64()*64 + 32)
  1242          if f > 0x10ffff {
  1243              f = 0x10ffff
  1244          }
  1245          c[i] = rune(f)
  1246      }
  1247      return string(c)
  1248  }
  1249  
  1250  func genArray(n int) []interface{} {
  1251      f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2)))
  1252      if f > n {
  1253          f = n
  1254      }
  1255      if f < 1 {
  1256          f = 1
  1257      }
  1258      x := make([]interface{}, f)
  1259      for i := range x {
  1260          x[i] = genValue(((i+1)*n)/f - (i*n)/f)
  1261      }
  1262      return x
  1263  }
  1264  
  1265  func genMap(n int) map[string]interface{} {
  1266      f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2)))
  1267      if f > n {
  1268          f = n
  1269      }
  1270      if n > 0 && f == 0 {
  1271          f = 1
  1272      }
  1273      x := make(map[string]interface{})
  1274      x[genString(10)] = genValue(n/f)
  1275      return x
  1276  }
  1277  
  1278  func TestUnmarshalMarshal(t *testing.T) {
  1279      initBig()
  1280      var v interface{}
  1281      if err := Unmarshal(jsonBig, &v); err != nil {
  1282          if e, ok := err.(decoder.SyntaxError); ok {
  1283              println(e.Description())
  1284          }
  1285          t.Fatalf("Unmarshal: %v", err)
  1286      }
  1287      b, err := Marshal(v)
  1288      if err != nil {
  1289          t.Fatalf("Marshal: %v", err)
  1290      }
  1291      if !bytes.Equal(jsonBig, b) {
  1292          t.Errorf("Marshal jsonBig")
  1293          diff(t, b, jsonBig)
  1294          println(string(b))
  1295          println(string(jsonBig))
  1296          return
  1297      }
  1298  }
  1299  
  1300  var numberTests = []struct {
  1301      in       string
  1302      i        int64
  1303      intErr   string
  1304      f        float64
  1305      floatErr string
  1306  }{
  1307      {in: "-1.23e1", intErr: "strconv.ParseInt: parsing \"-1.23e1\": invalid syntax", f: -1.23e1},
  1308      {in: "-12", i: -12, f: -12.0},
  1309      {in: "1e1000", intErr: "strconv.ParseInt: parsing \"1e1000\": invalid syntax", floatErr: "strconv.ParseFloat: parsing \"1e1000\": value out of range"},
  1310  }
  1311  
  1312  // Independent of Decode, basic coverage of the accessors in json.Number
  1313  func TestNumberAccessors(t *testing.T) {
  1314      for _, tt := range numberTests {
  1315          n := json.Number(tt.in)
  1316          if s := n.String(); s != tt.in {
  1317              t.Errorf("json.Number(%q).String() is %q", tt.in, s)
  1318          }
  1319          if i, err := n.Int64(); err == nil && tt.intErr == "" && i != tt.i {
  1320              t.Errorf("json.Number(%q).Int64() is %d", tt.in, i)
  1321          } else if (err == nil && tt.intErr != "") || (err != nil && err.Error() != tt.intErr) {
  1322              t.Errorf("json.Number(%q).Int64() wanted error %q but got: %v", tt.in, tt.intErr, err)
  1323          }
  1324          if f, err := n.Float64(); err == nil && tt.floatErr == "" && f != tt.f {
  1325              t.Errorf("json.Number(%q).Float64() is %g", tt.in, f)
  1326          } else if (err == nil && tt.floatErr != "") || (err != nil && err.Error() != tt.floatErr) {
  1327              t.Errorf("json.Number(%q).Float64() wanted error %q but got: %v", tt.in, tt.floatErr, err)
  1328          }
  1329      }
  1330  }
  1331  
  1332  func TestLargeByteSlice(t *testing.T) {
  1333      s0 := make([]byte, 2000)
  1334      for i := range s0 {
  1335          s0[i] = byte(i)
  1336      }
  1337      b, err := Marshal(s0)
  1338      if err != nil {
  1339          t.Fatalf("Marshal: %v", err)
  1340      }
  1341      var s1 []byte
  1342      if err := Unmarshal(b, &s1); err != nil {
  1343          t.Fatalf("Unmarshal: %v", err)
  1344      }
  1345      if !bytes.Equal(s0, s1) {
  1346          t.Errorf("Marshal large byte slice")
  1347          diff(t, s0, s1)
  1348      }
  1349  }
  1350  
  1351  type Xint struct {
  1352      X int
  1353  }
  1354  
  1355  func TestUnmarshalPtrPtr(t *testing.T) {
  1356      var xint Xint
  1357      pxint := &xint
  1358      if err := Unmarshal([]byte(`{"X":1}`), &pxint); err != nil {
  1359          t.Fatalf("Unmarshal: %v", err)
  1360      }
  1361      if xint.X != 1 {
  1362          t.Fatalf("Did not write to xint")
  1363      }
  1364  }
  1365  
  1366  func isSpace(c byte) bool {
  1367      return c <= ' ' && (c == ' ' || c == '\t' || c == '\r' || c == '\n')
  1368  }
  1369  
  1370  func noSpace(c rune) rune {
  1371      if isSpace(byte(c)) { // only used for ascii
  1372          return -1
  1373      }
  1374      return c
  1375  }
  1376  
  1377  type All struct {
  1378      Bool    bool
  1379      Int     int
  1380      Int8    int8
  1381      Int16   int16
  1382      Int32   int32
  1383      Int64   int64
  1384      Uint    uint
  1385      Uint8   uint8
  1386      Uint16  uint16
  1387      Uint32  uint32
  1388      Uint64  uint64
  1389      Uintptr uintptr
  1390      Float32 float32
  1391      Float64 float64
  1392  
  1393      Foo  string `json:"bar"`
  1394      Foo2 string `json:"bar2,dummyopt"`
  1395  
  1396      IntStr     int64   `json:",string"`
  1397      UintptrStr uintptr `json:",string"`
  1398  
  1399      PBool    *bool
  1400      PInt     *int
  1401      PInt8    *int8
  1402      PInt16   *int16
  1403      PInt32   *int32
  1404      PInt64   *int64
  1405      PUint    *uint
  1406      PUint8   *uint8
  1407      PUint16  *uint16
  1408      PUint32  *uint32
  1409      PUint64  *uint64
  1410      PUintptr *uintptr
  1411      PFloat32 *float32
  1412      PFloat64 *float64
  1413  
  1414      String  string
  1415      PString *string
  1416  
  1417      Map      map[string]Small
  1418      MapP     map[string]*Small
  1419      MapPNil  map[string]*Small
  1420      PMap     *map[string]Small
  1421      PMapP    *map[string]*Small
  1422      PMapPNil *map[string]*Small
  1423  
  1424      EmptyMap map[string]Small
  1425      NilMap   map[string]Small
  1426  
  1427      Slice   []Small
  1428      SliceP  []*Small
  1429      PSlice  *[]Small
  1430      PSliceP *[]*Small
  1431  
  1432      EmptySlice []Small
  1433      NilSlice   []Small
  1434  
  1435      StringSlice []string
  1436      ByteSlice   []byte
  1437  
  1438      Small   Small
  1439      PSmall  *Small
  1440      PPSmall **Small
  1441  
  1442      Interface  interface{}
  1443      PInterface *interface{}
  1444  
  1445      unexported int
  1446  }
  1447  
  1448  type Small struct {
  1449      Tag string
  1450  }
  1451  
  1452  var allValue = All{
  1453      Bool:       true,
  1454      Int:        2,
  1455      Int8:       3,
  1456      Int16:      4,
  1457      Int32:      5,
  1458      Int64:      6,
  1459      Uint:       7,
  1460      Uint8:      8,
  1461      Uint16:     9,
  1462      Uint32:     10,
  1463      Uint64:     11,
  1464      Uintptr:    12,
  1465      Float32:    14.25,
  1466      Float64:    15.25,
  1467      Foo:        "foo",
  1468      Foo2:       "foo2",
  1469      IntStr:     42,
  1470      UintptrStr: 44,
  1471      String:     "16",
  1472      Map: map[string]Small{
  1473          "17": {Tag: "tag17"},
  1474      },
  1475      MapP: map[string]*Small{
  1476          "18": {Tag: "tag19"},
  1477      },
  1478      MapPNil: map[string]*Small{
  1479          "19": nil,
  1480      },
  1481      EmptyMap:    map[string]Small{},
  1482      Slice:       []Small{{Tag: "tag20"}, {Tag: "tag21"}},
  1483      SliceP:      []*Small{{Tag: "tag22"}, nil, {Tag: "tag23"}},
  1484      EmptySlice:  []Small{},
  1485      StringSlice: []string{"str24", "str25", "str26"},
  1486      ByteSlice:   []byte{27, 28, 29},
  1487      Small:       Small{Tag: "tag30"},
  1488      PSmall:      &Small{Tag: "tag31"},
  1489      Interface:   5.2,
  1490  }
  1491  
  1492  var pallValue = All{
  1493      PBool:      &allValue.Bool,
  1494      PInt:       &allValue.Int,
  1495      PInt8:      &allValue.Int8,
  1496      PInt16:     &allValue.Int16,
  1497      PInt32:     &allValue.Int32,
  1498      PInt64:     &allValue.Int64,
  1499      PUint:      &allValue.Uint,
  1500      PUint8:     &allValue.Uint8,
  1501      PUint16:    &allValue.Uint16,
  1502      PUint32:    &allValue.Uint32,
  1503      PUint64:    &allValue.Uint64,
  1504      PUintptr:   &allValue.Uintptr,
  1505      PFloat32:   &allValue.Float32,
  1506      PFloat64:   &allValue.Float64,
  1507      PString:    &allValue.String,
  1508      PMap:       &allValue.Map,
  1509      PMapP:      &allValue.MapP,
  1510      PMapPNil:   &allValue.MapPNil,
  1511      PSlice:     &allValue.Slice,
  1512      PSliceP:    &allValue.SliceP,
  1513      PPSmall:    &allValue.PSmall,
  1514      PInterface: &allValue.Interface,
  1515  }
  1516  
  1517  var allValueIndent = `{
  1518      "Bool": true,
  1519      "Int": 2,
  1520      "Int8": 3,
  1521      "Int16": 4,
  1522      "Int32": 5,
  1523      "Int64": 6,
  1524      "Uint": 7,
  1525      "Uint8": 8,
  1526      "Uint16": 9,
  1527      "Uint32": 10,
  1528      "Uint64": 11,
  1529      "Uintptr": 12,
  1530      "Float32": 14.25,
  1531      "Float64": 15.25,
  1532      "bar": "foo",
  1533      "bar2": "foo2",
  1534      "IntStr": "42",
  1535      "UintptrStr": "44",
  1536      "PBool": null,
  1537      "PInt": null,
  1538      "PInt8": null,
  1539      "PInt16": null,
  1540      "PInt32": null,
  1541      "PInt64": null,
  1542      "PUint": null,
  1543      "PUint8": null,
  1544      "PUint16": null,
  1545      "PUint32": null,
  1546      "PUint64": null,
  1547      "PUintptr": null,
  1548      "PFloat32": null,
  1549      "PFloat64": null,
  1550      "String": "16",
  1551      "PString": null,
  1552      "Map": {
  1553          "17": {
  1554              "Tag": "tag17"
  1555          }
  1556      },
  1557      "MapP": {
  1558          "18": {
  1559              "Tag": "tag19"
  1560          }
  1561      },
  1562      "MapPNil": {
  1563          "19": null
  1564      },
  1565      "PMap": null,
  1566      "PMapP": null,
  1567      "PMapPNil": null,
  1568      "EmptyMap": {},
  1569      "NilMap": null,
  1570      "Slice": [
  1571          {
  1572              "Tag": "tag20"
  1573          },
  1574          {
  1575              "Tag": "tag21"
  1576          }
  1577      ],
  1578      "SliceP": [
  1579          {
  1580              "Tag": "tag22"
  1581          },
  1582          null,
  1583          {
  1584              "Tag": "tag23"
  1585          }
  1586      ],
  1587      "PSlice": null,
  1588      "PSliceP": null,
  1589      "EmptySlice": [],
  1590      "NilSlice": null,
  1591      "StringSlice": [
  1592          "str24",
  1593          "str25",
  1594          "str26"
  1595      ],
  1596      "ByteSlice": "Gxwd",
  1597      "Small": {
  1598          "Tag": "tag30"
  1599      },
  1600      "PSmall": {
  1601          "Tag": "tag31"
  1602      },
  1603      "PPSmall": null,
  1604      "Interface": 5.2,
  1605      "PInterface": null
  1606  }`
  1607  
  1608  var allValueCompact = strings.Map(noSpace, allValueIndent)
  1609  
  1610  var pallValueIndent = `{
  1611      "Bool": false,
  1612      "Int": 0,
  1613      "Int8": 0,
  1614      "Int16": 0,
  1615      "Int32": 0,
  1616      "Int64": 0,
  1617      "Uint": 0,
  1618      "Uint8": 0,
  1619      "Uint16": 0,
  1620      "Uint32": 0,
  1621      "Uint64": 0,
  1622      "Uintptr": 0,
  1623      "Float32": 0,
  1624      "Float64": 0,
  1625      "bar": "",
  1626      "bar2": "",
  1627      "IntStr": "0",
  1628      "UintptrStr": "0",
  1629      "PBool": true,
  1630      "PInt": 2,
  1631      "PInt8": 3,
  1632      "PInt16": 4,
  1633      "PInt32": 5,
  1634      "PInt64": 6,
  1635      "PUint": 7,
  1636      "PUint8": 8,
  1637      "PUint16": 9,
  1638      "PUint32": 10,
  1639      "PUint64": 11,
  1640      "PUintptr": 12,
  1641      "PFloat32": 14.25,
  1642      "PFloat64": 15.25,
  1643      "String": "",
  1644      "PString": "16",
  1645      "Map": null,
  1646      "MapP": null,
  1647      "MapPNil": null,
  1648      "PMap": {
  1649          "17": {
  1650              "Tag": "tag17"
  1651          }
  1652      },
  1653      "PMapP": {
  1654          "18": {
  1655              "Tag": "tag19"
  1656          }
  1657      },
  1658      "PMapPNil": {
  1659          "19": null
  1660      },
  1661      "EmptyMap": null,
  1662      "NilMap": null,
  1663      "Slice": null,
  1664      "SliceP": null,
  1665      "PSlice": [
  1666          {
  1667              "Tag": "tag20"
  1668          },
  1669          {
  1670              "Tag": "tag21"
  1671          }
  1672      ],
  1673      "PSliceP": [
  1674          {
  1675              "Tag": "tag22"
  1676          },
  1677          null,
  1678          {
  1679              "Tag": "tag23"
  1680          }
  1681      ],
  1682      "EmptySlice": null,
  1683      "NilSlice": null,
  1684      "StringSlice": null,
  1685      "ByteSlice": null,
  1686      "Small": {
  1687          "Tag": ""
  1688      },
  1689      "PSmall": null,
  1690      "PPSmall": {
  1691          "Tag": "tag31"
  1692      },
  1693      "Interface": null,
  1694      "PInterface": 5.2
  1695  }`
  1696  
  1697  var pallValueCompact = strings.Map(noSpace, pallValueIndent)
  1698  
  1699  func TestRefUnmarshal(t *testing.T) {
  1700      type S struct {
  1701          // Ref is defined in encode_test.go.
  1702          R0 Ref
  1703          R1 *Ref
  1704          R2 RefText
  1705          R3 *RefText
  1706      }
  1707      want := S{
  1708          R0: 12,
  1709          R1: new(Ref),
  1710          R2: 13,
  1711          R3: new(RefText),
  1712      }
  1713      *want.R1 = 12
  1714      *want.R3 = 13
  1715  
  1716      var got S
  1717      if err := Unmarshal([]byte(`{"R0":"ref","R1":"ref","R2":"ref","R3":"ref"}`), &got); err != nil {
  1718          t.Fatalf("Unmarshal: %v", err)
  1719      }
  1720      if !reflect.DeepEqual(got, want) {
  1721          t.Errorf("got %+v, want %+v", got, want)
  1722      }
  1723  }
  1724  
  1725  // Test that the empty string doesn't panic decoding when ,string is specified
  1726  // Issue 3450
  1727  func TestEmptyString(t *testing.T) {
  1728      type T2 struct {
  1729          Number1 int `json:",string"`
  1730          Number2 string `json:",string"`
  1731          Pass bool `json:",string"`
  1732      }
  1733      data := `{"Number1":"1", "Number2":"","Pass":"true"}`
  1734      var t2, t3 T2
  1735      t2.Number2 = "a"
  1736      t3.Number2 = "a"
  1737      err := Unmarshal([]byte(data), &t2)
  1738      if err == nil {
  1739          t.Fatal("Decode: did not return error")
  1740      }
  1741      println(err.Error())
  1742      err2 := json.Unmarshal([]byte(data), &t3)
  1743      assert.Equal(t, err == nil, err2 == nil)
  1744      assert.Equal(t, t3, t2)
  1745  }
  1746  
  1747  // Test that a null for ,string is not replaced with the previous quoted string (issue 7046).
  1748  // It should also not be an error (issue 2540, issue 8587).
  1749  func TestNullString(t *testing.T) {
  1750      type T struct {
  1751          A int  `json:",string"`
  1752          B int  `json:",string"`
  1753          C *int `json:",string"`
  1754      }
  1755      data := []byte(`{"A": "1", "B": null, "C": null}`)
  1756      var s T
  1757      s.B = 1
  1758      s.C = new(int)
  1759      *s.C = 2
  1760      err := Unmarshal(data, &s)
  1761      if err != nil {
  1762          t.Fatalf("Unmarshal: %v", err)
  1763      }
  1764      if s.B != 1 || s.C != nil {
  1765          t.Fatalf("after Unmarshal, s.B=%d, s.C=%p, want 1, nil", s.B, s.C)
  1766      }
  1767  }
  1768  
  1769  type NullTest struct {
  1770      Bool      bool
  1771      Int       int
  1772      Int8      int8
  1773      Int16     int16
  1774      Int32     int32
  1775      Int64     int64
  1776      Uint      uint
  1777      Uint8     uint8
  1778      Uint16    uint16
  1779      Uint32    uint32
  1780      Uint64    uint64
  1781      Float32   float32
  1782      Float64   float64
  1783      String    string
  1784      PBool     *bool
  1785      Map       map[string]string
  1786      Slice     []string
  1787      Interface interface{}
  1788  
  1789      PRaw    *json.RawMessage
  1790      PTime   *time.Time
  1791      PBigInt *big.Int
  1792      PText   *MustNotUnmarshalText
  1793      PBuffer *bytes.Buffer // has methods, just not relevant ones
  1794      PStruct *struct{}
  1795  
  1796      Raw    json.RawMessage
  1797      Time   time.Time
  1798      BigInt big.Int
  1799      Text   MustNotUnmarshalText
  1800      Buffer bytes.Buffer
  1801      Struct struct{}
  1802  }
  1803  
  1804  // JSON null values should be ignored for primitives and string values instead of resulting in an error.
  1805  // Issue 2540
  1806  func TestUnmarshalNulls(t *testing.T) {
  1807      // Unmarshal docs:
  1808      // The JSON null value unmarshals into an interface, map, pointer, or slice
  1809      // by setting that Go value to nil. Because null is often used in JSON to mean
  1810      // ``not present,'' unmarshaling a JSON null into any other Go type has no effect
  1811      // on the value and produces no error.
  1812  
  1813      jsonData := []byte(`{
  1814                  "Bool"    : null,
  1815                  "Int"     : null,
  1816                  "Int8"    : null,
  1817                  "Int16"   : null,
  1818                  "Int32"   : null,
  1819                  "Int64"   : null,
  1820                  "Uint"    : null,
  1821                  "Uint8"   : null,
  1822                  "Uint16"  : null,
  1823                  "Uint32"  : null,
  1824                  "Uint64"  : null,
  1825                  "Float32" : null,
  1826                  "Float64" : null,
  1827                  "String"  : null,
  1828                  "PBool": null,
  1829                  "Map": null,
  1830                  "Slice": null,
  1831                  "Interface": null,
  1832                  "PRaw": null,
  1833                  "PTime": null,
  1834                  "PBigInt": null,
  1835                  "PText": null,
  1836                  "PBuffer": null,
  1837                  "PStruct": null,
  1838                  "Raw": null,
  1839                  "Time": null,
  1840                  "BigInt": null,
  1841                  "Text": null,
  1842                  "Buffer": null,
  1843                  "Struct": null
  1844              }`)
  1845      nulls := NullTest{
  1846          Bool:      true,
  1847          Int:       2,
  1848          Int8:      3,
  1849          Int16:     4,
  1850          Int32:     5,
  1851          Int64:     6,
  1852          Uint:      7,
  1853          Uint8:     8,
  1854          Uint16:    9,
  1855          Uint32:    10,
  1856          Uint64:    11,
  1857          Float32:   12.1,
  1858          Float64:   13.1,
  1859          String:    "14",
  1860          PBool:     new(bool),
  1861          Map:       map[string]string{},
  1862          Slice:     []string{},
  1863          Interface: new(MustNotUnmarshalJSON),
  1864          PRaw:      new(json.RawMessage),
  1865          PTime:     new(time.Time),
  1866          PBigInt:   new(big.Int),
  1867          PText:     new(MustNotUnmarshalText),
  1868          PStruct:   new(struct{}),
  1869          PBuffer:   new(bytes.Buffer),
  1870          Raw:       json.RawMessage("123"),
  1871          Time:      time.Unix(123456789, 0),
  1872          BigInt:    *big.NewInt(123),
  1873      }
  1874  
  1875      before := nulls.Time.String()
  1876  
  1877      err := Unmarshal(jsonData, &nulls)
  1878      if err != nil {
  1879          t.Errorf("Unmarshal of null values failed: %v", err)
  1880      }
  1881      if !nulls.Bool || nulls.Int != 2 || nulls.Int8 != 3 || nulls.Int16 != 4 || nulls.Int32 != 5 || nulls.Int64 != 6 ||
  1882          nulls.Uint != 7 || nulls.Uint8 != 8 || nulls.Uint16 != 9 || nulls.Uint32 != 10 || nulls.Uint64 != 11 ||
  1883          nulls.Float32 != 12.1 || nulls.Float64 != 13.1 || nulls.String != "14" {
  1884          t.Errorf("Unmarshal of null values affected primitives")
  1885      }
  1886  
  1887      if nulls.PBool != nil {
  1888          t.Errorf("Unmarshal of null did not clear nulls.PBool")
  1889      }
  1890      if nulls.Map != nil {
  1891          t.Errorf("Unmarshal of null did not clear nulls.Map")
  1892      }
  1893      if nulls.Slice != nil {
  1894          t.Errorf("Unmarshal of null did not clear nulls.Slice")
  1895      }
  1896      if nulls.Interface != nil {
  1897          t.Errorf("Unmarshal of null did not clear nulls.Interface")
  1898      }
  1899      if nulls.PRaw != nil {
  1900          t.Errorf("Unmarshal of null did not clear nulls.PRaw")
  1901      }
  1902      if nulls.PTime != nil {
  1903          t.Errorf("Unmarshal of null did not clear nulls.PTime")
  1904      }
  1905      if nulls.PBigInt != nil {
  1906          t.Errorf("Unmarshal of null did not clear nulls.PBigInt")
  1907      }
  1908      if nulls.PText != nil {
  1909          t.Errorf("Unmarshal of null did not clear nulls.PText")
  1910      }
  1911      if nulls.PBuffer != nil {
  1912          t.Errorf("Unmarshal of null did not clear nulls.PBuffer")
  1913      }
  1914      if nulls.PStruct != nil {
  1915          t.Errorf("Unmarshal of null did not clear nulls.PStruct")
  1916      }
  1917  
  1918      if string(nulls.Raw) != "null" {
  1919          t.Errorf("Unmarshal of json.RawMessage null did not record null: %v", string(nulls.Raw))
  1920      }
  1921      if nulls.Time.String() != before {
  1922          t.Errorf("Unmarshal of time.Time null set time to %v", nulls.Time.String())
  1923      }
  1924      if nulls.BigInt.String() != "123" {
  1925          t.Errorf("Unmarshal of big.Int null set int to %v", nulls.BigInt.String())
  1926      }
  1927  }
  1928  
  1929  type MustNotUnmarshalJSON struct{}
  1930  
  1931  func (x MustNotUnmarshalJSON) UnmarshalJSON(_ []byte) error {
  1932      return errors.New("MustNotUnmarshalJSON was used")
  1933  }
  1934  
  1935  type MustNotUnmarshalText struct{}
  1936  
  1937  func (x MustNotUnmarshalText) UnmarshalText(_ []byte) error {
  1938      return errors.New("MustNotUnmarshalText was used")
  1939  }
  1940  
  1941  func TestStringKind(t *testing.T) {
  1942      type stringKind string
  1943  
  1944      var m1, m2 map[stringKind]int
  1945      m1 = map[stringKind]int{
  1946          "foo": 42,
  1947      }
  1948  
  1949      data, err := Marshal(m1)
  1950      if err != nil {
  1951          t.Errorf("Unexpected error marshaling: %v", err)
  1952      }
  1953  
  1954      err = Unmarshal(data, &m2)
  1955      if err != nil {
  1956          t.Errorf("Unexpected error unmarshaling: %v", err)
  1957      }
  1958  
  1959      if !reflect.DeepEqual(m1, m2) {
  1960          t.Error("Items should be equal after encoding and then decoding")
  1961      }
  1962  }
  1963  
  1964  // Custom types with []byte as underlying type could not be marshaled
  1965  // and then unmarshaled.
  1966  // Issue 8962.
  1967  func TestByteKind(t *testing.T) {
  1968      type byteKind []byte
  1969  
  1970      a := byteKind("hello")
  1971  
  1972      data, err := Marshal(a)
  1973      if err != nil {
  1974          t.Error(err)
  1975      }
  1976      var b byteKind
  1977      err = Unmarshal(data, &b)
  1978      if err != nil {
  1979          t.Fatal(err)
  1980      }
  1981      if !reflect.DeepEqual(a, b) {
  1982          t.Errorf("expected %v == %v", a, b)
  1983      }
  1984  }
  1985  
  1986  // The fix for issue 8962 introduced a regression.
  1987  // Issue 12921.
  1988  func TestSliceOfCustomByte(t *testing.T) {
  1989      type Uint8 uint8
  1990  
  1991      a := []Uint8("hello")
  1992  
  1993      data, err := Marshal(a)
  1994      if err != nil {
  1995          t.Fatal(err)
  1996      }
  1997      var b []Uint8
  1998      err = Unmarshal(data, &b)
  1999      if err != nil {
  2000          t.Fatal(err)
  2001      }
  2002      if !reflect.DeepEqual(a, b) {
  2003          t.Fatalf("expected %v == %v", a, b)
  2004      }
  2005  }
  2006  
  2007  var decodeTypeErrorTests = []struct {
  2008      dest interface{}
  2009      src  string
  2010  }{
  2011      {new(error), `{}`},                // issue 4222
  2012      {new(error), `[]`},
  2013      {new(error), `""`},
  2014      {new(error), `123`},
  2015      {new(error), `true`},
  2016  }
  2017  
  2018  func TestUnmarshalTypeError(t *testing.T) {
  2019      for _, item := range decodeTypeErrorTests {
  2020          err := Unmarshal([]byte(item.src), item.dest)
  2021          if _, ok := err.(*json.UnmarshalTypeError); !ok {
  2022              if _, ok = err.(decoder.SyntaxError); !ok {
  2023                  t.Errorf("expected type error for Unmarshal(%q, type %T): got %T",
  2024                      item.src, item.dest, err)
  2025              }
  2026          }
  2027      }
  2028  }
  2029  
  2030  var decodeMismatchErrorTests = []struct {
  2031      dest interface{}
  2032      src  string
  2033  }{
  2034      {new(int), `{}`},               
  2035      {new(string), `{}`},               
  2036      {new(bool), `{}`},               
  2037      {new([]byte), `{}`},               
  2038  }
  2039  
  2040  func TestMismatchTypeError(t *testing.T) {
  2041      for _, item := range decodeMismatchErrorTests {
  2042          err := Unmarshal([]byte(item.src), item.dest)
  2043          if _, ok := err.(*decoder.MismatchTypeError); !ok {
  2044              if _, ok = err.(decoder.SyntaxError); !ok {
  2045                  t.Errorf("expected mismatch error for Unmarshal(%q, type %T): got %T",
  2046                      item.src, item.dest, err)
  2047              }
  2048          }
  2049      }
  2050  }
  2051  
  2052  var unmarshalSyntaxTests = []string{
  2053      "tru",
  2054      "fals",
  2055      "nul",
  2056      "123e",
  2057      `"hello`,
  2058      `[1,2,3`,
  2059      `{"key":1`,
  2060      `{"key":1,`,
  2061  }
  2062  
  2063  func TestUnmarshalSyntax(t *testing.T) {
  2064      var x interface{}
  2065      for _, src := range unmarshalSyntaxTests {
  2066          err := Unmarshal([]byte(src), &x)
  2067          if _, ok := err.(decoder.SyntaxError); !ok {
  2068              t.Errorf("expected syntax error for Unmarshal(%q): got %T", src, err)
  2069          }
  2070      }
  2071  }
  2072  
  2073  // Test handling of unexported fields that should be ignored.
  2074  // Issue 4660
  2075  //goland:noinspection GoVetStructTag
  2076  type unexportedFields struct {
  2077      Name string
  2078      m    map[string]interface{} `json:"-"`
  2079      m2   map[string]interface{} `json:"abcd"`
  2080  
  2081      s []int `json:"-"`
  2082  }
  2083  
  2084  func TestUnmarshalUnexported(t *testing.T) {
  2085      input := `{"Name": "Bob", "m": {"x": 123}, "m2": {"y": 456}, "abcd": {"z": 789}, "s": [2, 3]}`
  2086      want := &unexportedFields{Name: "Bob"}
  2087  
  2088      out := &unexportedFields{}
  2089      err := Unmarshal([]byte(input), out)
  2090      if err != nil {
  2091          t.Errorf("got error %v, expected nil", err)
  2092      }
  2093      if !reflect.DeepEqual(out, want) {
  2094          t.Errorf("got %q, want %q", out, want)
  2095      }
  2096  }
  2097  
  2098  // Time3339 is a time.Time which encodes to and from JSON
  2099  // as an RFC 3339 time in UTC.
  2100  type Time3339 time.Time
  2101  
  2102  func (t *Time3339) UnmarshalJSON(b []byte) error {
  2103      if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
  2104          return fmt.Errorf("types: failed to unmarshal non-string value %q as an RFC 3339 time", b)
  2105      }
  2106      tm, err := time.Parse(time.RFC3339, string(b[1:len(b)-1]))
  2107      if err != nil {
  2108          return err
  2109      }
  2110      *t = Time3339(tm)
  2111      return nil
  2112  }
  2113  
  2114  func TestUnmarshalJSONLiteralError(t *testing.T) {
  2115      var t3 Time3339
  2116      err := Unmarshal([]byte(`"0000-00-00T00:00:00Z"`), &t3)
  2117      if err == nil {
  2118          t.Fatalf("expected error; got time %v", time.Time(t3))
  2119      }
  2120      if !strings.Contains(err.Error(), "range") {
  2121          t.Errorf("got err = %v; want out of range error", err)
  2122      }
  2123  }
  2124  
  2125  // Test that extra object elements in an array do not result in a
  2126  // "data changing underfoot" error.
  2127  // Issue 3717
  2128  func TestSkipArrayObjects(t *testing.T) {
  2129      s := `[{}]`
  2130      var dest [0]interface{}
  2131  
  2132      err := Unmarshal([]byte(s), &dest)
  2133      if err != nil {
  2134          t.Errorf("got error %q, want nil", err)
  2135      }
  2136  }
  2137  
  2138  // Test semantics of pre-filled data, such as struct fields, map elements,
  2139  // slices, and arrays.
  2140  // Issues 4900 and 8837, among others.
  2141  func TestPrefilled(t *testing.T) {
  2142      // Values here change, cannot reuse table across runs.
  2143      var prefillTests = []struct {
  2144          in  string
  2145          ptr interface{}
  2146          out interface{}
  2147      }{
  2148          {
  2149              in:  `{"X": 1, "Y": 2}`,
  2150              ptr: &XYZ{X: float32(3), Y: int16(4), Z: 1.5},
  2151              out: &XYZ{X: float64(1), Y: float64(2), Z: 1.5},
  2152          },
  2153          {
  2154              in:  `{"X": 1, "Y": 2}`,
  2155              ptr: &map[string]interface{}{"X": float32(3), "Y": int16(4), "Z": 1.5},
  2156              out: &map[string]interface{}{"X": float64(1), "Y": float64(2), "Z": 1.5},
  2157          },
  2158          {
  2159              in:  `[2]`,
  2160              ptr: &[]int{1},
  2161              out: &[]int{2},
  2162          },
  2163          {
  2164              in:  `[2, 3]`,
  2165              ptr: &[]int{1},
  2166              out: &[]int{2, 3},
  2167          },
  2168          {
  2169              in:  `[2, 3]`,
  2170              ptr: &[...]int{1},
  2171              out: &[...]int{2},
  2172          },
  2173          {
  2174              in:  `[3]`,
  2175              ptr: &[...]int{1, 2},
  2176              out: &[...]int{3, 0},
  2177          },
  2178      }
  2179  
  2180      for _, tt := range prefillTests {
  2181          ptrstr := fmt.Sprintf("%v", tt.ptr)
  2182          err := Unmarshal([]byte(tt.in), tt.ptr) // tt.ptr edited here
  2183          if err != nil {
  2184              t.Errorf("Unmarshal: %v", err)
  2185          }
  2186          if !reflect.DeepEqual(tt.ptr, tt.out) {
  2187              t.Errorf("Unmarshal(%#q, %s): have %v, want %v", tt.in, ptrstr, tt.ptr, tt.out)
  2188          }
  2189      }
  2190  }
  2191  
  2192  var invalidUnmarshalTests = []struct {
  2193      v    interface{}
  2194      want string
  2195  }{
  2196      {nil, "json: Unmarshal(nil)"},
  2197      {struct{}{}, "json: Unmarshal(non-pointer struct {})"},
  2198      {(*int)(nil), "json: Unmarshal(nil *int)"},
  2199  }
  2200  
  2201  func TestInvalidUnmarshal(t *testing.T) {
  2202      buf := []byte(`{"a":"1"}`)
  2203      for _, tt := range invalidUnmarshalTests {
  2204          err := Unmarshal(buf, tt.v)
  2205          if err == nil {
  2206              t.Errorf("Unmarshal expecting error, got nil")
  2207              continue
  2208          }
  2209          if got := err.Error(); got != tt.want {
  2210              t.Errorf("Unmarshal = %q; want %q", got, tt.want)
  2211          }
  2212      }
  2213  }
  2214  
  2215  var invalidUnmarshalTextTests = []struct {
  2216      v    interface{}
  2217      want string
  2218  }{
  2219      {nil, "json: Unmarshal(nil)"},
  2220      {struct{}{}, "json: Unmarshal(non-pointer struct {})"},
  2221      {(*int)(nil), "json: Unmarshal(nil *int)"},
  2222      {new(net.IP), "json: cannot unmarshal number into Go value of type *net.IP"},
  2223  }
  2224  
  2225  func TestInvalidUnmarshalText(t *testing.T) {
  2226      buf := []byte(`123`)
  2227      for _, tt := range invalidUnmarshalTextTests {
  2228          err := Unmarshal(buf, tt.v)
  2229          if err == nil {
  2230              t.Errorf("Unmarshal expecting error, got nil")
  2231              continue
  2232          }
  2233      }
  2234  }
  2235  
  2236  // Test that string option is ignored for invalid types.
  2237  // Issue 9812.
  2238  func TestInvalidStringOption(t *testing.T) {
  2239      num := 0
  2240      item := struct {
  2241          T time.Time         `json:",string"`
  2242          M map[string]string `json:",string"`
  2243          S []string          `json:",string"`
  2244          A [1]string         `json:",string"`
  2245          I interface{}       `json:",string"`
  2246          P *int              `json:",string"`
  2247      }{M: make(map[string]string), S: make([]string, 0), I: num, P: &num}
  2248  
  2249      data, err := Marshal(item)
  2250      if err != nil {
  2251          t.Fatalf("Marshal: %v", err)
  2252      }
  2253      err = Unmarshal(data, &item)
  2254      if err != nil {
  2255          t.Fatalf("Unmarshal: %v", err)
  2256      }
  2257  }
  2258  
  2259  func TestUnmarshalErrorAfterMultipleJSON(t *testing.T) {
  2260      tests := []struct {
  2261          in  string
  2262          err error
  2263      }{{
  2264          in:  `1 false null :`,
  2265          err: (&JsonSyntaxError{"invalid character ':' looking for beginning of value", 13}).err(),
  2266      }, {
  2267          in:  `1 [] [,]`,
  2268          err: (&JsonSyntaxError{"invalid character ',' looking for beginning of value", 6}).err(),
  2269      }, {
  2270          in:  `1 [] [true:]`,
  2271          err: (&JsonSyntaxError{"invalid character ':' after array element", 10}).err(),
  2272      }, {
  2273          in:  `1  {}    {"x"=}`,
  2274          err: (&JsonSyntaxError{"invalid character '=' after object key", 13}).err(),
  2275      }, {
  2276          in:  `falsetruenul#`,
  2277          err: (&JsonSyntaxError{"invalid character '#' in literal null (expecting 'l')", 12}).err(),
  2278      }}
  2279      for i, tt := range tests {
  2280          dec := decoder.NewDecoder(tt.in)
  2281          var err error
  2282          for {
  2283              var v interface{}
  2284              if err = dec.Decode(&v); err != nil {
  2285                  break
  2286              }
  2287          }
  2288          if v, ok := err.(decoder.SyntaxError); !ok {
  2289              t.Errorf("#%d: got %#v, want %#v", i, err, tt.err)
  2290          } else if v.Pos != int(tt.err.(*json.SyntaxError).Offset) {
  2291              t.Errorf("#%d: got %#v, want %#v", i, err, tt.err)
  2292              println(v.Description())
  2293          }
  2294      }
  2295  }
  2296  
  2297  type unmarshalPanic struct{}
  2298  
  2299  func (unmarshalPanic) UnmarshalJSON([]byte) error { panic(0xdead) }
  2300  
  2301  func TestUnmarshalPanic(t *testing.T) {
  2302      defer func() {
  2303          if got := recover(); !reflect.DeepEqual(got, 0xdead) {
  2304              t.Errorf("panic() = (%T)(%v), want 0xdead", got, got)
  2305          }
  2306      }()
  2307      _ = Unmarshal([]byte("{}"), &unmarshalPanic{})
  2308      t.Fatalf("Unmarshal should have panicked")
  2309  }
  2310  
  2311  // The decoder used to hang if decoding into an interface pointing to its own address.
  2312  // See golang.org/issues/31740.
  2313  func TestUnmarshalRecursivePointer(t *testing.T) {
  2314      var v interface{}
  2315      v = &v
  2316      data := []byte(`{"a": "b"}`)
  2317  
  2318      if err := Unmarshal(data, v); err != nil {
  2319          t.Fatal(err)
  2320      }
  2321  }
  2322  
  2323  type textUnmarshalerString string
  2324  
  2325  func (m *textUnmarshalerString) UnmarshalText(text []byte) error {
  2326      *m = textUnmarshalerString(strings.ToLower(string(text)))
  2327      return nil
  2328  }
  2329  
  2330  // Test unmarshal to a map, where the map key is a user defined type.
  2331  // See golang.org/issues/34437.
  2332  func TestUnmarshalMapWithTextUnmarshalerStringKey(t *testing.T) {
  2333      var p map[textUnmarshalerString]string
  2334      if err := Unmarshal([]byte(`{"FOO": "1"}`), &p); err != nil {
  2335          t.Fatalf("Unmarshal unexpected error: %v", err)
  2336      }
  2337  
  2338      if _, ok := p["foo"]; !ok {
  2339          t.Errorf(`Key "foo" does not exist in map: %v`, p)
  2340      }
  2341  }
  2342  
  2343  func TestUnmarshalRescanLiteralMangledUnquote(t *testing.T) {
  2344      // See golang.org/issues/38105.
  2345      var p map[textUnmarshalerString]string
  2346      if err := Unmarshal([]byte(`{"开源":"12345开源"}`), &p); err != nil {
  2347          t.Fatalf("Unmarshal unexpected error: %v", err)
  2348      }
  2349      if _, ok := p["开源"]; !ok {
  2350          t.Errorf(`Key "开源" does not exist in map: %v`, p)
  2351      }
  2352  
  2353      // See golang.org/issues/38126.
  2354      type T struct {
  2355          F1 string `json:"F1,string"`
  2356      }
  2357      t1 := T{"aaa\tbbb"}
  2358  
  2359      b, err := Marshal(t1)
  2360      if err != nil {
  2361          t.Fatalf("Marshal unexpected error: %v", err)
  2362      }
  2363      var t2 T
  2364      if err := Unmarshal(b, &t2); err != nil {
  2365          t.Fatalf("Unmarshal unexpected error: %v", err)
  2366      }
  2367      if t1 != t2 {
  2368          t.Errorf("Marshal and Unmarshal roundtrip mismatch: want %q got %q", t1, t2)
  2369      }
  2370  
  2371      // See golang.org/issues/39555.
  2372      input := map[textUnmarshalerString]string{"FOO": "", `"`: ""}
  2373  
  2374      encoded, err := Marshal(input)
  2375      if err != nil {
  2376          t.Fatalf("Marshal unexpected error: %v", err)
  2377      }
  2378      var got map[textUnmarshalerString]string
  2379      if err := Unmarshal(encoded, &got); err != nil {
  2380          t.Fatalf("Unmarshal unexpected error: %v", err)
  2381      }
  2382      want := map[textUnmarshalerString]string{"foo": "", `"`: ""}
  2383      if !reflect.DeepEqual(want, got) {
  2384          t.Fatalf("Unexpected roundtrip result:\nwant: %q\ngot:  %q", want, got)
  2385      }
  2386  }
  2387  
  2388  func TestUnmarshalMaxDepth(t *testing.T) {
  2389      const (
  2390          _MaxDepth = types.MAX_RECURSE
  2391          _OverMaxDepth = types.MAX_RECURSE + 1
  2392          _UnderMaxDepth = types.MAX_RECURSE - 2
  2393      )
  2394      testcases := []struct {
  2395          name        string
  2396          data        string
  2397          errMaxDepth bool
  2398      }{
  2399          {
  2400              name:        "ArrayUnderMaxNestingDepth",
  2401              data:        `{"a":` + strings.Repeat(`[`, _UnderMaxDepth) + `0` + strings.Repeat(`]`, _UnderMaxDepth) + `}`,
  2402              errMaxDepth: false,
  2403          },
  2404          {
  2405              name:        "ArrayOverMaxNestingDepth",
  2406              data:        `{"a":` + strings.Repeat(`[`, _OverMaxDepth) + `0` + strings.Repeat(`]`, _OverMaxDepth) + `}`,
  2407              errMaxDepth: true,
  2408          },
  2409          {
  2410              name:        "ArrayOverStackDepth",
  2411              data:        `{"a":` + strings.Repeat(`[`, 3000000) + `0` + strings.Repeat(`]`, 3000000) + `}`,
  2412              errMaxDepth: true,
  2413          },
  2414          {
  2415              name:        "ObjectUnderMaxNestingDepth",
  2416              data:        `{"a":` + strings.Repeat(`{"a":`, _UnderMaxDepth) + `0` + strings.Repeat(`}`, _UnderMaxDepth) + `}`,
  2417              errMaxDepth: false,
  2418          },
  2419          {
  2420              name:        "ObjectOverMaxNestingDepth",
  2421              data:        `{"a":` + strings.Repeat(`{"a":`, _OverMaxDepth) + `0` + strings.Repeat(`}`, _OverMaxDepth) + `}`,
  2422              errMaxDepth: true,
  2423          },
  2424          {
  2425              name:        "ObjectOverStackDepth",
  2426              data:        `{"a":` + strings.Repeat(`{"a":`, 3000000) + `0` + strings.Repeat(`}`, 3000000) + `}`,
  2427              errMaxDepth: true,
  2428          },
  2429      }
  2430  
  2431      targets := []struct {
  2432          name     string
  2433          newValue func() interface{}
  2434      }{
  2435          {
  2436              name: "unstructured",
  2437              newValue: func() interface{} {
  2438                  var v interface{}
  2439                  return &v
  2440              },
  2441          },
  2442          {
  2443              name: "typed named field",
  2444              newValue: func() interface{} {
  2445                  v := struct {
  2446                      A interface{} `json:"a"`
  2447                  }{}
  2448                  return &v
  2449              },
  2450          },
  2451          {
  2452              name: "typed missing field",
  2453              newValue: func() interface{} {
  2454                  v := struct {
  2455                      B interface{} `json:"b"`
  2456                  }{}
  2457                  return &v
  2458              },
  2459          },
  2460          {
  2461              name: "custom unmarshaler",
  2462              newValue: func() interface{} {
  2463                  v := unmarshaler{}
  2464                  return &v
  2465              },
  2466          },
  2467      }
  2468  
  2469      for _, tc := range testcases {
  2470          for _, target := range targets {
  2471              t.Run(target.name+"-"+tc.name, func(t *testing.T) {
  2472                  err := Unmarshal([]byte(tc.data), target.newValue())
  2473                  if !tc.errMaxDepth {
  2474                      if err != nil {
  2475                          t.Errorf("unexpected error: %v", err)
  2476                      }
  2477                  } else {
  2478                      if err == nil {
  2479                          t.Errorf("expected error containing 'exceeded max depth', got none")
  2480                      } else if !strings.Contains(err.Error(), "exceeded max depth") {
  2481                          t.Errorf("expected error containing 'exceeded max depth', got: %v", err)
  2482                      }
  2483                  }
  2484              })
  2485          }
  2486      }
  2487  }
  2488  
  2489  // Issues: map value type larger than 128 bytes are stored by pointer
  2490  type ChargeToolPacingBucketItemTcc struct {
  2491      _ [128]byte
  2492      T string `json:"T"`
  2493  }
  2494  
  2495  type ChargeToolPacingParamsForDataRead struct {
  2496      Bucket2Item map[int64]ChargeToolPacingBucketItemTcc `json:"bucket_to_item"`
  2497  }
  2498  
  2499  var panicStr = `
  2500  {
  2501      "bucket_to_item": {
  2502          "102" : {
  2503              "T": "xxxx"
  2504          }
  2505      }
  2506  }
  2507  `
  2508  
  2509  func TestChangeTool(t *testing.T) {
  2510      dataForRaw := ChargeToolPacingParamsForDataRead{}
  2511  
  2512      err := Unmarshal([]byte(panicStr), &dataForRaw)
  2513      if err != nil {
  2514          t.Fatalf("err %+v\n", err)
  2515      }
  2516      t.Logf("%#v\n", dataForRaw)
  2517      t.Logf("%#v\n", &dataForRaw.Bucket2Item)
  2518      a := dataForRaw.Bucket2Item[102]
  2519      if a.T != "xxxx" {
  2520          t.Fatalf("exp:%v, got:%v", "xxxx", a.T)
  2521      }
  2522  
  2523  }
  2524  
  2525  func TestDecoder_LongestInvalidUtf8(t *testing.T) {
  2526      for _, data := range([]string{
  2527          "\"" + strings.Repeat("\x80", 4096) + "\"",
  2528          "\"" + strings.Repeat("\x80", 4095) + "\"",
  2529          "\"" + strings.Repeat("\x80", 4097) + "\"",
  2530          "\"" + strings.Repeat("\x80", 12345) + "\"",
  2531      }) {
  2532          testDecodeInvalidUtf8(t, []byte(data))
  2533      }
  2534  }
  2535  
  2536  func testDecodeInvalidUtf8(t *testing.T, data []byte) {
  2537      var sgot, jgot string
  2538      serr := ConfigStd.Unmarshal(data, &sgot)
  2539      jerr := json.Unmarshal(data, &jgot)
  2540      assert.Equal(t, serr != nil, jerr != nil)
  2541      if jerr == nil {
  2542          assert.Equal(t, sgot, jgot)
  2543      }
  2544  }
  2545  
  2546  func needEscape(b byte) bool {
  2547      return b == '"' || b == '\\' || b < '\x20'
  2548  }
  2549  
  2550  func genRandJsonBytes(length int) []byte {
  2551      var buf bytes.Buffer
  2552      buf.WriteByte('"')
  2553      for j := 0; j < length; j++ {
  2554          r := rand.Intn(0xff + 1)
  2555          if needEscape(byte(r)) {
  2556              buf.WriteByte('\\')
  2557          }
  2558          buf.WriteByte(byte(r))
  2559      }
  2560      buf.WriteByte('"')
  2561      return buf.Bytes()
  2562  }
  2563  
  2564  func genRandJsonRune(length int) []byte {
  2565      var buf bytes.Buffer
  2566      buf.WriteByte('"')
  2567      for j := 0; j < length; j++ {
  2568          r := rand.Intn(0x10FFFF + 1)
  2569          if r < 0x80 && needEscape(byte(r)) {
  2570              buf.WriteByte('\\')
  2571              buf.WriteByte(byte(r))
  2572          } else {
  2573              buf.WriteRune(rune(r))
  2574          }
  2575      }
  2576      buf.WriteByte('"')
  2577      return buf.Bytes()
  2578  }
  2579  
  2580  func TestDecoder_RandomInvalidUtf8(t *testing.T) {
  2581      nums   := 1000
  2582      maxLen := 1000
  2583      for i := 0; i < nums; i++ {
  2584          length := rand.Intn(maxLen)
  2585          testDecodeInvalidUtf8(t, genRandJsonBytes(length))
  2586          testDecodeInvalidUtf8(t, genRandJsonRune(length))
  2587      }
  2588  }
  2589  
  2590  
  2591  type atofTest struct {
  2592      in  string
  2593      out string
  2594      err error
  2595  }
  2596  
  2597  // Tests from Go strconv package, https://github.com/golang/go/blob/master/src/strconv/atof_test.go
  2598  // All tests are passed in Go encoding/json.
  2599  var atoftests = []atofTest{
  2600      {"1.234e", "", nil}, // error
  2601      {"1i", "1", nil},    // pass
  2602      {"1", "1", nil},
  2603      {"1e23", "1e+23", nil},
  2604      {"1E23", "1e+23", nil},
  2605      {"100000000000000000000000", "1e+23", nil},
  2606      {"1e-100", "1e-100", nil},
  2607      {"123456700", "1.234567e+08", nil},
  2608      {"99999999999999974834176", "9.999999999999997e+22", nil},
  2609      {"100000000000000000000001", "1.0000000000000001e+23", nil},
  2610      {"100000000000000008388608", "1.0000000000000001e+23", nil},
  2611      {"100000000000000016777215", "1.0000000000000001e+23", nil},
  2612      {"100000000000000016777216", "1.0000000000000003e+23", nil},
  2613      {"-1", "-1", nil},
  2614      {"-0.1", "-0.1", nil},
  2615      {"-0", "-0", nil},
  2616      {"1e-20", "1e-20", nil},
  2617      {"625e-3", "0.625", nil},
  2618  
  2619      // zeros
  2620      {"0", "0", nil},
  2621      {"0e0", "0", nil},
  2622      {"-0e0", "-0", nil},
  2623      {"0e-0", "0", nil},
  2624      {"-0e-0", "-0", nil},
  2625      {"0e+0", "0", nil},
  2626      {"-0e+0", "-0", nil},
  2627      {"0e+01234567890123456789", "0", nil},
  2628      {"0.00e-01234567890123456789", "0", nil},
  2629      {"-0e+01234567890123456789", "-0", nil},
  2630      {"-0.00e-01234567890123456789", "-0", nil},
  2631  
  2632      {"0e291", "0", nil}, // issue 15364
  2633      {"0e292", "0", nil}, // issue 15364
  2634      {"0e347", "0", nil}, // issue 15364
  2635      {"0e348", "0", nil}, // issue 15364
  2636      {"-0e291", "-0", nil},
  2637      {"-0e292", "-0", nil},
  2638      {"-0e347", "-0", nil},
  2639      {"-0e348", "-0", nil},
  2640  
  2641      // largest float64
  2642      {"1.7976931348623157e308", "1.7976931348623157e+308", nil},
  2643      {"-1.7976931348623157e308", "-1.7976931348623157e+308", nil},
  2644  
  2645      // the border is ...158079
  2646      // borderline - okay
  2647      {"1.7976931348623158e308", "1.7976931348623157e+308", nil},
  2648      {"-1.7976931348623158e308", "-1.7976931348623157e+308", nil},
  2649  
  2650      // a little too large
  2651      {"1e308", "1e+308", nil},
  2652  
  2653      // denormalized
  2654      {"1e-305", "1e-305", nil},
  2655      {"1e-306", "1e-306", nil},
  2656      {"1e-307", "1e-307", nil},
  2657      {"1e-308", "1e-308", nil},
  2658      {"1e-309", "1e-309", nil},
  2659      {"1e-310", "1e-310", nil},
  2660      {"1e-322", "1e-322", nil},
  2661      // smallest denormal
  2662      {"5e-324", "5e-324", nil},
  2663      {"4e-324", "5e-324", nil},
  2664      {"3e-324", "5e-324", nil},
  2665      // too small
  2666      {"2e-324", "0", nil},
  2667      // way too small
  2668      {"1e-350", "0", nil},
  2669      {"1e-400000", "0", nil},
  2670  
  2671      // try to overflow exponent
  2672      {"1e-4294967296", "0", nil},
  2673      {"1e-18446744073709551616", "0", nil},
  2674  
  2675      // https://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
  2676      {"2.2250738585072012e-308", "2.2250738585072014e-308", nil},
  2677      // https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
  2678      {"2.2250738585072011e-308", "2.225073858507201e-308", nil},
  2679  
  2680      // A very large number (initially wrongly parsed by the fast algorithm).
  2681      {"4.630813248087435e+307", "4.630813248087435e+307", nil},
  2682  
  2683      // A different kind of very large number.
  2684      {"22.222222222222222", "22.22222222222222", nil},
  2685      {"2." + strings.Repeat("2", 800) + "e+1", "22.22222222222222", nil},
  2686  
  2687      // Exactly halfway between 1 and math.Nextafter(1, 2).
  2688      // Round to even (down).
  2689      {"1.00000000000000011102230246251565404236316680908203125", "1", nil},
  2690      // Slightly lower; still round down.
  2691      {"1.00000000000000011102230246251565404236316680908203124", "1", nil},
  2692      // Slightly higher; round up.
  2693      {"1.00000000000000011102230246251565404236316680908203126", "1.0000000000000002", nil},
  2694      // Slightly higher, but you have to read all the way to the end.
  2695      {"1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1", "1.0000000000000002", nil},
  2696  
  2697      // Halfway between x := math.Nextafter(1, 2) and math.Nextafter(x, 2)
  2698      // Round to even (up).
  2699      {"1.00000000000000033306690738754696212708950042724609375", "1.0000000000000004", nil},
  2700  
  2701      // Halfway between 1090544144181609278303144771584 and 1090544144181609419040633126912
  2702      // (15497564393479157p+46, should round to even 15497564393479156p+46, issue 36657)
  2703      {"1090544144181609348671888949248", "1.0905441441816093e+30", nil},
  2704  
  2705      // Corner case between int64 and float64 for the input
  2706      {"9223372036854775807", "9223372036854775807", nil}, // max int64: (1 << 63) - 1
  2707      {"9223372036854775808", "9223372036854775808", nil},
  2708      {"-9223372036854775808", "-9223372036854775808", nil}, // min int64: 1 << 63
  2709      {"-9223372036854775809", "-9223372036854775809", nil},
  2710  }
  2711  
  2712  func TestDecodeFloat(t *testing.T) {
  2713      for i, tt := range atoftests {
  2714          // default float64
  2715          var sonicout, stdout float64
  2716          sonicerr := decoder.NewDecoder(tt.in).Decode(&sonicout)
  2717          stderr := json.NewDecoder(strings.NewReader(tt.in)).Decode(&stdout)
  2718          if !reflect.DeepEqual(sonicout, stdout) {
  2719              t.Fatalf("Test %d, %#v\ngot:\n   %#v\nexp:\n   %#v\n", i, tt.in, sonicout, stdout)
  2720          }
  2721          if !reflect.DeepEqual(sonicerr == nil, stderr == nil) {
  2722              t.Fatalf("Test %d, %#v\ngot:\n   %#v\nexp:\n   %#v\n", i, tt.in, sonicerr, stderr)
  2723          }
  2724      }
  2725  }
  2726  
  2727  
  2728  type useInt64Test struct {
  2729      in  string
  2730      out int64
  2731  }
  2732  
  2733  type useFloatTest struct {
  2734      in  string
  2735      out float64
  2736  }
  2737  
  2738  var useinttest = []useInt64Test{
  2739      // int64
  2740      {"0", 0},
  2741      {"1", 1},
  2742      {"-1", -1},
  2743      {"100", 100},
  2744  
  2745      {"-9223372036854775807", -9223372036854775807},
  2746      {"-9223372036854775808", -9223372036854775808}, //min int64
  2747      {"9223372036854775807", 9223372036854775807},   //max int64
  2748      {"9223372036854775806", 9223372036854775806},
  2749  }
  2750  
  2751  var usefloattest = []useFloatTest{
  2752      // float64
  2753      {"-9223372036854775809", -9223372036854775809}, // int64 overflow
  2754      {"9223372036854775808", 9223372036854775808},   // int64 overflow
  2755      {"1e2", 1e2},
  2756      {"1e-20", 1e-20},
  2757      {"1.0", 1},
  2758  }
  2759  
  2760  func TestUseInt64(t *testing.T) {
  2761      for i, tt := range useinttest {
  2762          var sout interface{}
  2763          dc := decoder.NewDecoder(tt.in)
  2764          dc.UseInt64()
  2765          serr := dc.Decode(&sout)
  2766          if !reflect.DeepEqual(sout, tt.out) {
  2767              t.Errorf("Test %d, %#v\ngot:\n   %#v\nexp:\n   %#v\n", i, tt.in, sout, tt.in)
  2768          }
  2769          if serr != nil {
  2770              t.Errorf("Test %d, %#v\ngot:\n   %#v\nexp:\n   nil\n", i, tt, serr)
  2771          }
  2772      }
  2773  
  2774      for i, tt := range usefloattest {
  2775          var sout interface{}
  2776          dc := decoder.NewDecoder(tt.in)
  2777          dc.UseInt64()
  2778          //the input string is not int64, still return float64
  2779          serr := dc.Decode(&sout)
  2780          if !reflect.DeepEqual(sout, tt.out) {
  2781              t.Errorf("Test %d, %#v\ngot:\n   %#v\nexp:\n   %#v\n", i, tt.in, sout, tt.in)
  2782          }
  2783          if serr != nil {
  2784              t.Errorf("Test %d, %#v\ngot:\n   %#v\nexp:\n   nil\n", i, tt, serr)
  2785          }
  2786      }
  2787  }
  2788  
  2789  func TestUseNumber(t *testing.T) {
  2790      for i, tt := range useinttest {
  2791          var sout interface{}
  2792          dc := decoder.NewDecoder(tt.in)
  2793          dc.UseNumber()
  2794          serr := dc.Decode(&sout)
  2795          if !reflect.DeepEqual(sout, json.Number(tt.in)) {
  2796              t.Errorf("Test %d, %#v\ngot:\n   %#v\nexp:\n   %#v\n", i, tt.in, sout, tt.out)
  2797          }
  2798          if serr != nil {
  2799              t.Errorf("Test %d, %#v\ngot:\n   %#v\nexp:\n   nil\n", i, tt, serr)
  2800          }
  2801      }
  2802  
  2803      for i, tt := range usefloattest {
  2804          var sout interface{}
  2805          dc := decoder.NewDecoder(tt.in)
  2806          dc.UseNumber()
  2807          serr := dc.Decode(&sout)
  2808          if !reflect.DeepEqual(sout, json.Number(tt.in)) {
  2809              t.Errorf("Test %d, %#v\ngot:\n   %#v\nexp:\n   %#v\n", i, tt.in, sout, tt.out)
  2810          }
  2811          if serr != nil {
  2812              t.Errorf("Test %d, %#v\ngot:\n   %#v\nexp:\n   nil\n", i, tt, serr)
  2813          }
  2814      }
  2815  }