Skip to content

Commit b3db7b4

Browse files
committedApr 27, 2016
Merge branch 'fix-number-float'
2 parents c8c64e3 + bce90ab commit b3db7b4

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed
 

‎cmd/json2csv/main.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"log"
98
"os"
109

@@ -19,7 +18,7 @@ const (
1918
ApplicationName = "json2csv"
2019

2120
// Version is the version number of this application.
22-
Version = "0.1.0"
21+
Version = "0.1.1"
2322
)
2423

2524
var headerStyleTable = map[string]json2csv.KeyStyle{
@@ -145,13 +144,11 @@ func readJSONFile(filename string) (interface{}, error) {
145144
}
146145

147146
func readJSON(r io.Reader) (interface{}, error) {
148-
buf, err := ioutil.ReadAll(r)
149-
if err != nil {
150-
return nil, err
151-
}
147+
decoder := json.NewDecoder(r)
148+
decoder.UseNumber()
152149

153150
var data interface{}
154-
if err := json.Unmarshal(buf, &data); err != nil {
151+
if err := decoder.Decode(&data); err != nil {
155152
return nil, err
156153
}
157154

‎flatten.go

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package json2csv
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"reflect"
67
"sort"
@@ -9,6 +10,8 @@ import (
910
"github.com/yukithm/json2csv/jsonpointer"
1011
)
1112

13+
var jsonNumberType = reflect.TypeOf(json.Number(""))
14+
1215
type mapKeys []reflect.Value
1316

1417
func (k mapKeys) Len() int { return len(k) }
@@ -51,6 +54,14 @@ func _flatten(out KeyValue, obj interface{}, key jsonpointer.JSONPointer) error
5154
value = value.Elem()
5255
}
5356

57+
if value.IsValid() {
58+
vt := value.Type()
59+
if vt.AssignableTo(jsonNumberType) {
60+
out[key.String()] = value.Interface().(json.Number)
61+
return nil
62+
}
63+
}
64+
5465
switch value.Kind() {
5566
case reflect.Map:
5667
_flattenMap(out, value, key)

‎json2csv_test.go

+35-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
package json2csv
22

33
import (
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+
922
var 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

97120
func 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

Comments
 (0)
Please sign in to comment.