From 80a3717e0ac914ed7696a06279b65a0787c72442 Mon Sep 17 00:00:00 2001 From: John Floren Date: Fri, 25 Oct 2019 16:38:04 -0600 Subject: [PATCH 1/3] Fix an issue where you could not address arrays of strings --- parser.go | 10 ++++++++-- parser_test.go | 29 +++++++++++++++++------------ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/parser.go b/parser.go index 4327cc0..52e1490 100644 --- a/parser.go +++ b/parser.go @@ -511,8 +511,14 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str pathFlags |= bitwiseFlags[pi+1] if of != -1 { - v, dt, _, e := Get(value[of:]) - cb(pi, v, dt, e) + val := value[of:] + if dataType == String { + // the double-quotes were stripped, so we cannot call Get again. + cb(pi, val, dataType, nil) + } else { + v, dt, _, e := Get(val) + cb(pi, v, dt, e) + } } } } diff --git a/parser_test.go b/parser_test.go index c0a31e8..492fe33 100644 --- a/parser_test.go +++ b/parser_test.go @@ -887,18 +887,18 @@ var getStringTests = []GetTest{ data: "value\b\f\n\r\tvalue", // value is unescaped since this is GetString() }, { // This test checks we avoid an infinite loop for certain malformed JSON. We don't check for all malformed JSON as it would reduce performance. - desc: `malformed with double quotes`, - json: `{"a"":1}`, - path: []string{"a"}, + desc: `malformed with double quotes`, + json: `{"a"":1}`, + path: []string{"a"}, isFound: false, - data: ``, + data: ``, }, { // More malformed JSON testing, to be sure we avoid an infinite loop. - desc: `malformed with double quotes, and path does not exist`, - json: `{"z":123,"y":{"x":7,"w":0},"v":{"u":"t","s":"r","q":0,"p":1558051800},"a":"b","c":"2016-11-02T20:10:11Z","d":"e","f":"g","h":{"i":"j""},"k":{"l":"m"}}`, - path: []string{"o"}, + desc: `malformed with double quotes, and path does not exist`, + json: `{"z":123,"y":{"x":7,"w":0},"v":{"u":"t","s":"r","q":0,"p":1558051800},"a":"b","c":"2016-11-02T20:10:11Z","d":"e","f":"g","h":{"i":"j""},"k":{"l":"m"}}`, + path: []string{"o"}, isFound: false, - data: ``, + data: ``, }, } @@ -1453,7 +1453,7 @@ func TestObjectEach(t *testing.T) { } } -var testJson = []byte(`{"name": "Name", "order": "Order", "sum": 100, "len": 12, "isPaid": true, "nested": {"a":"test", "b":2, "nested3":{"a":"test3","b":4}, "c": "unknown"}, "nested2": {"a":"test2", "b":3}, "arr": [{"a":"zxc", "b": 1}, {"a":"123", "b":2}], "arrInt": [1,2,3,4], "intPtr": 10}`) +var testJson = []byte(`{"name": "Name", "order": "Order", "sum": 100, "len": 12, "isPaid": true, "nested": {"a":"test", "b":2, "nested3":{"a":"test3","b":4}, "c": "unknown"}, "nested2": {"a":"test2", "b":3}, "arr": [{"a":"zxc", "b": 1}, {"a":"123", "b":2}], "arrInt": [1,2,3,4], "intPtr": 10, "arrString": ["a","b","c"]}`) func TestEachKey(t *testing.T) { paths := [][]string{ @@ -1465,6 +1465,7 @@ func TestEachKey(t *testing.T) { {"nested", "nested3", "b"}, {"arr", "[1]", "b"}, {"arrInt", "[3]"}, + {"arrString", "[1]"}, {"arrInt", "[5]"}, // Should not find last key } @@ -1506,13 +1507,17 @@ func TestEachKey(t *testing.T) { if string(value) != "4" { t.Error("Should find 8 key", string(value)) } + case 8: + if string(value) != "b" { + t.Error("Should find 9 key", string(value)) + } default: - t.Errorf("Should found only 8 keys") + t.Errorf("Should found only 9 keys") } }, paths...) - if keysFound != 8 { - t.Errorf("Should find 8 keys: %d", keysFound) + if keysFound != 9 { + t.Errorf("Should find 9 keys: %d", keysFound) } } From 2951042f1c1396141000c893fca17e2be3cfaa25 Mon Sep 17 00:00:00 2001 From: John Floren Date: Fri, 25 Oct 2019 16:41:54 -0600 Subject: [PATCH 2/3] remove extraneous variable --- parser.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/parser.go b/parser.go index 52e1490..83ffe75 100644 --- a/parser.go +++ b/parser.go @@ -511,12 +511,11 @@ func EachKey(data []byte, cb func(int, []byte, ValueType, error), paths ...[]str pathFlags |= bitwiseFlags[pi+1] if of != -1 { - val := value[of:] if dataType == String { // the double-quotes were stripped, so we cannot call Get again. - cb(pi, val, dataType, nil) + cb(pi, value[of:], dataType, nil) } else { - v, dt, _, e := Get(val) + v, dt, _, e := Get(value[of:]) cb(pi, v, dt, e) } } From e5063027fcebbca771afbeff0e25c8e9c9e12a81 Mon Sep 17 00:00:00 2001 From: John Floren Date: Tue, 27 Jul 2021 12:19:45 -0700 Subject: [PATCH 3/3] Bring array-strings PR branch up to date --- parser_test.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/parser_test.go b/parser_test.go index b8f13ca..cc5a486 100644 --- a/parser_test.go +++ b/parser_test.go @@ -1719,8 +1719,6 @@ func TestEachKey(t *testing.T) { {"arrInt", "[3]"}, {"arrInt", "[5]"}, // Should not find last key {"nested"}, - {"arr", "["}, // issue#177 Invalid arguments - {"a\n", "b\n"}, // issue#165 {"arr", "["}, // issue#177 Invalid arguments {"a\n", "b\n"}, // issue#165 {"nested", "b"}, // Should find repeated key @@ -1778,11 +1776,11 @@ func TestEachKey(t *testing.T) { t.Error("Should find 11 key", string(value)) } case 12: - if string(value) != "b" { + if string(value) != "2" { t.Error("Should find 12 key", string(value)) } - case 12: - if string(value) != "2" { + case 13: + if string(value) != "b" { t.Errorf("Should find 11 key") } default: @@ -1790,8 +1788,8 @@ func TestEachKey(t *testing.T) { } }, paths...) - if keysFound != 11 { - t.Errorf("Should find 11 keys: %d", keysFound) + if keysFound != 12 { + t.Errorf("Should find 12 keys: %d", keysFound) } }