Open
Description
When attempting to use writeBinary() for a property name, the following exception is thrown:
com.fasterxml.jackson.core.JsonGenerationException: Can not write a binary value, expecting field name (context: Object)
at com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator.java:1897)
at com.fasterxml.jackson.core.json.JsonGeneratorImpl._reportCantWriteValueExpectName(JsonGeneratorImpl.java:244)
at com.fasterxml.jackson.core.json.JsonGeneratorImpl._verifyPrettyValueWrite(JsonGeneratorImpl.java:234)
at com.fasterxml.jackson.core.json.UTF8JsonGenerator._verifyValueWrite(UTF8JsonGenerator.java:1004)
at com.fasterxml.jackson.core.json.UTF8JsonGenerator.writeBinary(UTF8JsonGenerator.java:763)
at com.fasterxml.jackson.core.JsonGenerator.writeBinary(JsonGenerator.java:1144)
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.