Skip to content

Commit 5096fdd

Browse files
authored
Merge pull request #124 from vkd/fix-delete
Fix Delete()
2 parents fda8192 + 2b2103c commit 5096fdd

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

parser.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func findTokenStart(data []byte, token byte) int {
5252
func findKeyStart(data []byte, key string) (int, error) {
5353
i := 0
5454
ln := len(data)
55+
if ln > 0 && (data[0] == '{' || data[0] == '[') {
56+
i = 1
57+
}
5558
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings
5659

5760
if ku, err := Unescape(StringToBytes(key), stackbuf[:]); err == nil {
@@ -94,6 +97,10 @@ func findKeyStart(data []byte, key string) (int, error) {
9497
return keyBegin - 1, nil
9598
}
9699

100+
case '[':
101+
i = blockEnd(data[i:], data[i], ']') + i
102+
case '{':
103+
i = blockEnd(data[i:], data[i], '}') + i
97104
}
98105
i++
99106
}

parser_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,30 @@ var deleteTests = []DeleteTest{
143143
path: []string{"b"},
144144
data: `{"a":}`,
145145
},
146+
{
147+
desc: "Delete object without inner array",
148+
json: `{"a": {"b": 1}, "b": 2}`,
149+
path: []string{"b"},
150+
data: `{"a": {"b": 1}}`,
151+
},
152+
{
153+
desc: "Delete object without inner array",
154+
json: `{"a": [{"b": 1}], "b": 2}`,
155+
path: []string{"b"},
156+
data: `{"a": [{"b": 1}]}`,
157+
},
158+
{
159+
desc: "Delete object without inner array",
160+
json: `{"a": {"c": {"b": 3}, "b": 1}, "b": 2}`,
161+
path: []string{"a", "b"},
162+
data: `{"a": {"c": {"b": 3}}, "b": 2}`,
163+
},
164+
{
165+
desc: "Delete object without inner array",
166+
json: `{"a": [{"c": {"b": 3}, "b": 1}], "b": 2}`,
167+
path: []string{"a", "[0]", "b"},
168+
data: `{"a": [{"c": {"b": 3}}], "b": 2}`,
169+
},
146170
}
147171

148172
var setTests = []SetTest{

0 commit comments

Comments
 (0)