mailru / easyjson

Fast JSON serializer for golang.
MIT License
4.46k stars 422 forks source link

why not use map/hash instead of for-loop #395

Open ORESoftware opened 7 months ago

ORESoftware commented 7 months ago

there's a for loop here for every call, like so:

    in.Delim('{')
    for !in.IsDelim('}') {
        key := in.UnsafeFieldName(false)
        in.WantColon()
        if in.IsNull() {
            in.Skip()
            in.WantComma()
            continue
        }
        switch key {
        case "ErrId":
            out.ErrId = string(in.String())
        case "ErrMessage":
            out.ErrMessage = string(in.String())
        default:
            in.SkipRecursive()
        }
        in.WantComma()
    }
    in.Delim('}')

I am wondering why the code generator doesn't just generate map/hash lookup calls and avoid a for-loop altogether? Might be a good reason, not sure yet

so instead, would look like this:

    in.Delim('{')

     out.ErrId = x("ErrId", string(in.String()))
     out.ErrMessage = x("ErrMessage", in.String())

    in.Delim('}')

if a field had no value, then I guess zero value is ok?

ORESoftware commented 7 months ago

I think I know why, it's going character by character; so the for loop is operating on a per character basis.