forked from basgys/goxml2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter_test.go
233 lines (203 loc) · 6.42 KB
/
converter_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package xml2json
import (
"fmt"
"strings"
"testing"
sj "github.com/bitly/go-simplejson"
"github.com/stretchr/testify/assert"
)
// TestConvert ensures that the whole process works correctly
// It takes an XML document and outputs a JSON document
func TestConvert(t *testing.T) {
assert := assert.New(t)
s := `<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.0.2">
<bounds minlat="54.0889580" minlon="12.2487570" maxlat="54.0913900" maxlon="12.2524800"/>
<node id="298884269" lat="54.0901746" lon="12.2482632" user="SvenHRO" uid="46882" visible="true" version="1" changeset="676636" timestamp="2008-09-21T21:37:45Z"/>
<node id="261728686" lat="54.0906309" lon="12.2441924" user="PikoWinter" uid="36744" visible="true" version="1" changeset="323878" timestamp="2008-05-03T13:39:23Z"/>
<node id="1831881213" version="1" changeset="12370172" lat="54.0900666" lon="12.2539381" user="lafkor" uid="75625" visible="true" timestamp="2012-07-20T09:43:19Z">
<tag k="name" v="Neu Broderstorf"/>
<tag k="traffic_sign" v="city_limit"/>
</node>
<foo>bar</foo>
<mixed attr="attribute">
content
</mixed>
</osm>`
// Build SimpleJSON
json, err := sj.NewJson([]byte(`{
"osm": {
"-version": "0.6",
"-generator": "CGImap 0.0.2",
"bounds": {
"-minlat": "54.0889580",
"-minlon": "12.2487570",
"-maxlat": "54.0913900",
"-maxlon": "12.2524800"
},
"node": [
{
"-id": "298884269",
"-lat": "54.0901746",
"-lon": "12.2482632",
"-user": "SvenHRO",
"-uid": "46882",
"-visible": "true",
"-version": "1",
"-changeset": "676636",
"-timestamp": "2008-09-21T21:37:45Z"
},
{
"-id": "261728686",
"-lat": "54.0906309",
"-lon": "12.2441924",
"-user": "PikoWinter",
"-uid": "36744",
"-visible": "true",
"-version": "1",
"-changeset": "323878",
"-timestamp": "2008-05-03T13:39:23Z"
},
{
"-id": "1831881213",
"-version": "1",
"-changeset": "12370172",
"-lat": "54.0900666",
"-lon": "12.2539381",
"-user": "lafkor",
"-uid": "75625",
"-visible": "true",
"-timestamp": "2012-07-20T09:43:19Z",
"tag": [
{
"-k": "name",
"-v": "Neu Broderstorf"
},
{
"-k": "traffic_sign",
"-v": "city_limit"
}
]
}
],
"foo": "bar",
"mixed": {
"-attr": "attribute",
"#content": "content"
}
}
}`))
assert.NoError(err)
expected, err := json.MarshalJSON()
assert.NoError(err)
// Then encode it in JSON
res, err := Convert(strings.NewReader(s))
assert.NoError(err)
// Assertion
assert.JSONEq(string(expected), res.String(), "Drumroll")
}
func TestConvertWithNewLines(t *testing.T) {
assert := assert.New(t)
s := `<?xml version="1.0" encoding="UTF-8"?>
<osm>
<foo>
foo
bar
</foo>
</osm>`
// Build SimpleJSON
json, err := sj.NewJson([]byte(`{
"osm": {
"foo": "foo\n\n\t\tbar"
}
}`))
assert.NoError(err)
expected, err := json.MarshalJSON()
assert.NoError(err)
// Then encode it in JSON
res, err := Convert(strings.NewReader(s))
assert.NoError(err)
// Assertion
assert.JSONEq(string(expected), res.String(), "Drumroll")
}
func TestConvertWithMixedTags(t *testing.T) {
assert := assert.New(t)
s := `<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header>
<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext">
<wsse:BinarySecurityToken valueType="String" EncodingType="wsse:Base64Binary">
Shared/IDL:IceSess\/SessMgr:1\.0.IDL/Common/!ICESMS\/ACPCRTC!ICESMSLB\/CRT.LB!-3379045898978075261!1563026!0
</wsse:BinarySecurityToken>
</wsse:Security>
</soap-env:Header>
</soap-env:Envelope> `
// Build SimpleJSON
json, err := sj.NewJson([]byte(`{
"Envelope": {
"Header": {
"Security": {
"-wsse": "http://schemas.xmlsoap.org/ws/2002/12/secext",
"BinarySecurityToken": {
"#content": "Shared/IDL:IceSess\\/SessMgr:1\\.0.IDL/Common/!ICESMS\\/ACPCRTC!ICESMSLB\\/CRT.LB!-3379045898978075261!1563026!0",
"-EncodingType": "wsse:Base64Binary",
"-valueType": "String"
}
}
},
"-soap-env": "http://schemas.xmlsoap.org/soap/envelope/"
}
}`))
assert.NoError(err)
expected, err := json.MarshalJSON()
assert.NoError(err)
// Then encode it in JSON
res, err := Convert(strings.NewReader(s))
assert.NoError(err)
// Assertion
assert.JSONEq(string(expected), res.String(), "Drumroll")
}
// TestConvertISO ensures that other charsets can be converted
func TestConvertISO(t *testing.T) {
assert := assert.New(t)
s := []byte{0x3C, 0x3F, 0x78, 0x6D, 0x6C, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x22, 0x31, 0x2E, 0x30, 0x22, 0x20, 0x65, 0x6E, 0x63, 0x6F, 0x64, 0x69, 0x6E, 0x67, 0x3D, 0x22, 0x49, 0x53, 0x4F, 0x2D, 0x38, 0x38, 0x35, 0x39, 0x2D, 0x31, 0x22, 0x3F, 0x3E, 0x3C, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3E, 0xFC, 0x62, 0x65, 0x72, 0x20, 0x63, 0x6F, 0x6D, 0x70, 0x6C, 0x65, 0x78, 0x3C, 0x2F, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3E}
// Build SimpleJSON
json, err := sj.NewJson([]byte(`{
"charset": "über complex"
}`))
assert.NoError(err)
expected, err := json.MarshalJSON()
assert.NoError(err)
// Then encode it in JSON
res, err := Convert(strings.NewReader(string(s)))
assert.NoError(err)
// Assertion
assert.JSONEq(string(expected), res.String(), "Drumroll")
}
func TestParseXml(t *testing.T) {
s := `<?xml version="1.0" encoding="utf-8"?>
<PBDailyBonusList>
<bonus_infos id="1" probability="100">
<bonus_day>300001</bonus_day>
<bonus_day>300002</bonus_day>
<bonus_day>300003</bonus_day>
<bonus_day>300004</bonus_day>
<bonus_day>300005</bonus_day>
</bonus_infos>
<bonus_infos id="2" probability="0">
<bonus_day>300004</bonus_day>
<bonus_day>300005</bonus_day>
<bonus_day>300006</bonus_day>
<bonus_day>300001</bonus_day>
<bonus_day>300007</bonus_day>
</bonus_infos>
</PBDailyBonusList>`
// Then encode it in JSON
res, err := ConvertWithParam(strings.NewReader(s), 2, true)
if err != nil {
panic(err)
}
fmt.Println(s)
fmt.Println("<->")
fmt.Println(res)
}