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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var optionalsExpected = `{
|
|
|
|
"sr": "",
|
|
|
|
"omitempty": 0,
|
2011-12-02 20:34:41 +01:00
|
|
|
"slr": null,
|
2011-09-16 17:47:21 +02:00
|
|
|
"mr": {}
|
|
|
|
}`
|
|
|
|
|
|
|
|
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
|
|
|
|
err = NewDecoder(bytes.NewBuffer(got)).Decode(&s2)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|