forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrappers.go
158 lines (128 loc) · 3.03 KB
/
wrappers.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package json
import (
"bytes"
"reflect"
"sync"
"github.com/Velocidex/json"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
type RawMessage = json.RawMessage
type Marshaler = json.Marshaler
var (
EncoderCallbackSkip = json.EncoderCallbackSkip
bufferPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
)
func GetBuffer() *bytes.Buffer {
buf := bufferPool.Get().(*bytes.Buffer)
buf.Reset()
return buf
}
func PutBuffer(buf *bytes.Buffer) {
bufferPool.Put(buf)
}
func MarshalWithOptions(v interface{}, opts *json.EncOpts) ([]byte, error) {
if opts == nil {
return json.Marshal(v)
}
return json.MarshalWithOptions(v, opts)
}
func Marshal(v interface{}) ([]byte, error) {
opts := DefaultEncOpts()
return json.MarshalWithOptions(v, opts)
}
func MustMarshalIndent(v interface{}) []byte {
result, err := MarshalIndent(v)
if err != nil {
panic(err)
}
return result
}
func MustMarshalString(v interface{}) string {
result, err := Marshal(v)
if err != nil {
panic(err)
}
return string(result)
}
func StringIndent(v interface{}) string {
result, err := MarshalIndent(v)
if err != nil {
panic(err)
}
return string(result)
}
func MarshalIndent(v interface{}) ([]byte, error) {
opts := DefaultEncOpts()
return MarshalIndentWithOptions(v, opts)
}
func MarshalIndentWithOptions(v interface{}, opts *json.EncOpts) ([]byte, error) {
b, err := json.MarshalWithOptions(v, opts)
if err != nil {
return nil, err
}
buf := GetBuffer()
defer PutBuffer(buf)
err = json.Indent(buf, b, "", " ")
if err != nil {
return nil, err
}
// Need to make a copy because the real buffer will be reused in
// the pool.
return CopySlice(buf.Bytes()), nil
}
func MarshalJsonl(v interface{}) ([]byte, error) {
rt := reflect.TypeOf(v)
if rt == nil || rt.Kind() != reflect.Slice && rt.Kind() != reflect.Array {
return nil, json.EncoderCallbackSkip
}
a_slice := reflect.ValueOf(v)
options := DefaultEncOpts()
out := GetBuffer()
defer PutBuffer(out)
for i := 0; i < a_slice.Len(); i++ {
row := a_slice.Index(i).Interface()
serialized, err := json.MarshalWithOptions(row, options)
if err != nil {
return nil, err
}
out.Write(serialized)
out.Write([]byte{'\n'})
}
// Need to make a copy because the real buffer will be reused in
// the pool.
return CopySlice(out.Bytes()), nil
}
func Unmarshal(b []byte, v interface{}) error {
self, ok := v.(proto.Message)
if ok {
options := &protojson.UnmarshalOptions{
DiscardUnknown: true,
}
return options.Unmarshal(b, self)
}
return json.Unmarshal(b, v)
}
// Marshals into a normalized string with sorted keys - this is most
// important for tests.
func MarshalIndentNormalized(v interface{}) ([]byte, error) {
serialized, err := Marshal(v)
if err != nil {
return nil, err
}
data := make(map[string]interface{})
err = Unmarshal(serialized, &data)
if err != nil {
return nil, err
}
return MarshalIndent(data)
}
func CopySlice(in []byte) []byte {
result := make([]byte, len(in))
copy(result, in)
return result
}