Skip to content

Commit

Permalink
- Add documentation to readme for Delete
Browse files Browse the repository at this point in the history
- update name of Del method to Delete
  • Loading branch information
pytlesk4 committed Jul 5, 2017
1 parent d79e320 commit 5ef65f4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ Accepts multiple keys to specify path to JSON value (in case of updating or crea

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

### **`Delete`**
```go
func Delete(data []byte, keys ...string) value []byte
```
Receives existing data structure, and key path to delete. *This functionality is experimental.*

Returns:
* `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.

Accepts multiple keys to specify path to JSON value (in case of updating or creating nested structures).

Note that keys can be an array indexes: `jsonparser.Delete(data, "person", "avatars", "[0]", "url")`


## What makes it so fast?
* It does not rely on `encoding/json`, `reflection` or `interface{}`, the only real package dependency is `bytes`.
Expand Down Expand Up @@ -248,7 +261,7 @@ If you want to skip next sections we have 2 winner: `jsonparser` and `easyjson`.
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.
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`.
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`.
`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.
Expand Down Expand Up @@ -324,7 +337,7 @@ https://github.com/buger/jsonparser/blob/master/benchmark/benchmark_large_payloa
| mailru/easyjson | **154186** | **6992** | **288** |
| buger/jsonparser | **85308** | **0** | **0** |
`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)
`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)
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.
Expand Down
8 changes: 4 additions & 4 deletions benchmark/benchmark_medium_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ func BenchmarkJsonParserMedium(b *testing.B) {
}
}

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

nothing()
}
Expand Down
8 changes: 4 additions & 4 deletions benchmark/benchmark_small_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ func BenchmarkJsonParserDelSmall(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
fixture = append(fixture[:0], smallFixture...)
fixture = jsonparser.Del(fixture, "uuid")
fixture = jsonparser.Del(fixture, "tz")
fixture = jsonparser.Del(fixture, "ua")
fixture = jsonparser.Del(fixture, "stt")
fixture = jsonparser.Delete(fixture, "uuid")
fixture = jsonparser.Delete(fixture, "tz")
fixture = jsonparser.Delete(fixture, "ua")
fixture = jsonparser.Delete(fixture, "stt")

nothing()
}
Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ Returns:
`data` - return modified data
*/
func Del(data []byte, keys ...string) []byte {
func Delete(data []byte, keys ...string) []byte {
lk := len(keys)
if lk == 0 {
return data[:0]
Expand Down
16 changes: 8 additions & 8 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ type SetTest struct {
data interface{}
}

type DelTest struct {
type DeleteTest struct {
desc string
json string
path []string

data interface{}
}

var delTests = []DelTest{
var deleteTests = []DeleteTest{
{
desc: "Delete test key",
json: `{"test":"input"}`,
Expand Down Expand Up @@ -975,7 +975,7 @@ func runSetTests(t *testing.T, testKind string, tests []SetTest, runner func(Set
}
}

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

func TestDel(t *testing.T) {
runDelTests(t, "Del()", delTests,
func(test DelTest) interface{} {
return Del([]byte(test.json), test.path...)
func TestDelete(t *testing.T) {
runDeleteTests(t, "Delete()", deleteTests,
func(test DeleteTest) interface{} {
return Delete([]byte(test.json), test.path...)
},
func(test DelTest, value interface{}) (bool, interface{}) {
func(test DeleteTest, value interface{}) (bool, interface{}) {
expected := []byte(test.data.(string))
return bytes.Equal(expected, value.([]byte)), expected
},
Expand Down

0 comments on commit 5ef65f4

Please sign in to comment.