tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
13.95k stars 841 forks source link

modifier is ignored when the path is nested access value null #273

Closed Cluas closed 2 years ago

Cluas commented 2 years ago
// You can edit this code!
// Click here and start typing.
package main

import (
    "fmt"

    "github.com/tidwall/gjson"
)

func main() {
    gjson.AddModifier("exist",
        func(json, _ string) string {
            fmt.Println("moExist executed!")
            r := gjson.Parse(json)
            if !r.Exists() {
                return "false"
            }
            if r.IsObject() && len(r.Map()) == 0 {
                return "false"
            }
            if r.IsArray() && len(r.Array()) == 0 {
                return "false"
            }
            return "true"
        })
    r := gjson.Parse(`{"stock_score":null}`)
    fmt.Println(r.Get("stock_score.multi_letter.@exist").String())

}

https://go.dev/play/p/BcIxxd20JUX

Expected to be executed here, but not actually executed.