summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/d5/tengo/objects/builtin_json.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/d5/tengo/objects/builtin_json.go')
-rw-r--r--vendor/github.com/d5/tengo/objects/builtin_json.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/vendor/github.com/d5/tengo/objects/builtin_json.go b/vendor/github.com/d5/tengo/objects/builtin_json.go
new file mode 100644
index 00000000..c0810f7d
--- /dev/null
+++ b/vendor/github.com/d5/tengo/objects/builtin_json.go
@@ -0,0 +1,54 @@
+package objects
+
+import (
+ "encoding/json"
+)
+
+// to_json(v object) => bytes
+func builtinToJSON(args ...Object) (Object, error) {
+ if len(args) != 1 {
+ return nil, ErrWrongNumArguments
+ }
+
+ res, err := json.Marshal(objectToInterface(args[0]))
+ if err != nil {
+ return &Error{Value: &String{Value: err.Error()}}, nil
+ }
+
+ return &Bytes{Value: res}, nil
+}
+
+// from_json(data string/bytes) => object
+func builtinFromJSON(args ...Object) (Object, error) {
+ if len(args) != 1 {
+ return nil, ErrWrongNumArguments
+ }
+
+ var target interface{}
+
+ switch o := args[0].(type) {
+ case *Bytes:
+ err := json.Unmarshal(o.Value, &target)
+ if err != nil {
+ return &Error{Value: &String{Value: err.Error()}}, nil
+ }
+ case *String:
+ err := json.Unmarshal([]byte(o.Value), &target)
+ if err != nil {
+ return &Error{Value: &String{Value: err.Error()}}, nil
+ }
+ default:
+ return nil, ErrInvalidArgumentType{
+ Name: "first",
+ Expected: "bytes/string",
+ Found: args[0].TypeName(),
+ }
+ }
+
+ res, err := FromInterface(target)
+ if err != nil {
+ return nil, err
+ }
+
+ return res, nil
+}