When using to_json(...) marshal expr.Type into string will get empty
object {} if this Type doesn't implement marshal method. So UntypedValue
obj before json.Marshal if this obj isn't json.Marshaler.
22 lines
382 B
Go
22 lines
382 B
Go
package expr
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/PaesslerAG/gval"
|
|
)
|
|
|
|
func JsonFunctions() []gval.Language {
|
|
return []gval.Language{
|
|
// TODO: Json decoding, parsing, stringify, JQ, JSONPath
|
|
gval.Function("toJSON", toJSON),
|
|
}
|
|
}
|
|
|
|
func toJSON(f interface{}) string {
|
|
if _, is := f.(json.Marshaler); !is {
|
|
f = UntypedValue(f)
|
|
}
|
|
b, _ := json.Marshal(f)
|
|
return string(b)
|
|
}
|