francoispqt / gojay

high performance JSON encoder/decoder with stream API for Golang
MIT License
2.11k stars 112 forks source link

Code generation giving bad key strings #53

Closed veqryn closed 6 years ago

veqryn commented 6 years ago

This example code:

type Record struct {
    TS   int64   `json:"ts,omitempty"`  // Timestamp in seconds
    RID  string  `json:"rid,omitempty"`  // R-ID
}

Gives this generated code:

func (v *Record) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
    switch k {
    case "tS":
        return dec.Int64(&v.TS)
    case "rID":
        return dec.String(&v.RID)
    }
    return nil
}

func (v *Record) MarshalJSONObject(enc *gojay.Encoder) {
    enc.Int64Key("tS", v.TS)
    enc.StringKey("rID", v.RID)
}

When it should give:

func (v *Record) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
    switch k {
    case "tS":
        return dec.Int64(&v.TS)
    case "rID":
        return dec.String(&v.RID)
    }
    return nil
}

func (v *Record) MarshalJSONObject(enc *gojay.Encoder) {
    enc.Int64Key("ts", v.TS)
    enc.StringKeyOmitEmpty("rid", v.RID)
}

The code generator should be pulling the json tag and parsing it to determine the correct key string, as well as other things like omitempty, or fields to skip (-).

francoispqt commented 6 years ago

There is a gojay tag for code generation. See the documentation here: https://github.com/francoispqt/gojay/tree/master/gojay