Skip to content

Commit 79c042d

Browse files
Use easyjson.Unmarshaler, json.Unmarshaler or json.Unmarshal
In order to allow for faster JSON unmarshalling use `internal.UnmarshalJSON` which will pick the best possible json unmarshalling action.
1 parent 440e0ea commit 79c042d

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

driver/sql/projection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ func (p *ProjectionNotification) UnmarshalEasyJSON(in *jlexer.Lexer) {
8787
}
8888
switch key {
8989
case "no":
90-
p.No = int64(in.Int64())
90+
p.No = in.Int64()
9191
case "aggregate_id":
92-
p.AggregateID = string(in.String())
92+
p.AggregateID = in.String()
9393
default:
9494
in.SkipRecursive()
9595
}

strategy/json/internal/json.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,18 @@ func MarshalJSON(v interface{}) ([]byte, error) {
2222

2323
return json.Marshal(v)
2424
}
25+
26+
// UnmarshalJSON parses the JSON-encoded data and stores the result
27+
// in the value pointed to by v.
28+
// This is done using `easyjson.Unmarshal`, `json.Unmarshaler` or `json.Unmarshal`
29+
func UnmarshalJSON(data []byte, v interface{}) error {
30+
if vm, ok := v.(easyjson.Unmarshaler); ok {
31+
return easyjson.Unmarshal(data, vm)
32+
}
33+
34+
if vm, ok := v.(json.Unmarshaler); ok {
35+
return vm.UnmarshalJSON(data)
36+
}
37+
38+
return json.Unmarshal(data, v)
39+
}

strategy/json/payload_transformer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ func (p *PayloadTransformer) CreatePayload(typeName string, data interface{}) (i
146146

147147
// Pointer we can handle nicely
148148
if payloadType.isPtr {
149-
if err := json.Unmarshal(dataBytes, payload); err != nil {
149+
if err := internal.UnmarshalJSON(dataBytes, payload); err != nil {
150150
return nil, err
151151
}
152152
}
153153

154154
// Not a pointer so let's cry and use reflection
155155
vp := reflect.New(payloadType.reflectionType)
156156
vp.Elem().Set(reflect.ValueOf(payload))
157-
if err := json.Unmarshal(dataBytes, vp.Interface()); err != nil {
157+
if err := internal.UnmarshalJSON(dataBytes, vp.Interface()); err != nil {
158158
return nil, err
159159
}
160160

0 commit comments

Comments
 (0)