tidwall / gjson

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

Update the documentation of custom modifiers to specify the need of quotes in return type #305

Open sharadregoti opened 1 year ago

sharadregoti commented 1 year ago

Can we update the documentation of custom modifiers, to clearly specify that return type string should have quotes. If it doesn't have quotes we get empty value as result

package main

import (
    "strings"

    "github.com/tidwall/gjson"
)

const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`

func main() {
    gjson.AddModifier("case", func(json, arg string) string {
        if arg == "upper" {
            return strings.ToUpper(json)
        }
        if arg == "lower" {
            return strings.ToLower(json)
        }
        return json
    })
    gjson.AddModifier("age", func(json, arg string) string {
        if arg == "0" {
                        // Quotes is necessary here
            return `"test"`
        }
        return json
    })

    value2 := gjson.Get(json, "name.last|@case:upper")
    println(value2.String())
    value := gjson.Get(json, "name.last|@age:0")
    println(value.String())
}

I am thinking of adding this as note below custom modifier doc section

Please note that returned string type should have double quotes in it. 
    gjson.AddModifier("age", func(json, arg string) string {
        if arg == "custom-arg" {
                        // Quotes is necessary here
            return `"test"`
        }
        return json
    })