3
0
Files
corteza/pkg/expr/func_json.go
hantian_pang a80e2655fe Fix marshal expr.Type into empty object bug
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.
2022-10-24 08:10:18 +02:00

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)
}