1
1
package json2csv
2
2
3
3
import (
4
+ "bytes"
4
5
"encoding/json"
5
6
"reflect"
6
7
"testing"
7
8
)
8
9
10
+ // Decode JSON with UseNumber option.
11
+ func json2obj (jsonstr string ) (interface {}, error ) {
12
+ r := bytes .NewReader ([]byte (jsonstr ))
13
+ d := json .NewDecoder (r )
14
+ d .UseNumber ()
15
+ var obj interface {}
16
+ if err := d .Decode (& obj ); err != nil {
17
+ return nil , err
18
+ }
19
+ return obj , nil
20
+ }
21
+
9
22
var testJSON2CSVCases = []struct {
10
23
json string
11
24
expected []KeyValue
@@ -17,8 +30,8 @@ var testJSON2CSVCases = []struct {
17
30
{"id": 2, "name": "bar"}
18
31
]` ,
19
32
[]KeyValue {
20
- {"/id" : 1.0 , "/name" : "foo" },
21
- {"/id" : 2.0 , "/name" : "bar" },
33
+ {"/id" : json . Number ( "1" ) , "/name" : "foo" },
34
+ {"/id" : json . Number ( "2" ) , "/name" : "bar" },
22
35
},
23
36
`` ,
24
37
},
@@ -28,8 +41,8 @@ var testJSON2CSVCases = []struct {
28
41
{"id": 2, "name~b": "bar"}
29
42
]` ,
30
43
[]KeyValue {
31
- {"/id" : 1.0 , "/name~1a" : "foo" },
32
- {"/id" : 2.0 , "/name~0b" : "bar" },
44
+ {"/id" : json . Number ( "1" ) , "/name~1a" : "foo" },
45
+ {"/id" : json . Number ( "2" ) , "/name~0b" : "bar" },
33
46
},
34
47
`` ,
35
48
},
@@ -39,8 +52,8 @@ var testJSON2CSVCases = []struct {
39
52
{"id":2, "values":["x"]}
40
53
]` ,
41
54
[]KeyValue {
42
- {"/id" : 1.0 , "/values/0" : "a" , "/values/1" : "b" },
43
- {"/id" : 2.0 , "/values/0" : "x" },
55
+ {"/id" : json . Number ( "1" ) , "/values/0" : "a" , "/values/1" : "b" },
56
+ {"/id" : json . Number ( "2" ) , "/values/0" : "x" },
44
57
},
45
58
`` ,
46
59
},
@@ -50,8 +63,8 @@ var testJSON2CSVCases = []struct {
50
63
{"id":2, "values":["x"]}
51
64
]` ,
52
65
[]KeyValue {
53
- {"/id" : 1.0 },
54
- {"/id" : 2.0 , "/values/0" : "x" },
66
+ {"/id" : json . Number ( "1" ) },
67
+ {"/id" : json . Number ( "2" ) , "/values/0" : "x" },
55
68
},
56
69
`` ,
57
70
},
@@ -61,8 +74,8 @@ var testJSON2CSVCases = []struct {
61
74
{"id":2, "values":["x"]}
62
75
]` ,
63
76
[]KeyValue {
64
- {"/id" : 1.0 },
65
- {"/id" : 2.0 , "/values/0" : "x" },
77
+ {"/id" : json . Number ( "1" ) },
78
+ {"/id" : json . Number ( "2" ) , "/values/0" : "x" },
66
79
},
67
80
`` ,
68
81
},
@@ -75,7 +88,7 @@ var testJSON2CSVCases = []struct {
75
88
]
76
89
}` ,
77
90
[]KeyValue {
78
- {"/id" : 123.0 , "/values/0/foo" : "FOO" , "/values/1/bar" : "BAR" },
91
+ {"/id" : json . Number ( "123" ) , "/values/0/foo" : "FOO" , "/values/1/bar" : "BAR" },
79
92
},
80
93
`` ,
81
94
},
@@ -89,15 +102,24 @@ var testJSON2CSVCases = []struct {
89
102
[]KeyValue {},
90
103
`` ,
91
104
},
105
+ {
106
+ `{"large_int_value": 146163870300}` ,
107
+ []KeyValue {{"/large_int_value" : json .Number ("146163870300" )}},
108
+ `` ,
109
+ },
110
+ {
111
+ `{"float_value": 146163870.300}` ,
112
+ []KeyValue {{"/float_value" : json .Number ("146163870.300" )}},
113
+ `` ,
114
+ },
92
115
{`"foo"` , nil , `Unsupported JSON structure.` },
93
116
{`123` , nil , `Unsupported JSON structure.` },
94
117
{`true` , nil , `Unsupported JSON structure.` },
95
118
}
96
119
97
120
func TestJSON2CSV (t * testing.T ) {
98
121
for caseIndex , testCase := range testJSON2CSVCases {
99
- var obj interface {}
100
- err := json .Unmarshal ([]byte (testCase .json ), & obj )
122
+ obj , err := json2obj (testCase .json )
101
123
if err != nil {
102
124
t .Fatal (err )
103
125
}
0 commit comments