Closed jim-minter closed 4 years ago
AddExt is deprecated.
Please use SetInterfaceExt instead.
Also, what you have are not the encoder being called twice. But one of the callbacks is being called as part of the machination. I think it gets confusing if you are thinking of a callback as exclusively for encoding or decoding.
For understanding, see https://github.com/ugorji/go/blob/codec/v1.1.7/codec/decode.go#L1812
Please reply and tag me if you still see an issue here.
I'm using
github.com/ugorji/go/codec
to deserialise a JSON object to a struct containing a*rsa.PrivateKey
field. In the raw JSON, the private key appears as a base64 encoded string containing the DER bytes.I can use either of
codec.JsonHandle.AddExt
orcodec.JsonHandle.SetInterfaceExt
to customise serialisation/deserialisation to handle the above case. When I encode (from struct to JSON), I find that the encoder gets called as expected. However, I find that when I decode, the encoder unexpectedly gets called with a&rsa.PrivateKey{}
, then the decoder is called as expected.This is problematic in the
*rsa.PrivateKey
case as the zero valuersa.PrivateKey
is invalid andx509.MarshalPKCS1PrivateKey
panics when attempting to marshal it. More widely, I wonder if it's a logic error forgithub.com/ugorji/go/codec
to call the encoder on decode?The following shows the issue. The section commented
// Why is this needed?
is a workaround:Sample output is:
In the above output, I would expect to see the text
encoding...
once only.