Skip to content

Commit 3ecb830

Browse files
Remove jsonMetadata
Since we now have metadata.UnmarshalJSON there is no actual need for a jsonWrapper since we can use the easyjson lexer directly. This improve performance a little more from: `BenchmarkJSONMetadata_UnmarshalJSON-4 2000000 709 ns/op 408 B/op 12 allocs/op` to `BenchmarkJSONMetadata_UnmarshalJSON-4 2000000 615 ns/op 256 B/op 9 allocs/op`
1 parent 3f9643d commit 3ecb830

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

metadata/metadata.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package metadata
33
import (
44
"encoding/json"
55

6-
"github.com/mailru/easyjson"
76
"github.com/mailru/easyjson/jlexer"
87
)
98

@@ -100,23 +99,7 @@ func (v *valueData) MarshalJSON() ([]byte, error) {
10099
}
101100

102101
func UnmarshalJSON(json []byte) (Metadata, error) {
103-
wrapper := jsonMetadata{
104-
Metadata: New(),
105-
}
106-
107-
if err := easyjson.Unmarshal(json, &wrapper); err != nil {
108-
return nil, err
109-
}
110-
return wrapper.Metadata, nil
111-
}
112-
113-
// jsonMetadata is a special struct to UnmarshalJSON metadata
114-
type jsonMetadata struct {
115-
Metadata Metadata
116-
}
117-
118-
// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
119-
func (j *jsonMetadata) UnmarshalEasyJSON(in *jlexer.Lexer) {
102+
in := jlexer.Lexer{Data: json}
120103
metadata := New()
121104

122105
isTopLevel := in.IsStart()
@@ -125,8 +108,9 @@ func (j *jsonMetadata) UnmarshalEasyJSON(in *jlexer.Lexer) {
125108
in.Consumed()
126109
}
127110
in.Skip()
128-
return
111+
return metadata, in.Error()
129112
}
113+
130114
in.Delim('{')
131115
for !in.IsDelim('}') {
132116
key := in.UnsafeString()
@@ -135,9 +119,10 @@ func (j *jsonMetadata) UnmarshalEasyJSON(in *jlexer.Lexer) {
135119
in.WantComma()
136120
}
137121
in.Delim('}')
122+
138123
if isTopLevel {
139124
in.Consumed()
140125
}
141126

142-
j.Metadata = metadata
127+
return metadata, in.Error()
143128
}

0 commit comments

Comments
 (0)