github.com/segmentio/encoding@v0.4.0/json/golang_decode_test.go (about)

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