-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
JsonGenerator
cannot write binary-valued property names
#1553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Ok, this would really belong in
But beyond that there is no intent to support writing of arbitrary and untrusted bytes as names: not all backends would support it (for example, when It is doable, however, but only by implementing What I am curious about here is the use case -- what is the intent? Does byte array contain valid properly escape JSON name? Or is just opaque binary data which would require escaping? |
Ah, sorry about the wrong repo. WriteBinary() does not write arbitrary/untrusted bytes (that would be writeRawValue()), instead it encodes the bytes as base64, which results in a pretty safe name (e.g. As I mentioned in the initial comment, the use case is for a workaround to issue #1552, where I have values that are indexed by binary keys. In the world of ASN.1 (e.g. X.509 certificates and electronic signatures), keys are commonly binary values (sequences of arbitrary bytes), and it makes sense to have them as keys (names) in JSON objects after encoding them as a character string in some way (base64, in this case). As Jackson already encodes byte arrays as base64 by default when they occur as values, it would be useful, intuitive and safe to also do so for names. |
@nmatt Ah! Thank you for clarification. I did misunderstand your intent. So, essentially this would be required for, say, this kind of POJO: class BinaryValues {
public Map<byte[],byte[]> data;
} which would use Base64 encoding for textual formats, as you say, but possibly native byte arrays for formats that support this (CBOR perhaps)? I think this makes sense, although then the question would become that of how to expose that via API, and whether it should be something handled at streaming api layer ( So I am open to supporting this in some form, but not yet clear on what would be a good way. |
JsonGenerator
cannot write binary-valued property names
I can see why |
When attempting to use writeBinary() for a property name, the following exception is thrown:
There should probably exist a method writeBinaryFieldName(byte[]) (roughly similar to writeFieldId(long)), as byte arrays serialize to a base64 string, which is perfectly valid for property names in JSON objects.
This error occured when trying to use a custom key serializer to work around issue #1552.
As a workaround,
writeFieldName(Base64Variants.getDefaultVariant().encode(value, false))
can be used. However this is only incidentally safe, as the default base64 variant does not contain newlines. If it would, then they would be doubly escaped, once by Base64Variant#encode(), and once by writeFieldName(). Hence this solution is not entirely clean.The text was updated successfully, but these errors were encountered: