Skip to content

Commit ea7a5fa

Browse files
committed
Change easyjson -> jsonv2
1 parent 141d51e commit ea7a5fa

12 files changed

+418
-557
lines changed

fixup/fixup.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
// where the package name is a prefix (ie, `CSS` domain).
3030
// - add Error() method to `Runtime.ExceptionDetails` so that it can be used
3131
// as error.
32-
// - change `Network.Headers` type to map[string]interface{}.
32+
// - change `Network.Headers` type to map[string]any.
3333
// - add special unmarshaler to `Network.CookiePartitionKey` type to handle
3434
// different versions.
3535
//
@@ -241,10 +241,10 @@ const ModifierCommand Modifier = ModifierMeta
241241
t.Extra += gotpl.ExtraTimestampTemplate(t, d)
242242
}
243243

244-
// change Headers to be a map[string]interface{}
244+
// change Headers to be a map[string]any
245245
if t.Name == "Headers" {
246246
t.Type = pdl.TypeAny
247-
t.Ref = "map[string]interface{}"
247+
t.Ref = "map[string]any"
248248
}
249249

250250
// change Response
@@ -259,7 +259,7 @@ const ModifierCommand Modifier = ModifierMeta
259259
// add unmarshaler for CookiePartitionKey type
260260
if t.Name == "CookiePartitionKey" {
261261
t.Extra += gotpl.ExtraCookiePartitionKeyUnmarshaler(snaker.ForceCamelIdentifier(t.Name))
262-
t.SwapEasyJSONUnmarshaler = true
262+
t.SwapUnmarshaler = true
263263
}
264264
}
265265

gen/gogen.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,11 @@ func (fb fileBuffers) get(s string, pkgName string, d *pdl.Domain, domains []*pd
179179

180180
// add import map
181181
importMap := map[string]string{
182-
"encoding/json": "",
183-
basePkg + "/cdp": "",
184-
"github.com/mailru/easyjson": "",
185-
"github.com/mailru/easyjson/jlexer": "",
186-
"github.com/mailru/easyjson/jwriter": "",
187-
"github.com/chromedp/sysutil": "",
182+
"encoding/json": "",
183+
basePkg + "/cdp": "",
184+
"github.com/chromedp/sysutil": "",
185+
"github.com/go-json-experiment/json": "jsonv2",
186+
"github.com/go-json-experiment/json/jsontext": "",
188187
}
189188
// add io only for cdp package
190189
if pkgName == "cdp" {

gen/gotpl/extra.qtpl

+34-55
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"github.com/chromedp/cdproto-gen/pdl"
44
) %}
55

6-
// ExtraTimestampTemplate is a special template for the Timestamp type that
6+
// ExtraTimestampTemplate is a special template for the [Timestamp] type that
77
// defines its JSON unmarshaling.
88
{% func ExtraTimestampTemplate(t *pdl.Type, d *pdl.Domain) %}{%code
99
typ := t.Name
@@ -24,28 +24,21 @@ func init() {
2424
}
2525
{% endif %}
2626

27-
// MarshalEasyJSON satisfies easyjson.Marshaler.
28-
func (t {%s= typ %}) MarshalEasyJSON(out *jwriter.Writer) {
27+
// MarshalText satisfies [encoding.TextMarshaler].
28+
func (t {%s= typ %}) MarshalText() ([]byte, error) {
2929
v := {% if monotonic %}float64(time.Time(t).Sub(*{%s= typ %}Epoch))/float64(time.Second){% else %}float64(time.Time(t).UnixNano()/int64({%s= timeRes %})){% endif %}
30-
31-
out.Buffer.EnsureSpace(20)
32-
out.Buffer.Buf = strconv.AppendFloat(out.Buffer.Buf, v, 'f', -1, 64)
33-
}
34-
35-
// MarshalJSON satisfies json.Marshaler.
36-
func (t {%s= typ %}) MarshalJSON() ([]byte, error) {
37-
return easyjson.Marshal(t)
38-
}
39-
40-
// UnmarshalEasyJSON satisfies easyjson.Unmarshaler.
41-
func (t *{%s= typ %}) UnmarshalEasyJSON(in *jlexer.Lexer) {{% if monotonic %}
42-
*t = {%s= typ %}({%s= typ %}Epoch.Add(time.Duration(in.Float64()*float64(time.Second)))){% else %}
43-
*t = {%s= typ %}(time.Unix(0, int64(in.Float64()*float64({%s= timeRes %})))){% endif %}
30+
return strconv.AppendFloat(make([]byte, 20), v, 'f', -1, 64), nil
4431
}
4532

46-
// UnmarshalJSON satisfies json.Unmarshaler.
47-
func (t *{%s= typ %}) UnmarshalJSON(buf []byte) error {
48-
return easyjson.Unmarshal(buf, t)
33+
// UnmarshalText satisfies [encoding.TextUnmarshaler].
34+
func (t *{%s= typ %}) UnmarshalText(buf []byte) error {
35+
f, err := strconv.ParseFloat(string(buf), 64)
36+
if err != nil {
37+
return err
38+
}{% if monotonic %}
39+
*t = {%s= typ %}({%s= typ %}Epoch.Add(time.Duration(f*float64(time.Second)))){% else %}
40+
*t = {%s= typ %}(time.Unix(0, int64(f*float64({%s= timeRes %})))){% endif %}
41+
return nil
4942
}
5043
{% endfunc %}
5144

@@ -333,40 +326,32 @@ const EmptyNodeID = NodeID(0)
333326

334327
// ExtraFixStringUnmarshaler is a template that forces values to be parsed properly.
335328
{% func ExtraFixStringUnmarshaler(typ, parseFunc, extra string) %}
336-
// UnmarshalEasyJSON satisfies easyjson.Unmarshaler.
337-
func (t *{%s= typ %}) UnmarshalEasyJSON(in *jlexer.Lexer) {
338-
buf := in.Raw()
329+
// UnmarshalText satisfies [encoding.TextUnmarshaler].
330+
func (t *{%s= typ %}) UnmarshalText(buf []byte) error {
339331
if l := len(buf); l > 2 && buf[0] == '"' && buf[l-1] == '"' {
340332
buf = buf[1:l-1]
341333
}
342334
{% if parseFunc != "" %}
343335
v, err := strconv.{%s= parseFunc %}(string(buf){%s= extra %})
344336
if err != nil {
345-
in.AddError(err)
337+
return err
346338
}
347339
{% endif %}
348340
*t = {%s= typ %}({% if parseFunc != "" %}v{% else %}buf{% endif %})
349-
}
350-
351-
// UnmarshalJSON satisfies json.Unmarshaler.
352-
func (t *{%s= typ %}) UnmarshalJSON(buf []byte) error {
353-
return easyjson.Unmarshal(buf, t)
341+
return nil
354342
}
355343
{% endfunc %}
356344

357345

358346
// ExtraCookiePartitionKeyUnmarshaler is a template that handles unmarshaling
359347
// Network.CookiePartitionKey.
360348
{% func ExtraCookiePartitionKeyUnmarshaler(typ string) %}
361-
// UnmarshalEasyJSON satisfies easyjson.Unmarshaler.
362-
func (t *{%s= typ %}) UnmarshalEasyJSONZZ(in *jlexer.Lexer) {
363-
buf := in.Raw()
349+
// UnmarshalText satisfies [encoding.TextUnmarshaler].
350+
func (t *{%s= typ %}) UnmarshalText(buf []byte) error {
364351
if l := len(buf); l > 2 && buf[0] == '"' && buf[l-1] == '"' {
365352
var err error
366-
if t.TopLevelSite, err = strconv.Unquote(string(buf)); err != nil {
367-
in.AddError(err)
368-
}
369-
return
353+
t.TopLevelSite, err = strconv.Unquote(string(buf));
354+
return err
370355
}
371356
dec := json.NewDecoder(bytes.NewReader(buf))
372357
dec.DisallowUnknownFields()
@@ -375,10 +360,10 @@ func (t *{%s= typ %}) UnmarshalEasyJSONZZ(in *jlexer.Lexer) {
375360
HasCrossSiteAncestor bool `json:"hasCrossSiteAncestor"`
376361
}
377362
if err := dec.Decode(&v); err != nil {
378-
in.AddError(err)
379-
} else {
380-
t.TopLevelSite, t.HasCrossSiteAncestor = v.TopLevelSite, v.HasCrossSiteAncestor
381-
}
363+
return err
364+
}
365+
t.TopLevelSite, t.HasCrossSiteAncestor = v.TopLevelSite, v.HasCrossSiteAncestor
366+
return nil
382367
}
383368
{% endfunc %}
384369

@@ -389,7 +374,7 @@ func (t *{%s= typ %}) UnmarshalEasyJSONZZ(in *jlexer.Lexer) {
389374
// Executor is the common interface for executing a command.
390375
type Executor interface {
391376
// Execute executes the command.
392-
Execute(context.Context, string, easyjson.Marshaler, easyjson.Unmarshaler) error
377+
Execute(context.Context, string, any, any) error
393378
}
394379

395380
// contextKey is the context key type.
@@ -412,7 +397,7 @@ func ExecutorFromContext(ctx context.Context) Executor {
412397

413398
// Execute uses the context's message executor to send a command or event
414399
// method marshaling the provided parameters, and unmarshaling to res.
415-
func Execute(ctx context.Context, method string, params easyjson.Marshaler, res easyjson.Unmarshaler) error {
400+
func Execute(ctx context.Context, method string, params, res any) error {
416401
if executor := ctx.Value(executorKey); executor != nil {
417402
return executor.(Executor).Execute(ctx, method, params, res)
418403
}
@@ -465,37 +450,31 @@ type empty struct{}
465450
var emptyVal = &empty{}
466451

467452
// UnmarshalMessage unmarshals the message result or params.
468-
func UnmarshalMessage(msg *Message) (interface{}, error) {
469-
var v easyjson.Unmarshaler
453+
func UnmarshalMessage(msg *Message) (any, error) {
454+
var v any
470455
switch msg.Method {{% for _, d := range domains %}{% for _, c := range d.Commands %}
471456
case {%s= CommandMethodType(c, d) %}:{% if len(c.Returns) == 0 %}
472457
return emptyVal, nil{% else %}
473-
v = new({%s= genutil.PackageName(d) %}.{%s= CommandReturnsType(c) %}){% endif %}
474-
{% endfor %}{% for _, e := range d.Events %}
458+
v = new({%s= genutil.PackageName(d) %}.{%s= CommandReturnsType(c) %}){% endif %}{% endfor %}{% for _, e := range d.Events %}
475459
case {%s= EventMethodType(e, d) %}:
476-
v = new({%s= genutil.PackageName(d) %}.{%s= EventType(e) %})
477-
{% endfor %}{% endfor %}
460+
v = new({%s= genutil.PackageName(d) %}.{%s= EventType(e) %}){% endfor %}
461+
{% endfor %}
478462
default:
479463
return nil, cdp.ErrUnknownCommandOrEvent(msg.Method)
480464
}
481465

482-
var buf easyjson.RawMessage
466+
var buf jsontext.Value
483467
switch {
484468
case msg.Params != nil:
485469
buf = msg.Params
486-
487470
case msg.Result != nil:
488471
buf = msg.Result
489-
490472
default:
491473
return nil, cdp.ErrMsgMissingParamsOrResult
492474
}
493-
494-
err := easyjson.Unmarshal(buf, v)
495-
if err != nil {
475+
if err := jsonv2.Unmarshal(buf, v); err != nil {
496476
return nil, err
497477
}
498-
499478
return v, nil
500479
}
501480
{% endfunc %}

0 commit comments

Comments
 (0)