-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathjson_test.go
183 lines (163 loc) · 3.15 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package gscript
import (
"encoding/json"
"fmt"
"github.com/crossoverJie/xjson"
"testing"
)
func TestJSON(t *testing.T) {
script := `
class Person{
int age;
string name;
float weight;
bool man;
Person(string n, int a, float w, bool m){
name = n;
age = a;
weight = w;
man =m;
}
}
Person p1 = Person("abc",10,99.99,true);
Person p2 = Person("a",11,999.99,false);
string json = JSON(p1);
println(json);
int[] a = {1,2,3};
json = JSON(a);
println(json);
int b =10;
json = JSON(b);
println(json);
Person[] list = {p1,p2};
println(list);
Person temp = list[0];
println(temp.name);
json = JSON(list);
//[{"age":10,"man":true,"name":"abc","weight":99.99},{"age":11,"man":false,"name":"a","weight":999.99}]
println(json);
`
NewCompiler().Compiler(script)
}
func TestJSONGet(t *testing.T) {
script := `
class Person{
int age;
string name;
float weight;
bool man;
Person(string n, int a, float w, bool m){
name = n;
age = a;
weight = w;
man =m;
}
}
Person p1 = Person("abc",10,99.99,true);
string json = JSON(p1);
println(json);
int age = JSONGet(json, "age");
println(age);
assertEqual(age,10);
bool man = JSONGet(json, "man");
println(man);
assertEqual(man,true);
string name = JSONGet(json, "name");
println(name);
assertEqual(name,"abc");
float weight = JSONGet(json, "weight");
println(weight);
assertEqual(weight,99.99);
string j=^{"age":10, "abc":{"def":"def"},"list":[1,2,3]}^;
string def = JSONGet(j, "abc.def");
println(def);
assertEqual(def,"def");
int l1 = JSONGet(j, "list[0]");
println(l1);
assertEqual(l1,1);
string str=^
{
"name": "bob",
"age": 20,
"skill": {
"lang": [
{
"go": {
"feature": [
"goroutine",
"channel",
"simple",
true
]
}
}
]
}
}
^;
string g = JSONGet(str, "skill.lang[0].go.feature[0]");
println(g);
assertEqual(g,"goroutine");
`
NewCompiler().Compiler(script)
}
func TestJSON1(t *testing.T) {
a := 10
marshal, err := json.Marshal(a)
fmt.Println(string(marshal), err)
s1 := struct {
Name string `json:"name"`
Age int `json:"age"`
}{Name: "a", Age: 100}
s2 := struct {
Name string `json:"name"`
Age int `json:"age"`
}{Name: "a", Age: 10}
list := []interface{}{s1, s2}
bytes, err := json.Marshal(list)
fmt.Println(string(bytes), err)
fmt.Println(list)
}
func TestVisitor_JSONGet(t *testing.T) {
str := "{\"age\":10, \"abc\":{\"def\":\"def\"}}"
get := xjson.Get(str, "abc.def")
fmt.Println(get.String())
}
func TestVisitor_JSONGet2(t *testing.T) {
script := `
string s="1";
println(s);
string s1=^123^;
println(s1);
`
NewCompiler().Compiler(script)
}
func TestVisitor_JSONGet3(t *testing.T) {
script := `
class P{
string name;
P(string n){
name = n;
}
}
class Object{
P p;
int x;
Object(P pp, int xx){
p = pp;
x = xx;
}
}
P p1 = P("abc");
Object o1 = Object(p1, 100);
string json = JSON(o1);
println(json);
int x = JSONGet(json,"x");
println(x);
assertEqual(x,100);
string name = JSONGet(json,"p.name");
println(name);
assertEqual(name,"abc");
`
NewCompiler().Compiler(script)
}