Skip to content

Commit 5ef65f4

Browse files
committed
- Add documentation to readme for Delete
- update name of Del method to Delete
1 parent d79e320 commit 5ef65f4

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,19 @@ Accepts multiple keys to specify path to JSON value (in case of updating or crea
215215

216216
Note that keys can be an array indexes: `jsonparser.Set(data, []byte("http://github.com"), "person", "avatars", "[0]", "url")`
217217

218+
### **`Delete`**
219+
```go
220+
func Delete(data []byte, keys ...string) value []byte
221+
```
222+
Receives existing data structure, and key path to delete. *This functionality is experimental.*
223+
224+
Returns:
225+
* `value` - Pointer to original data structure with key path deleted if it can be found. If there is no key path, then the whole data structure is deleted.
226+
227+
Accepts multiple keys to specify path to JSON value (in case of updating or creating nested structures).
228+
229+
Note that keys can be an array indexes: `jsonparser.Delete(data, "person", "avatars", "[0]", "url")`
230+
218231

219232
## What makes it so fast?
220233
* It does not rely on `encoding/json`, `reflection` or `interface{}`, the only real package dependency is `bytes`.
@@ -248,7 +261,7 @@ If you want to skip next sections we have 2 winner: `jsonparser` and `easyjson`.
248261
249262
It's hard to fully compare `jsonparser` and `easyjson` (or `ffson`), they a true parsers and fully process record, unlike `jsonparser` which parse only keys you specified.
250263
251-
If you searching for replacement of `encoding/json` while keeping structs, `easyjson` is an amazing choise. If you want to process dynamic JSON, have memory constrains, or more control over your data you should try `jsonparser`.
264+
If you searching for replacement of `encoding/json` while keeping structs, `easyjson` is an amazing choice. If you want to process dynamic JSON, have memory constrains, or more control over your data you should try `jsonparser`.
252265
253266
`jsonparser` performance heavily depends on usage, and it works best when you do not need to process full record, only some keys. The more calls you need to make, the slower it will be, in contrast `easyjson` (or `ffjson`, `encoding/json`) parser record only 1 time, and then you can make as many calls as you want.
254267
@@ -324,7 +337,7 @@ https://github.com/buger/jsonparser/blob/master/benchmark/benchmark_large_payloa
324337
| mailru/easyjson | **154186** | **6992** | **288** |
325338
| buger/jsonparser | **85308** | **0** | **0** |
326339
327-
`jsonparser` now is a winner, but do not forget that it is way more lighweight parser than `ffson` or `easyjson`, and they have to parser all the data, while `jsonparser` parse only what you need. All `ffjson`, `easysjon` and `jsonparser` have their own parsing code, and does not depend on `encoding/json` or `interface{}`, thats one of the reasons why they are so fast. `easyjson` also use a bit of `unsafe` package to reduce memory consuption (in theory it can lead to some unexpected GC issue, but i did not tested enough)
340+
`jsonparser` now is a winner, but do not forget that it is way more lightweight parser than `ffson` or `easyjson`, and they have to parser all the data, while `jsonparser` parse only what you need. All `ffjson`, `easysjon` and `jsonparser` have their own parsing code, and does not depend on `encoding/json` or `interface{}`, thats one of the reasons why they are so fast. `easyjson` also use a bit of `unsafe` package to reduce memory consuption (in theory it can lead to some unexpected GC issue, but i did not tested enough)
328341
329342
Also last benchmark did not included `EachKey` test, because in this particular case we need to read lot of Array values, and using `ArrayEach` is more efficient.
330343

benchmark/benchmark_medium_payload_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ func BenchmarkJsonParserMedium(b *testing.B) {
3838
}
3939
}
4040

41-
func BenchmarkJsonParserDelMedium(b *testing.B) {
41+
func BenchmarkJsonParserDeleteMedium(b *testing.B) {
4242
fixture := make([]byte, 0, len(mediumFixture))
4343
b.ResetTimer()
4444
for i := 0; i < b.N; i++ {
4545
fixture = append(fixture[:0], mediumFixture...)
46-
fixture = jsonparser.Del(fixture, "person", "name", "fullName")
47-
fixture = jsonparser.Del(fixture, "person", "github", "followers")
48-
fixture = jsonparser.Del(fixture, "company")
46+
fixture = jsonparser.Delete(fixture, "person", "name", "fullName")
47+
fixture = jsonparser.Delete(fixture, "person", "github", "followers")
48+
fixture = jsonparser.Delete(fixture, "company")
4949

5050
nothing()
5151
}

benchmark/benchmark_small_payload_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ func BenchmarkJsonParserDelSmall(b *testing.B) {
147147
b.ResetTimer()
148148
for i := 0; i < b.N; i++ {
149149
fixture = append(fixture[:0], smallFixture...)
150-
fixture = jsonparser.Del(fixture, "uuid")
151-
fixture = jsonparser.Del(fixture, "tz")
152-
fixture = jsonparser.Del(fixture, "ua")
153-
fixture = jsonparser.Del(fixture, "stt")
150+
fixture = jsonparser.Delete(fixture, "uuid")
151+
fixture = jsonparser.Delete(fixture, "tz")
152+
fixture = jsonparser.Delete(fixture, "ua")
153+
fixture = jsonparser.Delete(fixture, "stt")
154154

155155
nothing()
156156
}

parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ Returns:
587587
`data` - return modified data
588588
589589
*/
590-
func Del(data []byte, keys ...string) []byte {
590+
func Delete(data []byte, keys ...string) []byte {
591591
lk := len(keys)
592592
if lk == 0 {
593593
return data[:0]

parser_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ type SetTest struct {
5050
data interface{}
5151
}
5252

53-
type DelTest struct {
53+
type DeleteTest struct {
5454
desc string
5555
json string
5656
path []string
5757

5858
data interface{}
5959
}
6060

61-
var delTests = []DelTest{
61+
var deleteTests = []DeleteTest{
6262
{
6363
desc: "Delete test key",
6464
json: `{"test":"input"}`,
@@ -975,7 +975,7 @@ func runSetTests(t *testing.T, testKind string, tests []SetTest, runner func(Set
975975
}
976976
}
977977

978-
func runDelTests(t *testing.T, testKind string, tests []DelTest, runner func(DelTest) interface{}, resultChecker func(DelTest, interface{}) (bool, interface{})) {
978+
func runDeleteTests(t *testing.T, testKind string, tests []DeleteTest, runner func(DeleteTest) interface{}, resultChecker func(DeleteTest, interface{}) (bool, interface{})) {
979979
for _, test := range tests {
980980
if activeTest != "" && test.desc != activeTest {
981981
continue
@@ -1015,12 +1015,12 @@ func TestSet(t *testing.T) {
10151015
)
10161016
}
10171017

1018-
func TestDel(t *testing.T) {
1019-
runDelTests(t, "Del()", delTests,
1020-
func(test DelTest) interface{} {
1021-
return Del([]byte(test.json), test.path...)
1018+
func TestDelete(t *testing.T) {
1019+
runDeleteTests(t, "Delete()", deleteTests,
1020+
func(test DeleteTest) interface{} {
1021+
return Delete([]byte(test.json), test.path...)
10221022
},
1023-
func(test DelTest, value interface{}) (bool, interface{}) {
1023+
func(test DeleteTest, value interface{}) (bool, interface{}) {
10241024
expected := []byte(test.data.(string))
10251025
return bytes.Equal(expected, value.([]byte)), expected
10261026
},

0 commit comments

Comments
 (0)