1
0
mirror of https://github.com/json-iterator/go.git synced 2024-12-12 09:03:48 +02:00
json-iterator/jsoniter_object_test.go

97 lines
1.8 KiB
Go
Raw Normal View History

2016-12-01 10:11:07 +02:00
package jsoniter
import (
"testing"
"encoding/json"
2016-12-06 08:23:59 +02:00
"fmt"
2016-12-01 10:11:07 +02:00
)
func Test_empty_object(t *testing.T) {
iter := ParseString(`{}`)
field := iter.ReadObject()
if field != "" {
t.Fatal(field)
}
}
func Test_one_field(t *testing.T) {
iter := ParseString(`{"a": "b"}`)
field := iter.ReadObject()
if field != "a" {
2016-12-06 08:23:59 +02:00
fmt.Println(iter.Error)
2016-12-01 10:11:07 +02:00
t.Fatal(field)
}
value := iter.ReadString()
if value != "b" {
t.Fatal(field)
}
field = iter.ReadObject()
if field != "" {
t.Fatal(field)
}
}
func Test_two_field(t *testing.T) {
iter := ParseString(`{ "a": "b" , "c": "d" }`)
field := iter.ReadObject()
if field != "a" {
t.Fatal(field)
}
value := iter.ReadString()
if value != "b" {
t.Fatal(field)
}
field = iter.ReadObject()
if field != "c" {
t.Fatal(field)
}
value = iter.ReadString()
if value != "d" {
t.Fatal(field)
}
field = iter.ReadObject()
if field != "" {
t.Fatal(field)
}
iter = ParseString(`{"field1": "1", "field2": 2}`)
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
switch field {
case "field1":
iter.ReadString()
case "field2":
iter.ReadInt64()
default:
iter.ReportError("bind object", "unexpected field")
}
}
}
type TestObj struct {
Field1 string
Field2 uint64
}
func Benchmark_jsoniter_object(b *testing.B) {
for n := 0; n < b.N; n++ {
2016-12-06 15:10:09 +02:00
iter := ParseString(`{"field1": "1", "field2": 2}`)
obj := TestObj{}
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
switch field {
case "field1":
obj.Field1 = iter.ReadString()
case "field2":
obj.Field2 = iter.ReadUint64()
default:
iter.ReportError("bind object", "unexpected field")
}
}
2016-12-04 05:06:38 +02:00
}
}
2016-12-06 15:10:09 +02:00
2016-12-01 10:11:07 +02:00
func Benchmark_json_object(b *testing.B) {
for n := 0; n < b.N; n++ {
result := TestObj{}
json.Unmarshal([]byte(`{"field1": "1", "field2": 2}`), &result)
}
}