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