ugorji / go

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

Encoding same struct with different tags does not work #256

Closed masterada closed 6 years ago

masterada commented 6 years ago

Description

Json encoding the same struct twice with different tags does not work, first tag is used for second encode as well.

Version: v1.1.1

Test case

    type t struct {
        Field string `foo:"field_foo" bar:"field_bar"`
    }

    v := &t{
        Field: "value",
    }

    handle := &codec.JsonHandle{}
    handle.TypeInfos = codec.NewTypeInfos([]string{"foo"})
    var b []byte
    enc := codec.NewEncoderBytes(&b, handle)
    enc.Encode(v)
    log.Print(string(b))

    handle = &codec.JsonHandle{}
    handle.TypeInfos = codec.NewTypeInfos([]string{"bar"})
    b = nil
    enc = codec.NewEncoderBytes(&b, handle)
    enc.Encode(v)
    log.Print(string(b))

Got

{"field_foo":"value"}
{"field_foo":"value"}

Expected

{"field_foo":"value"}
{"field_bar":"value"}
ugorji commented 6 years ago

Phew. Good catch.

Fix coming shortly.