-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
144 lines (127 loc) · 3.61 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
package henge
import (
"fmt"
"math"
)
func ExampleValueConverter_JSONValue() {
fmt.Println("int64")
fmt.Printf("%v -> %v\n", math.MinInt64, New(math.MinInt64).JSONValue().Value())
fmt.Printf("%v -> %v\n", 0, New(0).JSONValue().Value())
fmt.Println()
fmt.Println("uint64")
fmt.Printf("%v -> %v\n", uint64(math.MaxUint64), New(uint64(math.MaxUint64)).JSONValue().Value())
fmt.Printf("%v -> %v\n", 0, New(0).JSONValue().Value())
fmt.Println()
fmt.Println("float64")
fmt.Printf("%v -> %v\n", -1*math.MaxFloat64, New(-1*math.MaxFloat64).JSONValue().Value())
fmt.Printf("%v -> %v\n", 0.0, New(0.0).JSONValue().Value())
fmt.Println()
fmt.Println("bool")
fmt.Printf("%v -> %v\n", true, New(true).JSONValue().Value())
fmt.Printf("%v -> %v\n", false, New(false).JSONValue().Value())
fmt.Println()
fmt.Println("string")
fmt.Printf("%v -> %v\n", "aaaa", New("aaaa").JSONValue().Value())
fmt.Println()
fmt.Println("null")
fmt.Printf("%v -> %v\n", nil, New(nil).JSONValue().Value())
fmt.Println()
type Y struct {
Z string
}
type X struct {
Y
}
type Object struct {
X
}
fmt.Println("object")
fmt.Printf("%#v\n -> %#v\n", Object{X: X{Y{Z: "z"}}}, New(Object{X: X{Y{Z: "z"}}}).JSONValue().Value())
fmt.Printf(
"%#v\n -> %#v\n",
map[interface{}]interface{}{"a": Object{X: X{Y{Z: "z"}}}},
New(map[interface{}]interface{}{"a": Object{X: X{Y{Z: "z"}}}}).JSONValue().Value(),
)
fmt.Println()
fmt.Println("array")
fmt.Printf(
"%#v\n -> %#v\n",
[]interface{}{Object{X: X{Y{Z: "z"}}}},
New([]interface{}{Object{X: X{Y{Z: "z"}}}}).JSONValue().Value(),
)
// Output:
// int64
// -9223372036854775808 -> -9223372036854775808
// 0 -> 0
//
// uint64
// 18446744073709551615 -> 18446744073709551615
// 0 -> 0
//
// float64
// -1.7976931348623157e+308 -> -1.7976931348623157e+308
// 0 -> 0
//
// bool
// true -> true
// false -> false
//
// string
// aaaa -> aaaa
//
// null
// <nil> -> <nil>
//
// object
// henge.Object{X:henge.X{Y:henge.Y{Z:"z"}}}
// -> map[string]interface {}{"X":map[string]interface {}{"Y":map[string]interface {}{"Z":"z"}}}
// map[interface {}]interface {}{"a":henge.Object{X:henge.X{Y:henge.Y{Z:"z"}}}}
// -> map[string]interface {}{"a":map[string]interface {}{"X":map[string]interface {}{"Y":map[string]interface {}{"Z":"z"}}}}
//
// array
// []interface {}{henge.Object{X:henge.X{Y:henge.Y{Z:"z"}}}}
// -> []interface {}{map[string]interface {}{"X":map[string]interface {}{"Y":map[string]interface {}{"Z":"z"}}}}
}
func ExampleValueConverter_JSONObject() {
type Y struct {
Z string
}
type X struct {
Y
}
type Object struct {
X
}
value := map[interface{}]interface{}{"a": Object{X: X{Y{Z: "z"}}}}
fmt.Printf("%#v\n -> %#v\n", value, New(value).JSONValue().Value())
// Output:
// map[interface {}]interface {}{"a":henge.Object{X:henge.X{Y:henge.Y{Z:"z"}}}}
// -> map[string]interface {}{"a":map[string]interface {}{"X":map[string]interface {}{"Y":map[string]interface {}{"Z":"z"}}}}
}
func ExampleValueConverter_JSONArray() {
type Y struct {
Z string
}
type X struct {
Y
}
type Object struct {
X
}
value := []interface{}{
1,
true,
"a",
map[interface{}]interface{}{1: Y{Z: "z"}},
Object{X: X{Y{Z: "z"}}},
}
for i, v := range New(value).JSONArray().Value() {
fmt.Printf("[%v] %#v -> %#v\n", i, value[i], v)
}
// Output:
// [0] 1 -> 1
// [1] true -> true
// [2] "a" -> "a"
// [3] map[interface {}]interface {}{1:henge.Y{Z:"z"}} -> map[string]interface {}{"1":map[string]interface {}{"Z":"z"}}
// [4] henge.Object{X:henge.X{Y:henge.Y{Z:"z"}}} -> map[string]interface {}{"X":map[string]interface {}{"Y":map[string]interface {}{"Z":"z"}}}
}