...
Run Format

Source file src/encoding/json/decode_test.go

Documentation: encoding/json

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

View as plain text