2010-12-03 05:34:57 +01:00
|
|
|
|
// Copyright 2010 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 (
|
|
|
|
|
"bytes"
|
2013-01-30 02:37:13 +01:00
|
|
|
|
"io/ioutil"
|
2012-10-23 06:31:11 +02:00
|
|
|
|
"net"
|
2010-12-03 05:34:57 +01:00
|
|
|
|
"reflect"
|
2013-01-30 02:37:13 +01:00
|
|
|
|
"strings"
|
2010-12-03 05:34:57 +01:00
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Test values for the stream test.
|
|
|
|
|
// One of each JSON kind.
|
|
|
|
|
var streamTest = []interface{}{
|
2011-01-21 19:19:03 +01:00
|
|
|
|
0.1,
|
2010-12-03 05:34:57 +01:00
|
|
|
|
"hello",
|
|
|
|
|
nil,
|
|
|
|
|
true,
|
|
|
|
|
false,
|
|
|
|
|
[]interface{}{"a", "b", "c"},
|
|
|
|
|
map[string]interface{}{"K": "Kelvin", "ß": "long s"},
|
2011-01-21 19:19:03 +01:00
|
|
|
|
3.14, // another value to make sure something can follow map
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var streamEncoded = `0.1
|
|
|
|
|
"hello"
|
|
|
|
|
null
|
|
|
|
|
true
|
|
|
|
|
false
|
|
|
|
|
["a","b","c"]
|
|
|
|
|
{"ß":"long s","K":"Kelvin"}
|
|
|
|
|
3.14
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func TestEncoder(t *testing.T) {
|
|
|
|
|
for i := 0; i <= len(streamTest); i++ {
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
enc := NewEncoder(&buf)
|
|
|
|
|
for j, v := range streamTest[0:i] {
|
|
|
|
|
if err := enc.Encode(v); err != nil {
|
|
|
|
|
t.Fatalf("encode #%d: %v", j, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if have, want := buf.String(), nlines(streamEncoded, i); have != want {
|
|
|
|
|
t.Errorf("encoding %d items: mismatch", i)
|
|
|
|
|
diff(t, []byte(have), []byte(want))
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDecoder(t *testing.T) {
|
|
|
|
|
for i := 0; i <= len(streamTest); i++ {
|
|
|
|
|
// Use stream without newlines as input,
|
|
|
|
|
// just to stress the decoder even more.
|
|
|
|
|
// Our test input does not include back-to-back numbers.
|
|
|
|
|
// Otherwise stripping the newlines would
|
|
|
|
|
// merge two adjacent JSON values.
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
for _, c := range nlines(streamEncoded, i) {
|
|
|
|
|
if c != '\n' {
|
|
|
|
|
buf.WriteRune(c)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out := make([]interface{}, i)
|
|
|
|
|
dec := NewDecoder(&buf)
|
|
|
|
|
for j := range out {
|
|
|
|
|
if err := dec.Decode(&out[j]); err != nil {
|
|
|
|
|
t.Fatalf("decode #%d/%d: %v", j, i, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !reflect.DeepEqual(out, streamTest[0:i]) {
|
2011-01-21 19:19:03 +01:00
|
|
|
|
t.Errorf("decoding %d items: mismatch", i)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
for j := range out {
|
|
|
|
|
if !reflect.DeepEqual(out[j], streamTest[j]) {
|
2011-01-21 19:19:03 +01:00
|
|
|
|
t.Errorf("#%d: have %v want %v", j, out[j], streamTest[j])
|
2010-12-03 05:34:57 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-30 02:37:13 +01:00
|
|
|
|
func TestDecoderBuffered(t *testing.T) {
|
|
|
|
|
r := strings.NewReader(`{"Name": "Gopher"} extra `)
|
|
|
|
|
var m struct {
|
|
|
|
|
Name string
|
|
|
|
|
}
|
|
|
|
|
d := NewDecoder(r)
|
|
|
|
|
err := d.Decode(&m)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if m.Name != "Gopher" {
|
|
|
|
|
t.Errorf("Name = %q; want Gopher", m.Name)
|
|
|
|
|
}
|
|
|
|
|
rest, err := ioutil.ReadAll(d.Buffered())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if g, w := string(rest), " extra "; g != w {
|
|
|
|
|
t.Errorf("Remaining = %q; want %q", g, w)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
|
func nlines(s string, n int) string {
|
|
|
|
|
if n <= 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
for i, c := range s {
|
|
|
|
|
if c == '\n' {
|
|
|
|
|
if n--; n == 0 {
|
|
|
|
|
return s[0 : i+1]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRawMessage(t *testing.T) {
|
|
|
|
|
// TODO(rsc): Should not need the * in *RawMessage
|
|
|
|
|
var data struct {
|
|
|
|
|
X float64
|
|
|
|
|
Id *RawMessage
|
|
|
|
|
Y float32
|
|
|
|
|
}
|
|
|
|
|
const raw = `["\u0056",null]`
|
|
|
|
|
const msg = `{"X":0.1,"Id":["\u0056",null],"Y":0.2}`
|
|
|
|
|
err := Unmarshal([]byte(msg), &data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Unmarshal: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if string([]byte(*data.Id)) != raw {
|
|
|
|
|
t.Fatalf("Raw mismatch: have %#q want %#q", []byte(*data.Id), raw)
|
|
|
|
|
}
|
|
|
|
|
b, err := Marshal(&data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Marshal: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if string(b) != msg {
|
|
|
|
|
t.Fatalf("Marshal: have %#q want %#q", b, msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
|
|
|
|
|
|
func TestNullRawMessage(t *testing.T) {
|
|
|
|
|
// TODO(rsc): Should not need the * in *RawMessage
|
|
|
|
|
var data struct {
|
|
|
|
|
X float64
|
|
|
|
|
Id *RawMessage
|
|
|
|
|
Y float32
|
|
|
|
|
}
|
|
|
|
|
data.Id = new(RawMessage)
|
|
|
|
|
const msg = `{"X":0.1,"Id":null,"Y":0.2}`
|
|
|
|
|
err := Unmarshal([]byte(msg), &data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Unmarshal: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if data.Id != nil {
|
|
|
|
|
t.Fatalf("Raw mismatch: have non-nil, want nil")
|
|
|
|
|
}
|
|
|
|
|
b, err := Marshal(&data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Marshal: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if string(b) != msg {
|
|
|
|
|
t.Fatalf("Marshal: have %#q want %#q", b, msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
|
|
var blockingTests = []string{
|
|
|
|
|
`{"x": 1}`,
|
|
|
|
|
`[1, 2, 3]`,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBlocking(t *testing.T) {
|
|
|
|
|
for _, enc := range blockingTests {
|
|
|
|
|
r, w := net.Pipe()
|
|
|
|
|
go w.Write([]byte(enc))
|
|
|
|
|
var val interface{}
|
|
|
|
|
|
|
|
|
|
// If Decode reads beyond what w.Write writes above,
|
|
|
|
|
// it will block, and the test will deadlock.
|
|
|
|
|
if err := NewDecoder(r).Decode(&val); err != nil {
|
|
|
|
|
t.Errorf("decoding %s: %v", enc, err)
|
|
|
|
|
}
|
|
|
|
|
r.Close()
|
|
|
|
|
w.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
|
|
|
|
|
|
func BenchmarkEncoderEncode(b *testing.B) {
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
type T struct {
|
|
|
|
|
X, Y string
|
|
|
|
|
}
|
|
|
|
|
v := &T{"foo", "bar"}
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
if err := NewEncoder(ioutil.Discard).Encode(v); err != nil {
|
|
|
|
|
b.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|