forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
87 lines (71 loc) · 1.71 KB
/
json.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Wrap json library to control encoding.
package json
import (
"bytes"
"context"
"reflect"
"github.com/Velocidex/json"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter"
"www.velocidex.com/golang/vfilter/protocols"
"www.velocidex.com/golang/vfilter/types"
)
var (
NoEncOpts *json.EncOpts = nil
)
type EncOpts = json.EncOpts
func MarshalJSONDict(v interface{}, opts *json.EncOpts) ([]byte, error) {
if v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr &&
reflect.ValueOf(v).IsNil()) {
return []byte("{}"), nil
}
self, ok := v.(*ordereddict.Dict)
if !ok || self == nil {
return nil, json.EncoderCallbackSkip
}
buf := &bytes.Buffer{}
buf.Write([]byte("{"))
for _, k := range self.Keys() {
// add key
kEscaped, err := json.MarshalWithOptions(k, opts)
if err != nil {
continue
}
buf.Write(kEscaped)
buf.Write([]byte(":"))
// add value
v, ok := self.Get(k)
if !ok {
v = "null"
}
// If v is a callable, run it
callable, ok := v.(func() vfilter.Any)
if ok {
v = callable()
}
vBytes, err := json.MarshalWithOptions(v, opts)
if err == nil {
buf.Write(vBytes)
buf.Write([]byte(","))
} else {
buf.Write([]byte("null,"))
}
}
if len(self.Keys()) > 0 {
buf.Truncate(buf.Len() - 1)
}
buf.Write([]byte("}"))
return buf.Bytes(), nil
}
func MarshalLazyFunctions(v interface{}, opts *json.EncOpts) ([]byte, error) {
lazy_expr, ok := v.(types.LazyExpr)
if ok {
return json.MarshalWithOptions(
lazy_expr.Reduce(context.Background()), opts)
}
return nil, json.EncoderCallbackSkip
}
func init() {
RegisterCustomEncoder(ordereddict.NewDict(), MarshalJSONDict)
RegisterCustomEncoder(&protocols.LazyFunctionWrapper{}, MarshalLazyFunctions)
}