ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.85k stars 295 forks source link

codec v1.2.3 cannot encode nested json.RawMessage #353

Closed vdobler closed 3 years ago

vdobler commented 3 years ago

If optimized encoders and decoders for the types S and T in the following code are generated via codecgen then the function Bug prints malformed JSON {"t":} without error.

package bug 

//go:generate codecgen -o codec.generated.go -d 9552 data.go

type S struct {
    T T `json:"t"`
}

type T struct {
    JRM json.RawMessage
}

func Bug() {
    s := S{
        T: T{
            JRM: json.RawMessage{0x37, 0x38, 0x33},
        },
    }

    jsonHandle := new(codec.JsonHandle)
    enc := codec.NewEncoder(os.Stdout, jsonHandle)
    err := enc.Encode(s)
    if err != nil {
        panic(err)
    }
}

Note that encoding s.T works properly and produces {"JRM":783}.

Note that the bug was not present in version 1.1.7

ugorji commented 3 years ago

Fixed by change fff0ea1 above

vdobler commented 3 years ago

@ugorji Thanks for the quick fix!

Maybe it might be worth adding a test of some sort to prevent this problem from reoccurring?