fastjson icon indicating copy to clipboard operation
fastjson copied to clipboard

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

Open Dentrax opened this issue 4 years ago • 1 comments

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
  }
}
  • If Value is at the top level (which means there is no parent key), it returns nil or "".
  • If Value is at inside c key, it returns "c"

Thanks!

Dentrax avatar May 07 '20 11:05 Dentrax

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.

Dentrax avatar Aug 18 '20 20:08 Dentrax