-
Notifications
You must be signed in to change notification settings - Fork 1.6k
support custom JSON (de)serialization by delegating to message if it implements json.Marshaler/json.Unmarshaler #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
e7d161e
e8c65fd
cbc0d77
a26be53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,22 @@ type Marshaler struct { | |
OrigName bool | ||
} | ||
|
||
// JSONPBMarshaler is implemented by protobuf messages that customize the | ||
// way they are marshaled to JSON. Messages that implement this should | ||
// also implement JSONPBUnmarshaler so that the custom format can be | ||
// parsed. | ||
type JSONPBMarshaler interface { | ||
MarshalJSONPB(*Marshaler) ([]byte, error) | ||
} | ||
|
||
// JSONPBUnmarshaler is implemented by protobuf messages that customize | ||
// the way they are unmarshaled from JSON. Messages that implement this | ||
// should also implement JSONPBMarshaler so that the custom format can be | ||
// produced. | ||
type JSONPBUnmarshaler interface { | ||
UnmarshalJSONPB(*Unmarshaler, []byte) error | ||
} | ||
|
||
// Marshal marshals a protocol buffer into JSON. | ||
func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error { | ||
writer := &errWriter{writer: out} | ||
|
@@ -102,6 +118,15 @@ type wkt interface { | |
|
||
// marshalObject writes a struct to the Writer. | ||
func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeURL string) error { | ||
if jsm, ok := v.(JSONPBMarshaler); ok { | ||
b, err := jsm.MarshalJSONPB(m) | ||
if err != nil { | ||
return err | ||
} | ||
out.write(string(b)) | ||
return out.err | ||
} | ||
|
||
s := reflect.ValueOf(v).Elem() | ||
|
||
// Handle well-known types. | ||
|
@@ -528,6 +553,9 @@ func (u *Unmarshaler) UnmarshalNext(dec *json.Decoder, pb proto.Message) error { | |
if err := dec.Decode(&inputValue); err != nil { | ||
return err | ||
} | ||
if jsu, ok := pb.(JSONPBUnmarshaler); ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about moving this inside unmarshalValue instead? And I think it may need to be after block that allocates memory for pointer fields, i.e. 597-600. You can do type assertion using ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch |
||
return jsu.UnmarshalJSONPB(u, []byte(inputValue)) | ||
} | ||
return u.unmarshalValue(reflect.ValueOf(pb).Elem(), inputValue, nil) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cybrcodr, I wasn't sure if this struct could ever become stateful, so I have it passed by ref. Should this instead be pass-by-value? I know it currently just represents marshaling options; but it seemed like, from the name, it could at some point change to such that a pointer is the correct way to pass it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(same goes for passing
Unmarshaler
by pointer in the other interface)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass by ref sounds good.