gopherjs - json/decode.go
1 package json // import "vimagination.zapto.org/gopherjs/json"
2
3 import (
4 "errors"
5
6 "github.com/gopherjs/gopherjs/js"
7 )
8
9 func Unmarshal(data []byte, v interface{}) error {
10 return UnmarshalString(string(data), v)
11 }
12
13 func UnmarshalString(data string, v interface{}) (err error) {
14 defer func() {
15 if e := recover(); e != nil {
16 if er, ok := e.(*js.Error); ok && er != nil {
17 err = er
18 }
19 }
20 }()
21 io := js.InternalObject(v)
22 if io.Get("constructor").Get("kind").Int() != ptrKind { // necessary???
23 return ErrPointerRequired
24 }
25
26 parsed := js.Global.Get("JSON").Call("parse", data)
27 unjsonify(io, parsed)
28 return nil
29 }
30
31 func unjsonify(v, parsed *js.Object) {
32 t := v.Get("constructor")
33 v, t = unwrap(v, t)
34 }
35
36 // Errors
37 var (
38 ErrPointerRequired = errors.New("pointer required")
39 )