Skip to content

Commit 7408c49

Browse files
committed
Adds .UseNumber() to JSON decoding; fixes #6
1 parent 502a311 commit 7408c49

File tree

6 files changed

+41
-32
lines changed

6 files changed

+41
-32
lines changed

CHANGELOG.mkd

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## 0.2.4
4+
- Fixes handling of large integers (issue #6)
5+
6+
## 0.2.3
7+
- Switches Windows binary packaging to zip instead of tgz
8+
9+
## 0.2.2
10+
- Tweaks release automation, no user-facing changes
11+
312
## 0.2.1
413
- Adds windows binary
514

README.mkd

+4-5
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,10 @@ Exit Codes:
177177
0 OK
178178
1 Failed to open file
179179
2 Failed to read input
180-
3 Failed to decode JSON
181-
4 Failed to form statements
182-
5 Failed to fetch URL
183-
6 Failed to parse statements
184-
7 Failed to encode JSON
180+
3 Failed to form statements
181+
4 Failed to fetch URL
182+
5 Failed to parse statements
183+
6 Failed to encode JSON
185184
186185
Examples:
187186
gron /tmp/apiresponse.json

main.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"flag"
77
"fmt"
88
"io"
9-
"io/ioutil"
109
"os"
1110
"sort"
1211
)
@@ -15,7 +14,6 @@ const (
1514
exitOK = iota
1615
exitOpenFile
1716
exitReadInput
18-
exitJSONDecode
1917
exitFormStatements
2018
exitFetchURL
2119
exitParseStatements
@@ -36,7 +34,6 @@ func init() {
3634
h += fmt.Sprintf(" %d\t%s\n", exitOK, "OK")
3735
h += fmt.Sprintf(" %d\t%s\n", exitOpenFile, "Failed to open file")
3836
h += fmt.Sprintf(" %d\t%s\n", exitReadInput, "Failed to read input")
39-
h += fmt.Sprintf(" %d\t%s\n", exitJSONDecode, "Failed to decode JSON")
4037
h += fmt.Sprintf(" %d\t%s\n", exitFormStatements, "Failed to form statements")
4138
h += fmt.Sprintf(" %d\t%s\n", exitFetchURL, "Failed to fetch URL")
4239
h += fmt.Sprintf(" %d\t%s\n", exitParseStatements, "Failed to parse statements")
@@ -99,21 +96,7 @@ func main() {
9996

10097
func gron(r io.Reader, w io.Writer) (int, error) {
10198

102-
b, err := ioutil.ReadAll(r)
103-
if err != nil {
104-
return exitReadInput, fmt.Errorf("failed to read input: %s", err)
105-
}
106-
107-
// The 'JSON' might be an object, array or scalar, so the
108-
// best we can do for now is an empty interface type
109-
var top interface{}
110-
111-
err = json.Unmarshal(b, &top)
112-
if err != nil {
113-
return exitJSONDecode, fmt.Errorf("failed to decode JSON: %s", err)
114-
}
115-
116-
ss, err := makeStatements("json", top)
99+
ss, err := makeStatementsFromJSON(r)
117100
if err != nil {
118101
return exitFormStatements, fmt.Errorf("failed to form statements: %s", err)
119102
}

statements.go

+19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"strconv"
78
"unicode"
89
"unicode/utf8"
@@ -154,6 +155,19 @@ func (ss statements) Contains(search string) bool {
154155
return false
155156
}
156157

158+
// makeStatementsFromJSON takes an io.Reader containing JSON
159+
// and returns statements or an error on failure
160+
func makeStatementsFromJSON(r io.Reader) (statements, error) {
161+
var top interface{}
162+
d := json.NewDecoder(r)
163+
d.UseNumber()
164+
err := d.Decode(&top)
165+
if err != nil {
166+
return nil, err
167+
}
168+
return makeStatements("json", top)
169+
}
170+
157171
// makeStatements takes a prefix and interface value and returns
158172
// a statements list or an error on failure
159173
func makeStatements(prefix string, v interface{}) (statements, error) {
@@ -193,7 +207,12 @@ func makeStatements(prefix string, v interface{}) (statements, error) {
193207
ss.AddMulti(extra)
194208
}
195209

210+
case json.Number:
211+
ss.Add(prefix, vv.String())
212+
196213
case float64:
214+
// This case *should* be handled by json.Number
215+
// but is left just in case
197216
ss.Add(prefix, formatValue(vv))
198217

199218
case string:

statements_test.go

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

33
import (
4+
"bytes"
45
"encoding/json"
56
"reflect"
67
"sort"
@@ -19,16 +20,11 @@ func TestStatementsSimple(t *testing.T) {
1920
"anob": {
2021
"foo": "bar"
2122
},
22-
"else": 1
23+
"else": 1,
24+
"id": 66912849
2325
}`)
2426

25-
var top interface{}
26-
err := json.Unmarshal(j, &top)
27-
if err != nil {
28-
t.Errorf("Failed to unmarshal test file: %s", err)
29-
}
30-
31-
ss, err := makeStatements("json", top)
27+
ss, err := makeStatementsFromJSON(bytes.NewReader(j))
3228

3329
if err != nil {
3430
t.Errorf("Want nil error from makeStatements() but got %s", err)
@@ -47,8 +43,10 @@ func TestStatementsSimple(t *testing.T) {
4743
`json.anob = {};`,
4844
`json.anob.foo = "bar";`,
4945
`json["else"] = 1;`,
46+
`json.id = 66912849;`,
5047
}
5148

49+
t.Logf("Have: %#v", ss)
5250
for _, want := range wants {
5351
if !ss.Contains(want) {
5452
t.Errorf("Statement group should contain `%s` but doesn't", want)

testdata/one.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
},
1212
"abool": true,
1313
"abool2": false,
14-
"isnull": null
14+
"isnull": null,
15+
"id": 66912849
1516
}

0 commit comments

Comments
 (0)