Skip to content
Snippets Groups Projects
Commit dcd562c1 authored by Jono Wenger's avatar Jono Wenger
Browse files

Add JsToJson to convert from a javascript object to JSON

parent 7e27bf72
No related branches found
No related tags found
1 merge request!60Revert "Fail a test to be sure it works"
...@@ -25,6 +25,7 @@ import ( ...@@ -25,6 +25,7 @@ import (
var ( var (
Error = js.Global().Get("Error") Error = js.Global().Get("Error")
JSON = js.Global().Get("JSON")
Promise = js.Global().Get("Promise") Promise = js.Global().Get("Promise")
Uint8Array = js.Global().Get("Uint8Array") Uint8Array = js.Global().Get("Uint8Array")
) )
...@@ -62,15 +63,20 @@ func WrapCB(parent js.Value, m string) func(args ...interface{}) js.Value { ...@@ -62,15 +63,20 @@ func WrapCB(parent js.Value, m string) func(args ...interface{}) js.Value {
} }
// JsonToJS converts a marshalled JSON bytes to a Javascript object. // JsonToJS converts a marshalled JSON bytes to a Javascript object.
func JsonToJS(src []byte) js.Value { func JsonToJS(src []byte) (js.Value, error) {
var inInterface map[string]interface{} var inInterface map[string]interface{}
err := json.Unmarshal(src, &inInterface) err := json.Unmarshal(src, &inInterface)
if err != nil { if err != nil {
Throw(TypeError, err) Throw(TypeError, err)
return js.ValueOf(nil) return js.ValueOf(nil), err
} }
return js.ValueOf(inInterface) return js.ValueOf(inInterface), nil
}
// JsToJson converts the Javascript value to JSON.
func JsToJson(value js.Value) string {
return JSON.Call("stringify", value).String()
} }
type PromiseFn func(resolve, reject func(args ...interface{}) js.Value) type PromiseFn func(resolve, reject func(args ...interface{}) js.Value)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment