-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding_test.go
105 lines (99 loc) · 2.06 KB
/
encoding_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
package encoding
import (
"fmt"
"testing"
)
func TestEncoding(t *testing.T) {
type testCase struct {
json, yml, xml string
}
cases := []*testCase{
&testCase{
`
{
"a": "aaa",
"b": 1234,
"b2": 1234.56,
"c": true,
"d": ["xx", "yy", "zz"],
"e": [11, 22, 33]
}
`,
``,
`
<data>
<ret>0</ret>
<errcode>0</errcode>
<msg>ok</msg>
<data>
<timestamp>128679200</timestamp>
<hasnext>0</hasnext>
<info>
<name></name>
<openid>B624064BA065E01CB73F835017FE96FA</openid>
<nick>aaaa</nick>
<head>http://app.qlogo.cn/mbloghead/563ad8b6be488a07a694</head>
<sex>1</sex>
<location>广东 深圳</location>
<country_code>1</country_code>
<province_code>44</province_code>
<city_code>3</city_code>
<tweet>
<text></text>
<from>来自网页</from>
<id>7987543214334</id>
<timestamp>1285813236</timestamp>
</tweet>
<fansnum>15</fansnum>
<idolnum>20</idolnum>
<isfans>1</isfans>
<isvip>0</isvip>
<tag>
<id>1</id>
<name></name>
</tag>
</info>
</data>
</data>
`,
},
}
for i, v := range cases {
{
d, _ := FromJson(v.json)
fmt.Printf("%d %s %#v\n", i, v, d)
yml, _ := ToYaml(d, true)
fmt.Printf("yml:\n%s\n", yml)
xml, _ := ToXml(d, true)
fmt.Printf("xml:\n%s\n", xml)
}
if v.xml != "" {
d, _ := FromXml(v.xml)
fmt.Printf("%d %s %#v\n", i, v, d)
}
}
var xx = []interface{}{
123,
"string",
true,
12.34,
[]string{"a", "b", "c"},
[]int{1, 2, 3, 4},
struct {
X int
Y string
}{2, "yy"},
}
xml, _ := ToXml(xx, true)
fmt.Printf("xml:\n%s\n", xml)
xmls, _ := FromXml(xml)
fmt.Printf("xmls:\n%#v\n", xmls)
yml, _ := ToYaml(xx, true)
fmt.Printf("yml:\n%s\n", yml)
ymls := MustFromYaml(yml)
fmt.Printf("ymls:\n%#v\n", ymls)
jsn, _ := ToJson(xx, true)
fmt.Printf("json:\n%s\n", jsn)
jsns := MustFromJson(jsn)
fmt.Printf("jsns:\n%#v\n", jsns)
}