francoispqt / gojay

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

Problem with gojay generator #80

Closed xocasdashdash closed 5 years ago

xocasdashdash commented 5 years ago

Sorry I closed the wrong dupe: I'm trying to decode this structure

type MyVeryOwnMap map[string]string

type MyStruct struct { Properties MyVeryOwnMap gojay:"properties" } In the end the gojay generator is trying to generate this code:

enc.AnyKey("properties", v.Properties)

But that method is nowhere, How can I get gojay to encode (and decode) a map[string]string

Is the generator the right way of using this library? Thanks!

francoispqt commented 5 years ago

HI, sorry for the time it took to answer. The generator is limited at the moment and we're working on a much thorough implementation.

For now, you can implement it yourself this way:

type MyVeryOwnMap map[string]string

type MyStruct struct {
    Properties MyVeryOwnMap gojay:"properties"
}

func (m *MyStruct) MarshalJSONObject(enc *gojay.Encoder) {
    enc.ObjectKey("properties", m.Properties)
}

func (m *MyStruct) IsNil() bool {
    return m == nil
}

func (m MyVeryOwnMap) MarshalJSONObject(enc *gojay.Encoder) {
    for k, v := range m {
        enc.StringKey(k, v)
    }
}

func (m MyVeryOwnMap) IsNil() bool {
    return m == nil
}

Thanks