11package json2csv
22
33import (
4+ "bytes"
45 "encoding/json"
56 "reflect"
67 "testing"
78)
89
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+
922var testJSON2CSVCases = []struct {
1023 json string
1124 expected []KeyValue
@@ -17,8 +30,8 @@ var testJSON2CSVCases = []struct {
1730 {"id": 2, "name": "bar"}
1831 ]` ,
1932 []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" },
2235 },
2336 `` ,
2437 },
@@ -28,8 +41,8 @@ var testJSON2CSVCases = []struct {
2841 {"id": 2, "name~b": "bar"}
2942 ]` ,
3043 []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" },
3346 },
3447 `` ,
3548 },
@@ -39,8 +52,8 @@ var testJSON2CSVCases = []struct {
3952 {"id":2, "values":["x"]}
4053 ]` ,
4154 []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" },
4457 },
4558 `` ,
4659 },
@@ -50,8 +63,8 @@ var testJSON2CSVCases = []struct {
5063 {"id":2, "values":["x"]}
5164 ]` ,
5265 []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" },
5568 },
5669 `` ,
5770 },
@@ -61,8 +74,8 @@ var testJSON2CSVCases = []struct {
6174 {"id":2, "values":["x"]}
6275 ]` ,
6376 []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" },
6679 },
6780 `` ,
6881 },
@@ -75,7 +88,7 @@ var testJSON2CSVCases = []struct {
7588 ]
7689 }` ,
7790 []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" },
7992 },
8093 `` ,
8194 },
@@ -89,15 +102,24 @@ var testJSON2CSVCases = []struct {
89102 []KeyValue {},
90103 `` ,
91104 },
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+ },
92115 {`"foo"` , nil , `Unsupported JSON structure.` },
93116 {`123` , nil , `Unsupported JSON structure.` },
94117 {`true` , nil , `Unsupported JSON structure.` },
95118}
96119
97120func TestJSON2CSV (t * testing.T ) {
98121 for caseIndex , testCase := range testJSON2CSVCases {
99- var obj interface {}
100- err := json .Unmarshal ([]byte (testCase .json ), & obj )
122+ obj , err := json2obj (testCase .json )
101123 if err != nil {
102124 t .Fatal (err )
103125 }
0 commit comments