Skip to content

Commit

Permalink
fix: allow empty arrays and objects in json flattener
Browse files Browse the repository at this point in the history
Closes: #82
  • Loading branch information
jhawk28 committed Jul 8, 2022
1 parent f811b97 commit 5d6fd72
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions flatten_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ func (fj *flattenJSON) readObject(pathName []byte) error {
}
memberIsUsed = (fj.skipping == 0) && fj.tracker.IsNameUsed(memberName)
state = seekingColonState
case '}':
return nil
default:
return fj.error(fmt.Sprintf("illegal character %c in JSON object", ch))
}
Expand Down Expand Up @@ -328,6 +330,8 @@ func (fj *flattenJSON) readArray(pathName []byte) error {
if err != nil {
return err
}
case ']':
return nil
default:
return fj.error(fmt.Sprintf("illegal character %c in array", ch))
}
Expand Down
6 changes: 3 additions & 3 deletions flatten_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func bequal(a []byte, b []byte) bool {
}

func TestFJBasic(t *testing.T) {
j := `{ "a": 1, "b": "two", "c": true, "d": null, "e": { "e1": 2, "e2": 3.02e-5}, "f": [33e2, "x", true, false, null], "g": false}`
allYes := fakeMatcher("a", "b", "c", "d", "e", "e1", "e2", "f", "g")
j := `{ "a": 1, "b": "two", "c": true, "d": null, "e": { "e1": 2, "e2": 3.02e-5}, "f": [33e2, "x", true, false, null], "g": false, "h": [], "i": {}}`
allYes := fakeMatcher("a", "b", "c", "d", "e", "e1", "e2", "f", "g", "h", "i")

f := newJSONFlattener()
list, err := f.Flatten([]byte(j), allYes)
if err != nil {
t.Error("E: " + err.Error())
}
wantedPaths := []string{"a", "b", "c", "d", "e\ne1", "e\ne2", "f", "f", "f", "f", "f", "g"}
wantedPaths := []string{"a", "b", "c", "d", "e\ne1", "e\ne2", "f", "f", "f", "f", "f", "g", "h", "i"}
wantedVals := []string{"1", "\"two\"", "true", "null", "2", "3.02e-5", "33e2", "\"x\"", "true", "false", "null", "false"}
if len(list) != len(wantedVals) {
t.Errorf("list len %d wanted %d", len(list), len(wantedVals))
Expand Down

0 comments on commit 5d6fd72

Please sign in to comment.