github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/decode_test.go (about)

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