valyala / fastjson

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection
MIT License
2.3k stars 138 forks source link

Consider provide a function in order to simply get *Value's parent key value #56

Open Dentrax opened 4 years ago

Dentrax commented 4 years ago

Hey! Thanks for such a great JSON framework. I am happy to use!

We could provide a string field in *Value struct or a function that called like: parentKey or GetParentKey() string

Why we should need like these field or function is that actually is we can not get parent key's value from in *Value context.

func get(v *fastjson.Value) {
 var key string = v.GetParentKey()
}

We must use Visit function in order to get key's value, which is not useful due to we must scan all of JSON's fields.

func get(v *fastjson.Value) {
 v.Visit(func(key []byte, v *fastjson.Value) {
  var key string = string(key)
 })
}

A few GetParentKey() examples:

{
  "a": "foo",
  "b": "bar",
  "c": {
    "d": "baz",
    "e": 0
  }
}

Thanks!

Dentrax commented 4 years ago

Hey @valyala, would you like any help with this issue? I am not making any promises but I'd be interested in looking into it, if you want.