Skip to content

Commit

Permalink
Fix parsing of large JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Krivchun Maxim committed Sep 7, 2020
1 parent bbbdafd commit 2d48ee5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jsoncjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (t *jsoncTranslator) Read(jsonOut []byte) (n int, err error) {
}
}

return n, nil
return n + 1, nil
}

type token int8
Expand Down
26 changes: 26 additions & 0 deletions jsoncjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package jsoncjson_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"reflect"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -194,3 +196,27 @@ func testJSON(t *testing.T, in string, exp interface{}, got interface{}) {
t.Fatalf("exp %+x, got: %+x", exp, got)
}
}

func TestLargeJSON(t *testing.T) {
var data = make(map[string]int, bytes.MinRead)
for i := 0; i < bytes.MinRead; i++ {
data[strconv.Itoa(i)] = i
}

var expBuf, err = json.Marshal(data)
if err != nil {
t.Fatalf("err: %s", err)
}

var r = jsoncjson.NewReader(bytes.NewReader(expBuf))

var gotBuf []byte
gotBuf, err = ioutil.ReadAll(r)
if err != nil {
t.Fatalf("err: %s", err)
}

if !bytes.Equal(expBuf, gotBuf) {
t.Fatalf("exp len %d, got len: %d", len(expBuf), len(gotBuf))
}
}

0 comments on commit 2d48ee5

Please sign in to comment.