-
Notifications
You must be signed in to change notification settings - Fork 498
/
simplejson_reader_test.go
127 lines (115 loc) · 2.79 KB
/
simplejson_reader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package simplejson
import (
"bytes"
"encoding/json"
"reflect"
"strconv"
"testing"
)
func TestNewFromReader(t *testing.T) {
//Use New Constructor
buf := bytes.NewBuffer([]byte(`{
"test": {
"array": [1, "2", 3],
"arraywithsubs": [
{"subkeyone": 1},
{"subkeytwo": 2, "subkeythree": 3}
],
"bignum": 9223372036854775807,
"uint64": 18446744073709551615
}
}`))
js, err := NewFromReader(buf)
//Standard Test Case
if js == nil {
t.Fatal("got nil")
}
if err != nil {
t.Fatalf("got err %#v", err)
}
arr, _ := js.Get("test").Get("array").Array()
if arr == nil {
t.Fatal("got nil")
}
for i, v := range arr {
var iv int
switch v.(type) {
case json.Number:
i64, err := v.(json.Number).Int64()
if err != nil {
t.Fatalf("got err %#v", err)
}
iv = int(i64)
case string:
iv, _ = strconv.Atoi(v.(string))
}
if iv != i+1 {
t.Errorf("got %#v expected %#v", iv, i+1)
}
}
if ma := js.Get("test").Get("array").MustArray(); !reflect.DeepEqual(ma, []interface{}{json.Number("1"), "2", json.Number("3")}) {
t.Errorf("got %#v", ma)
}
mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap()
if !reflect.DeepEqual(mm, map[string]interface{}{"subkeyone": json.Number("1")}) {
t.Errorf("got %#v", mm)
}
if n := js.Get("test").Get("bignum").MustInt64(); n != int64(9223372036854775807) {
t.Errorf("got %#v", n)
}
if n := js.Get("test").Get("uint64").MustUint64(); n != uint64(18446744073709551615) {
t.Errorf("got %#v", n)
}
}
func TestSimplejsonGo11(t *testing.T) {
js, err := NewJson([]byte(`{
"test": {
"array": [1, "2", 3],
"arraywithsubs": [
{"subkeyone": 1},
{"subkeytwo": 2, "subkeythree": 3}
],
"bignum": 9223372036854775807,
"uint64": 18446744073709551615
}
}`))
if js == nil {
t.Fatal("got nil")
}
if err != nil {
t.Fatalf("got err %#v", err)
}
arr, _ := js.Get("test").Get("array").Array()
if arr == nil {
t.Fatal("got nil")
}
for i, v := range arr {
var iv int
switch v.(type) {
case json.Number:
i64, err := v.(json.Number).Int64()
if err != nil {
t.Fatalf("got err %#v", err)
}
iv = int(i64)
case string:
iv, _ = strconv.Atoi(v.(string))
}
if iv != i+1 {
t.Errorf("got %#v expected %#v", iv, i+1)
}
}
if ma := js.Get("test").Get("array").MustArray(); !reflect.DeepEqual(ma, []interface{}{json.Number("1"), "2", json.Number("3")}) {
t.Errorf("got %#v", ma)
}
mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap()
if !reflect.DeepEqual(mm, map[string]interface{}{"subkeyone": json.Number("1")}) {
t.Errorf("got %#v", mm)
}
if n := js.Get("test").Get("bignum").MustInt64(); n != int64(9223372036854775807) {
t.Errorf("got %#v", n)
}
if n := js.Get("test").Get("uint64").MustUint64(); n != uint64(18446744073709551615) {
t.Errorf("got %#v", n)
}
}