2011-09-16 17:47:21 +02:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2011-09-22 06:47:32 +02:00
|
|
|
"bytes"
|
2012-01-25 21:56:26 +01:00
|
|
|
"math"
|
2011-09-22 06:47:32 +02:00
|
|
|
"reflect"
|
2011-09-16 17:47:21 +02:00
|
|
|
"testing"
|
2013-11-06 20:49:01 +01:00
|
|
|
"unicode"
|
2011-09-16 17:47:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Optionals struct {
|
|
|
|
Sr string `json:"sr"`
|
|
|
|
So string `json:"so,omitempty"`
|
2011-10-27 01:57:58 +02:00
|
|
|
Sw string `json:"-"`
|
2011-09-16 17:47:21 +02:00
|
|
|
|
|
|
|
Ir int `json:"omitempty"` // actually named omitempty, not an option
|
|
|
|
Io int `json:"io,omitempty"`
|
|
|
|
|
|
|
|
Slr []string `json:"slr,random"`
|
|
|
|
Slo []string `json:"slo,omitempty"`
|
|
|
|
|
|
|
|
Mr map[string]interface{} `json:"mr"`
|
|
|
|
Mo map[string]interface{} `json:",omitempty"`
|
2014-06-05 01:15:33 +02:00
|
|
|
|
|
|
|
Fr float64 `json:"fr"`
|
|
|
|
Fo float64 `json:"fo,omitempty"`
|
|
|
|
|
|
|
|
Br bool `json:"br"`
|
|
|
|
Bo bool `json:"bo,omitempty"`
|
|
|
|
|
|
|
|
Ur uint `json:"ur"`
|
|
|
|
Uo uint `json:"uo,omitempty"`
|
|
|
|
|
|
|
|
Str struct{} `json:"str"`
|
|
|
|
Sto struct{} `json:"sto,omitempty"`
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var optionalsExpected = `{
|
|
|
|
"sr": "",
|
|
|
|
"omitempty": 0,
|
2011-12-02 20:34:41 +01:00
|
|
|
"slr": null,
|
2014-06-05 01:15:33 +02:00
|
|
|
"mr": {},
|
|
|
|
"fr": 0,
|
|
|
|
"br": false,
|
|
|
|
"ur": 0,
|
|
|
|
"str": {},
|
|
|
|
"sto": {}
|
2011-09-16 17:47:21 +02:00
|
|
|
}`
|
|
|
|
|
|
|
|
func TestOmitEmpty(t *testing.T) {
|
|
|
|
var o Optionals
|
2011-10-27 01:57:58 +02:00
|
|
|
o.Sw = "something"
|
2011-09-16 17:47:21 +02:00
|
|
|
o.Mr = map[string]interface{}{}
|
|
|
|
o.Mo = map[string]interface{}{}
|
|
|
|
|
|
|
|
got, err := MarshalIndent(&o, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got := string(got); got != optionalsExpected {
|
|
|
|
t.Errorf(" got: %s\nwant: %s\n", got, optionalsExpected)
|
|
|
|
}
|
|
|
|
}
|
2011-09-22 06:47:32 +02:00
|
|
|
|
|
|
|
type StringTag struct {
|
|
|
|
BoolStr bool `json:",string"`
|
|
|
|
IntStr int64 `json:",string"`
|
|
|
|
StrStr string `json:",string"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var stringTagExpected = `{
|
|
|
|
"BoolStr": "true",
|
|
|
|
"IntStr": "42",
|
|
|
|
"StrStr": "\"xzbit\""
|
|
|
|
}`
|
|
|
|
|
|
|
|
func TestStringTag(t *testing.T) {
|
|
|
|
var s StringTag
|
|
|
|
s.BoolStr = true
|
|
|
|
s.IntStr = 42
|
|
|
|
s.StrStr = "xzbit"
|
|
|
|
got, err := MarshalIndent(&s, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got := string(got); got != stringTagExpected {
|
|
|
|
t.Fatalf(" got: %s\nwant: %s\n", got, stringTagExpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that it round-trips.
|
|
|
|
var s2 StringTag
|
2014-06-07 00:37:27 +02:00
|
|
|
err = NewDecoder(bytes.NewReader(got)).Decode(&s2)
|
2011-09-22 06:47:32 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Decode: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(s, s2) {
|
|
|
|
t.Fatalf("decode didn't match.\nsource: %#v\nEncoded as:\n%s\ndecode: %#v", s, string(got), s2)
|
|
|
|
}
|
|
|
|
}
|
2012-01-12 02:31:45 +01:00
|
|
|
|
|
|
|
// byte slices are special even if they're renamed types.
|
|
|
|
type renamedByte byte
|
|
|
|
type renamedByteSlice []byte
|
|
|
|
type renamedRenamedByteSlice []renamedByte
|
|
|
|
|
|
|
|
func TestEncodeRenamedByteSlice(t *testing.T) {
|
|
|
|
s := renamedByteSlice("abc")
|
|
|
|
result, err := Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expect := `"YWJj"`
|
|
|
|
if string(result) != expect {
|
|
|
|
t.Errorf(" got %s want %s", result, expect)
|
|
|
|
}
|
|
|
|
r := renamedRenamedByteSlice("abc")
|
|
|
|
result, err = Marshal(r)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(result) != expect {
|
|
|
|
t.Errorf(" got %s want %s", result, expect)
|
|
|
|
}
|
|
|
|
}
|
2012-01-25 21:56:26 +01:00
|
|
|
|
|
|
|
var unsupportedValues = []interface{}{
|
|
|
|
math.NaN(),
|
|
|
|
math.Inf(-1),
|
|
|
|
math.Inf(1),
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnsupportedValues(t *testing.T) {
|
|
|
|
for _, v := range unsupportedValues {
|
|
|
|
if _, err := Marshal(v); err != nil {
|
|
|
|
if _, ok := err.(*UnsupportedValueError); !ok {
|
|
|
|
t.Errorf("for %v, got %T want UnsupportedValueError", v, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Errorf("for %v, expected error", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
|
|
|
|
// Ref has Marshaler and Unmarshaler methods with pointer receiver.
|
|
|
|
type Ref int
|
|
|
|
|
|
|
|
func (*Ref) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"ref"`), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Ref) UnmarshalJSON([]byte) error {
|
|
|
|
*r = 12
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Val has Marshaler methods with value receiver.
|
|
|
|
type Val int
|
|
|
|
|
|
|
|
func (Val) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"val"`), nil
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// RefText has Marshaler and Unmarshaler methods with pointer receiver.
|
|
|
|
type RefText int
|
|
|
|
|
|
|
|
func (*RefText) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(`"ref"`), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RefText) UnmarshalText([]byte) error {
|
|
|
|
*r = 13
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValText has Marshaler methods with value receiver.
|
|
|
|
type ValText int
|
|
|
|
|
|
|
|
func (ValText) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(`"val"`), nil
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
func TestRefValMarshal(t *testing.T) {
|
|
|
|
var s = struct {
|
|
|
|
R0 Ref
|
|
|
|
R1 *Ref
|
2013-11-06 20:49:01 +01:00
|
|
|
R2 RefText
|
|
|
|
R3 *RefText
|
2012-02-09 09:19:58 +01:00
|
|
|
V0 Val
|
|
|
|
V1 *Val
|
2013-11-06 20:49:01 +01:00
|
|
|
V2 ValText
|
|
|
|
V3 *ValText
|
2012-02-09 09:19:58 +01:00
|
|
|
}{
|
|
|
|
R0: 12,
|
|
|
|
R1: new(Ref),
|
2013-11-06 20:49:01 +01:00
|
|
|
R2: 14,
|
|
|
|
R3: new(RefText),
|
2012-02-09 09:19:58 +01:00
|
|
|
V0: 13,
|
|
|
|
V1: new(Val),
|
2013-11-06 20:49:01 +01:00
|
|
|
V2: 15,
|
|
|
|
V3: new(ValText),
|
2012-02-09 09:19:58 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
const want = `{"R0":"ref","R1":"ref","R2":"\"ref\"","R3":"\"ref\"","V0":"val","V1":"val","V2":"\"val\"","V3":"\"val\""}`
|
2012-02-09 09:19:58 +01:00
|
|
|
b, err := Marshal(&s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Marshal: %v", err)
|
|
|
|
}
|
|
|
|
if got := string(b); got != want {
|
|
|
|
t.Errorf("got %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
2012-03-06 18:57:23 +01:00
|
|
|
|
|
|
|
// C implements Marshaler and returns unescaped JSON.
|
|
|
|
type C int
|
|
|
|
|
|
|
|
func (C) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"<&>"`), nil
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// CText implements Marshaler and returns unescaped text.
|
|
|
|
type CText int
|
|
|
|
|
|
|
|
func (CText) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(`"<&>"`), nil
|
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func TestMarshalerEscaping(t *testing.T) {
|
|
|
|
var c C
|
2013-11-06 20:49:01 +01:00
|
|
|
want := `"\u003c\u0026\u003e"`
|
2012-03-06 18:57:23 +01:00
|
|
|
b, err := Marshal(c)
|
|
|
|
if err != nil {
|
2013-11-06 20:49:01 +01:00
|
|
|
t.Fatalf("Marshal(c): %v", err)
|
2012-03-06 18:57:23 +01:00
|
|
|
}
|
|
|
|
if got := string(b); got != want {
|
2013-11-06 20:49:01 +01:00
|
|
|
t.Errorf("Marshal(c) = %#q, want %#q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ct CText
|
|
|
|
want = `"\"\u003c\u0026\u003e\""`
|
|
|
|
b, err = Marshal(ct)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Marshal(ct): %v", err)
|
|
|
|
}
|
|
|
|
if got := string(b); got != want {
|
|
|
|
t.Errorf("Marshal(ct) = %#q, want %#q", got, want)
|
2012-03-06 18:57:23 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
|
|
|
|
type IntType int
|
|
|
|
|
|
|
|
type MyStruct struct {
|
|
|
|
IntType
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAnonymousNonstruct(t *testing.T) {
|
|
|
|
var i IntType = 11
|
|
|
|
a := MyStruct{i}
|
|
|
|
const want = `{"IntType":11}`
|
|
|
|
|
|
|
|
b, err := Marshal(a)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Marshal: %v", err)
|
|
|
|
}
|
|
|
|
if got := string(b); got != want {
|
|
|
|
t.Errorf("got %q, want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
type BugA struct {
|
|
|
|
S string
|
|
|
|
}
|
|
|
|
|
|
|
|
type BugB struct {
|
|
|
|
BugA
|
|
|
|
S string
|
|
|
|
}
|
|
|
|
|
|
|
|
type BugC struct {
|
|
|
|
S string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Legal Go: We never use the repeated embedded field (S).
|
|
|
|
type BugX struct {
|
|
|
|
A int
|
|
|
|
BugA
|
|
|
|
BugB
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue 5245.
|
|
|
|
func TestEmbeddedBug(t *testing.T) {
|
|
|
|
v := BugB{
|
|
|
|
BugA{"A"},
|
|
|
|
"B",
|
|
|
|
}
|
|
|
|
b, err := Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Marshal:", err)
|
|
|
|
}
|
|
|
|
want := `{"S":"B"}`
|
|
|
|
got := string(b)
|
|
|
|
if got != want {
|
|
|
|
t.Fatalf("Marshal: got %s want %s", got, want)
|
|
|
|
}
|
|
|
|
// Now check that the duplicate field, S, does not appear.
|
|
|
|
x := BugX{
|
|
|
|
A: 23,
|
|
|
|
}
|
|
|
|
b, err = Marshal(x)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Marshal:", err)
|
|
|
|
}
|
|
|
|
want = `{"A":23}`
|
|
|
|
got = string(b)
|
|
|
|
if got != want {
|
|
|
|
t.Fatalf("Marshal: got %s want %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type BugD struct { // Same as BugA after tagging.
|
|
|
|
XXX string `json:"S"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// BugD's tagged S field should dominate BugA's.
|
|
|
|
type BugY struct {
|
|
|
|
BugA
|
|
|
|
BugD
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that a field with a tag dominates untagged fields.
|
|
|
|
func TestTaggedFieldDominates(t *testing.T) {
|
|
|
|
v := BugY{
|
|
|
|
BugA{"BugA"},
|
|
|
|
BugD{"BugD"},
|
|
|
|
}
|
|
|
|
b, err := Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Marshal:", err)
|
|
|
|
}
|
|
|
|
want := `{"S":"BugD"}`
|
|
|
|
got := string(b)
|
|
|
|
if got != want {
|
|
|
|
t.Fatalf("Marshal: got %s want %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are no tags here, so S should not appear.
|
|
|
|
type BugZ struct {
|
|
|
|
BugA
|
|
|
|
BugC
|
|
|
|
BugY // Contains a tagged S field through BugD; should not dominate.
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicatedFieldDisappears(t *testing.T) {
|
|
|
|
v := BugZ{
|
|
|
|
BugA{"BugA"},
|
|
|
|
BugC{"BugC"},
|
|
|
|
BugY{
|
|
|
|
BugA{"nested BugA"},
|
|
|
|
BugD{"nested BugD"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
b, err := Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Marshal:", err)
|
|
|
|
}
|
|
|
|
want := `{}`
|
|
|
|
got := string(b)
|
|
|
|
if got != want {
|
|
|
|
t.Fatalf("Marshal: got %s want %s", got, want)
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
func TestStringBytes(t *testing.T) {
|
|
|
|
// Test that encodeState.stringBytes and encodeState.string use the same encoding.
|
|
|
|
es := &encodeState{}
|
|
|
|
var r []rune
|
|
|
|
for i := '\u0000'; i <= unicode.MaxRune; i++ {
|
|
|
|
r = append(r, i)
|
|
|
|
}
|
|
|
|
s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
|
|
|
|
_, err := es.string(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
esBytes := &encodeState{}
|
|
|
|
_, err = esBytes.stringBytes([]byte(s))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := es.Buffer.String()
|
|
|
|
encBytes := esBytes.Buffer.String()
|
|
|
|
if enc != encBytes {
|
|
|
|
i := 0
|
|
|
|
for i < len(enc) && i < len(encBytes) && enc[i] == encBytes[i] {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
enc = enc[i:]
|
|
|
|
encBytes = encBytes[i:]
|
|
|
|
i = 0
|
|
|
|
for i < len(enc) && i < len(encBytes) && enc[len(enc)-i-1] == encBytes[len(encBytes)-i-1] {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
enc = enc[:len(enc)-i]
|
|
|
|
encBytes = encBytes[:len(encBytes)-i]
|
|
|
|
|
|
|
|
if len(enc) > 20 {
|
|
|
|
enc = enc[:20] + "..."
|
|
|
|
}
|
|
|
|
if len(encBytes) > 20 {
|
|
|
|
encBytes = encBytes[:20] + "..."
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Errorf("encodings differ at %#q vs %#q", enc, encBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssue6458(t *testing.T) {
|
|
|
|
type Foo struct {
|
|
|
|
M RawMessage
|
|
|
|
}
|
|
|
|
x := Foo{RawMessage(`"foo"`)}
|
|
|
|
|
|
|
|
b, err := Marshal(&x)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if want := `{"M":"foo"}`; string(b) != want {
|
|
|
|
t.Errorf("Marshal(&x) = %#q; want %#q", b, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = Marshal(x)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if want := `{"M":"ImZvbyI="}`; string(b) != want {
|
|
|
|
t.Errorf("Marshal(x) = %#q; want %#q", b, want)
|
|
|
|
}
|
|
|
|
}
|
2014-06-05 01:15:33 +02:00
|
|
|
|
|
|
|
func TestHTMLEscape(t *testing.T) {
|
|
|
|
var b, want bytes.Buffer
|
|
|
|
m := `{"M":"<html>foo &` + "\xe2\x80\xa8 \xe2\x80\xa9" + `</html>"}`
|
|
|
|
want.Write([]byte(`{"M":"\u003chtml\u003efoo \u0026\u2028 \u2029\u003c/html\u003e"}`))
|
|
|
|
HTMLEscape(&b, []byte(m))
|
|
|
|
if !bytes.Equal(b.Bytes(), want.Bytes()) {
|
|
|
|
t.Errorf("HTMLEscape(&b, []byte(m)) = %s; want %s", b.Bytes(), want.Bytes())
|
|
|
|
}
|
|
|
|
}
|