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