-
Notifications
You must be signed in to change notification settings - Fork 112
/
json_test.go
137 lines (102 loc) · 2.85 KB
/
json_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
128
129
130
131
132
133
134
135
136
137
package mxj
import (
"bytes"
"fmt"
"io"
"testing"
)
var jdata = []byte(`{ "key1":"string", "key2":34, "key3":true, "key4":"unsafe: <>&", "key5":null }`)
var jdata2 = []byte(`{ "key1":"string", "key2":34, "key3":true, "key4":"unsafe: <>&" },
{ "key":"value in new JSON string" }`)
func TestJsonHeader(t *testing.T) {
fmt.Println("\n---------------- json_test.go ...")
}
func TestNewMapJson(t *testing.T) {
m, merr := NewMapJson(jdata)
if merr != nil {
t.Fatal("NewMapJson, merr:", merr.Error())
}
fmt.Println("NewMapJson, jdata:", string(jdata))
fmt.Printf("NewMapJson, m : %#v\n", m)
}
func TestNewMapJsonNumber(t *testing.T) {
JsonUseNumber = true
m, merr := NewMapJson(jdata)
if merr != nil {
t.Fatal("NewMapJson, merr:", merr.Error())
}
fmt.Println("NewMapJson, jdata:", string(jdata))
fmt.Printf("NewMapJson, m : %#v\n", m)
JsonUseNumber = false
}
func TestNewMapJsonError(t *testing.T) {
m, merr := NewMapJson(jdata[:len(jdata)-2])
if merr == nil {
t.Fatal("NewMapJsonError, m:", m)
}
fmt.Println("NewMapJsonError, jdata :", string(jdata[:len(jdata)-2]))
fmt.Println("NewMapJsonError, merror:", merr.Error())
newData := []byte(`{ "this":"is", "in":error }`)
m, merr = NewMapJson(newData)
if merr == nil {
t.Fatal("NewMapJsonError, m:", m)
}
fmt.Println("NewMapJsonError, newData :", string(newData))
fmt.Println("NewMapJsonError, merror :", merr.Error())
}
func TestNewMapJsonReader(t *testing.T) {
rdr := bytes.NewBuffer(jdata2)
for {
m, jb, merr := NewMapJsonReaderRaw(rdr)
if merr != nil && merr != io.EOF {
t.Fatal("NewMapJsonReader, merr:", merr.Error())
}
if merr == io.EOF {
break
}
fmt.Println("NewMapJsonReader, jb:", string(jb))
fmt.Printf("NewMapJsonReader, m : %#v\n", m)
}
}
func TestNewMapJsonReaderNumber(t *testing.T) {
JsonUseNumber = true
rdr := bytes.NewBuffer(jdata2)
for {
m, jb, merr := NewMapJsonReaderRaw(rdr)
if merr != nil && merr != io.EOF {
t.Fatal("NewMapJsonReader, merr:", merr.Error())
}
if merr == io.EOF {
break
}
fmt.Println("NewMapJsonReader, jb:", string(jb))
fmt.Printf("NewMapJsonReader, m : %#v\n", m)
}
JsonUseNumber = false
}
func TestJson(t *testing.T) {
m, _ := NewMapJson(jdata)
j, jerr := m.Json()
if jerr != nil {
t.Fatal("Json, jerr:", jerr.Error())
}
fmt.Println("Json, jdata:", string(jdata))
fmt.Println("Json, j :", string(j))
j, _ = m.Json(true)
fmt.Println("Json, j safe:", string(j))
}
func TestJsonWriter(t *testing.T) {
mv := Map(map[string]interface{}{"this": "is a", "float": 3.14159, "and": "a", "bool": true})
w := new(bytes.Buffer)
raw, err := mv.JsonWriterRaw(w)
if err != nil {
t.Fatal("err:", err.Error())
}
b := make([]byte, w.Len())
_, err = w.Read(b)
if err != nil {
t.Fatal("err:", err.Error())
}
fmt.Println("JsonWriter, raw:", string(raw))
fmt.Println("JsonWriter, b :", string(b))
}