|
|
@ -31,26 +31,35 @@ func init() { |
|
|
|
type codec struct{} |
|
|
|
type codec struct{} |
|
|
|
|
|
|
|
|
|
|
|
func (codec) Marshal(v interface{}) ([]byte, error) { |
|
|
|
func (codec) Marshal(v interface{}) ([]byte, error) { |
|
|
|
if m, ok := v.(proto.Message); ok { |
|
|
|
switch m := v.(type) { |
|
|
|
|
|
|
|
case json.Marshaler: |
|
|
|
|
|
|
|
return m.MarshalJSON() |
|
|
|
|
|
|
|
case proto.Message: |
|
|
|
return MarshalOptions.Marshal(m) |
|
|
|
return MarshalOptions.Marshal(m) |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return json.Marshal(m) |
|
|
|
} |
|
|
|
} |
|
|
|
return json.Marshal(v) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (codec) Unmarshal(data []byte, v interface{}) error { |
|
|
|
func (codec) Unmarshal(data []byte, v interface{}) error { |
|
|
|
rv := reflect.ValueOf(v) |
|
|
|
switch m := v.(type) { |
|
|
|
for rv := rv; rv.Kind() == reflect.Ptr; { |
|
|
|
case json.Unmarshaler: |
|
|
|
if rv.IsNil() { |
|
|
|
return m.UnmarshalJSON(data) |
|
|
|
rv.Set(reflect.New(rv.Type().Elem())) |
|
|
|
case proto.Message: |
|
|
|
} |
|
|
|
|
|
|
|
rv = rv.Elem() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if m, ok := v.(proto.Message); ok { |
|
|
|
|
|
|
|
return UnmarshalOptions.Unmarshal(data, m) |
|
|
|
|
|
|
|
} else if m, ok := reflect.Indirect(rv).Interface().(proto.Message); ok { |
|
|
|
|
|
|
|
return UnmarshalOptions.Unmarshal(data, m) |
|
|
|
return UnmarshalOptions.Unmarshal(data, m) |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
rv := reflect.ValueOf(v) |
|
|
|
|
|
|
|
for rv := rv; rv.Kind() == reflect.Ptr; { |
|
|
|
|
|
|
|
if rv.IsNil() { |
|
|
|
|
|
|
|
rv.Set(reflect.New(rv.Type().Elem())) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
rv = rv.Elem() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if m, ok := reflect.Indirect(rv).Interface().(proto.Message); ok { |
|
|
|
|
|
|
|
return UnmarshalOptions.Unmarshal(data, m) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return json.Unmarshal(data, m) |
|
|
|
} |
|
|
|
} |
|
|
|
return json.Unmarshal(data, v) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (codec) Name() string { |
|
|
|
func (codec) Name() string { |
|
|
|