-
Notifications
You must be signed in to change notification settings - Fork 16
/
marshal.go
executable file
·66 lines (58 loc) · 1.85 KB
/
marshal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package trace
import (
"context"
"encoding/json"
"fmt"
"os"
"reflect"
"github.com/xhd2015/xgo/runtime/core"
"github.com/xhd2015/xgo/runtime/functab"
"github.com/xhd2015/xgo/runtime/trap"
)
// MarshalAnyJSON marshals aribitray go value `v`
// to JSON, when it encounters unmarshalable values
// like func, chan, it will bypass these values.
func MarshalAnyJSON(v interface{}) ([]byte, error) {
newTypeEncoder := functab.Info("encoding/json", "newTypeEncoder")
if newTypeEncoder == nil {
fmt.Fprintf(os.Stderr, "WARNING: encoding/json.newTypeEncoder not trapped(requires xgo).\n")
return json.Marshal(v)
}
// get the unmarshalable function
unmarshalable := getMarshaler(newTypeEncoder.Func, reflect.TypeOf(unmarshalableFunc))
var data []byte
var err error
// mock the encoding json
trap.WithFuncOverride(newTypeEncoder, &trap.Interceptor{
// Pre: func(ctx context.Context, f *core.FuncInfo, args, result core.Object) (interface{}, error) {
// return nil, nil
// },
Post: func(ctx context.Context, f *core.FuncInfo, args, result core.Object, data interface{}) error {
if f != newTypeEncoder {
return nil
}
resField := result.GetFieldIndex(0)
// if unmarshalable, replace with an empty struct
if reflect.ValueOf(resField.Value()).Pointer() == reflect.ValueOf(unmarshalable).Pointer() {
resField.Set(getMarshaler(newTypeEncoder.Func, reflect.TypeOf(struct{}{})))
}
return nil
},
}, func() {
data, err = json.Marshal(v)
})
return data, err
}
func unmarshalableFunc() {}
// newTypeEncoder signature: func(x Type,allowAddr bool) func()
func getMarshaler(newTypeEncoder interface{}, v reflect.Type) interface{} {
var res interface{}
trap.Direct(func() {
results := reflect.ValueOf(newTypeEncoder).Call([]reflect.Value{
reflect.ValueOf(v),
reflect.ValueOf(false),
})
res = results[0].Interface()
})
return res
}