xgo/stdlib/json.go

147 lines
3.1 KiB
Go
Raw Normal View History

package stdlib
import (
"bytes"
gojson "encoding/json"
2019-12-20 22:40:38 +03:00
"github.com/d5/tengo"
"github.com/d5/tengo/stdlib/json"
)
2019-12-20 22:40:38 +03:00
var jsonModule = map[string]tengo.Object{
"decode": &tengo.UserFunction{
Name: "decode",
Value: jsonDecode,
},
"encode": &tengo.UserFunction{
Name: "encode",
Value: jsonEncode,
},
"indent": &tengo.UserFunction{
Name: "encode",
Value: jsonIndent,
},
"html_escape": &tengo.UserFunction{
Name: "html_escape",
Value: jsonHTMLEscape,
},
}
2019-12-20 22:40:38 +03:00
func jsonDecode(args ...tengo.Object) (ret tengo.Object, err error) {
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrWrongNumArguments
}
switch o := args[0].(type) {
2019-12-20 22:40:38 +03:00
case *tengo.Bytes:
v, err := json.Decode(o.Value)
if err != nil {
2019-12-20 22:40:38 +03:00
return &tengo.Error{
Value: &tengo.String{Value: err.Error()},
}, nil
}
return v, nil
2019-12-20 22:40:38 +03:00
case *tengo.String:
v, err := json.Decode([]byte(o.Value))
if err != nil {
2019-12-20 22:40:38 +03:00
return &tengo.Error{
Value: &tengo.String{Value: err.Error()},
}, nil
}
return v, nil
default:
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "bytes/string",
Found: args[0].TypeName(),
}
}
}
2019-12-20 22:40:38 +03:00
func jsonEncode(args ...tengo.Object) (ret tengo.Object, err error) {
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrWrongNumArguments
}
b, err := json.Encode(args[0])
if err != nil {
2019-12-20 22:40:38 +03:00
return &tengo.Error{Value: &tengo.String{Value: err.Error()}}, nil
}
2019-12-20 22:40:38 +03:00
return &tengo.Bytes{Value: b}, nil
}
2019-12-20 22:40:38 +03:00
func jsonIndent(args ...tengo.Object) (ret tengo.Object, err error) {
if len(args) != 3 {
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrWrongNumArguments
}
2019-12-20 22:40:38 +03:00
prefix, ok := tengo.ToString(args[1])
if !ok {
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrInvalidArgumentType{
Name: "prefix",
Expected: "string(compatible)",
Found: args[1].TypeName(),
}
}
2019-12-20 22:40:38 +03:00
indent, ok := tengo.ToString(args[2])
if !ok {
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrInvalidArgumentType{
Name: "indent",
Expected: "string(compatible)",
Found: args[2].TypeName(),
}
}
switch o := args[0].(type) {
2019-12-20 22:40:38 +03:00
case *tengo.Bytes:
var dst bytes.Buffer
err := gojson.Indent(&dst, o.Value, prefix, indent)
if err != nil {
2019-12-20 22:40:38 +03:00
return &tengo.Error{
Value: &tengo.String{Value: err.Error()},
}, nil
}
2019-12-20 22:40:38 +03:00
return &tengo.Bytes{Value: dst.Bytes()}, nil
case *tengo.String:
var dst bytes.Buffer
err := gojson.Indent(&dst, []byte(o.Value), prefix, indent)
if err != nil {
2019-12-20 22:40:38 +03:00
return &tengo.Error{
Value: &tengo.String{Value: err.Error()},
}, nil
}
2019-12-20 22:40:38 +03:00
return &tengo.Bytes{Value: dst.Bytes()}, nil
default:
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "bytes/string",
Found: args[0].TypeName(),
}
}
}
2019-12-20 22:40:38 +03:00
func jsonHTMLEscape(args ...tengo.Object) (ret tengo.Object, err error) {
if len(args) != 1 {
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrWrongNumArguments
}
switch o := args[0].(type) {
2019-12-20 22:40:38 +03:00
case *tengo.Bytes:
var dst bytes.Buffer
gojson.HTMLEscape(&dst, o.Value)
2019-12-20 22:40:38 +03:00
return &tengo.Bytes{Value: dst.Bytes()}, nil
case *tengo.String:
var dst bytes.Buffer
gojson.HTMLEscape(&dst, []byte(o.Value))
2019-12-20 22:40:38 +03:00
return &tengo.Bytes{Value: dst.Bytes()}, nil
default:
2019-12-20 22:40:38 +03:00
return nil, tengo.ErrInvalidArgumentType{
Name: "first",
Expected: "bytes/string",
Found: args[0].TypeName(),
}
}
}