-
Notifications
You must be signed in to change notification settings - Fork 13
/
json.go
243 lines (214 loc) · 7.77 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package csproto
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
gogojson "github.com/gogo/protobuf/jsonpb"
gogo "github.com/gogo/protobuf/proto"
"github.com/golang/protobuf/jsonpb" //nolint: staticcheck // using this deprecated package intentionally as this is a compatibility shim
protov1 "github.com/golang/protobuf/proto" //nolint: staticcheck // using this deprecated package intentionally as this is a compatibility shim
"google.golang.org/protobuf/encoding/protojson"
protov2 "google.golang.org/protobuf/proto"
)
// JSONMarshaler returns an implementation of the json.Marshaler interface that formats msg to JSON
// using the specified options.
func JSONMarshaler(msg interface{}, opts ...JSONOption) json.Marshaler {
m := jsonMarshaler{
msg: msg,
}
for _, o := range opts {
o(&m.opts)
}
return &m
}
// jsonMarshaler wraps a Protobuf message and satisfies the json.Marshaler interface
type jsonMarshaler struct {
msg interface{}
opts jsonOptions
}
// compile-time interface check
var _ json.Marshaler = (*jsonMarshaler)(nil)
// MarshalJSON satisfies the json.Marshaler interface
//
// If the wrapped message is nil, or a non-nil interface value holding nil, this method returns nil.
// If the message satisfies the json.Marshaler interface we delegate to it directly. Otherwise,
// this method calls the appropriate underlying runtime (Gogo vs Google V1 vs Google V2) based on
// the message's actual type.
func (m *jsonMarshaler) MarshalJSON() ([]byte, error) {
value := reflect.ValueOf(m.msg)
if m.msg == nil || value.Kind() == reflect.Ptr && value.IsNil() {
return nil, nil
}
// call the message's implementation directly, if present
if jm, ok := m.msg.(json.Marshaler); ok {
return jm.MarshalJSON()
}
var buf bytes.Buffer
// Google V2 message?
if msg, isV2 := m.msg.(protov2.Message); isV2 {
mo := protojson.MarshalOptions{
Indent: m.opts.indent,
UseEnumNumbers: m.opts.useEnumNumbers,
EmitUnpopulated: m.opts.emitZeroValues,
}
b, err := mo.Marshal(msg)
if err != nil {
return nil, fmt.Errorf("unable to marshal message to JSON: %w", err)
}
return b, nil
}
// Google V1 message?
if msg, isV1 := m.msg.(protov1.Message); isV1 {
jm := jsonpb.Marshaler{
Indent: m.opts.indent,
EnumsAsInts: m.opts.useEnumNumbers,
EmitDefaults: m.opts.emitZeroValues,
}
if err := jm.Marshal(&buf, msg); err != nil {
return nil, fmt.Errorf("unable to marshal message to JSON: %w", err)
}
return buf.Bytes(), nil
}
// Gogo message?
if msg, isGogo := m.msg.(gogo.Message); isGogo {
jm := gogojson.Marshaler{
Indent: m.opts.indent,
EnumsAsInts: m.opts.useEnumNumbers,
EmitDefaults: m.opts.emitZeroValues,
}
if err := jm.Marshal(&buf, msg); err != nil {
return nil, fmt.Errorf("unable to marshal message to JSON: %w", err)
}
return buf.Bytes(), nil
}
return nil, fmt.Errorf("unsupported message type %T", m.msg)
}
// JSONUnmarshaler returns an implementation of the json.Unmarshaler interface that unmarshals a
// JSON data stream into msg using the specified options.
func JSONUnmarshaler(msg interface{}, opts ...JSONOption) json.Unmarshaler {
m := jsonUnmarshaler{
msg: msg,
}
for _, o := range opts {
o(&m.opts)
}
return &m
}
// jsonUnmarshaler wraps a Protobuf message and satisfies the json.Unmarshaler interface
type jsonUnmarshaler struct {
msg interface{}
opts jsonOptions
}
// UnmarshalJSON satisfies the json.Unmarshaler interface.
//
// If the wrapped message is nil, or a non-nil interface value holding nil, this method returns an error.
// If the message satisfies the json.Marshaler interface we delegate to it directly. Otherwise,
// this method calls the appropriate underlying runtime (Gogo vs Google V1 vs Google V2) based on
// the message's actual type.
func (m *jsonUnmarshaler) UnmarshalJSON(data []byte) error {
if m.msg == nil || reflect.ValueOf(m.msg).IsNil() {
return fmt.Errorf("cannot unmarshal into nil")
}
// call the message's implementation directly, if present
if jm, ok := m.msg.(json.Unmarshaler); ok {
return jm.UnmarshalJSON(data)
}
// Google V2 message?
if msg, isV2 := m.msg.(protov2.Message); isV2 {
mo := protojson.UnmarshalOptions{
AllowPartial: m.opts.allowPartial,
DiscardUnknown: m.opts.allowUnknownFields,
}
if err := mo.Unmarshal(data, msg); err != nil {
return fmt.Errorf("unable to unmarshal JSON data: %w", err)
}
return nil
}
// Google V1 message?
if msg, isV1 := m.msg.(protov1.Message); isV1 {
jm := jsonpb.Unmarshaler{
AllowUnknownFields: m.opts.allowUnknownFields,
}
if err := jm.Unmarshal(bytes.NewReader(data), msg); err != nil {
return fmt.Errorf("unable to unmarshal JSON data: %w", err)
}
return nil
}
// Gogo message?
if msg, isGogo := m.msg.(gogo.Message); isGogo {
jm := gogojson.Unmarshaler{
AllowUnknownFields: m.opts.allowUnknownFields,
}
if err := jm.Unmarshal(bytes.NewBuffer(data), msg); err != nil {
return fmt.Errorf("unable to unmarshal JSON data: %w", err)
}
return nil
}
return fmt.Errorf("unsupported message type %T", m.msg)
}
// JSONOption defines a function that sets a specific JSON formatting option
type JSONOption func(*jsonOptions)
// JSONIndent returns a JSONOption that configures the JSON indentation.
//
// Passing an empty string disables indentation. If not empty, indent must consist of only spaces or
// tab characters.
func JSONIndent(indent string) JSONOption {
return func(opts *jsonOptions) {
opts.indent = indent
}
}
// JSONUseEnumNumbers returns a JSON option that enables or disables outputting integer values rather
// than the enum names for enum fields.
func JSONUseEnumNumbers(useNumbers bool) JSONOption {
return func(opts *jsonOptions) {
opts.useEnumNumbers = useNumbers
}
}
// JSONIncludeZeroValues returns a JSON option that enables or disables including zero-valued fields
// in the JSON output.
func JSONIncludeZeroValues(emitZeroValues bool) JSONOption {
return func(opts *jsonOptions) {
opts.emitZeroValues = emitZeroValues
}
}
// JSONAllowUnknownFields returns a JSON option that configures JSON unmarshaling to skip unknown
// fields rather than return an error
func JSONAllowUnknownFields(allow bool) JSONOption {
return func(opts *jsonOptions) {
opts.allowUnknownFields = allow
}
}
// JSONAllowPartialMessages returns a JSON option that configured JSON unmarshaling to not return an
// error if unmarshaling data results in required fields not being set on the message.
//
// Note: only applies to Google V2 (google.golang.org/protobuf) messages that are using proto2 syntax.
func JSONAllowPartialMessages(allow bool) JSONOption {
return func(opts *jsonOptions) {
opts.allowPartial = allow
}
}
// jsonOptions defines the JSON formatting options
//
// These options are a subset of those available by each of the three supported runtimes. The supported
// options consist of the things that are provided by all 3 runtimes in the same manner. If you need
// the full spectrum of the formatting options you will need to use the appropriate runtime.
//
// The zero value results in no indentation, enum values using the enum names, and not including
// zero-valued fields in the output.
type jsonOptions struct {
// If set, generate multi-line output such that each field is prefixed by indent and terminated
// by a newline
indent string
// If true, enum fields will be output as integers rather than the enum value names
useEnumNumbers bool
// If true, include zero-valued fields in the JSON output
emitZeroValues bool
// If true, unknown fields will be discarded when unmarshaling rather than unmarshaling returning
// an error
allowUnknownFields bool
// If true, unmarshaled messages with missing required fields will not return an error
//
// Note: only applies to Google V2 (google.golang.org/protobuf) messages that are using proto2 syntax.
allowPartial bool
}