I'm using the jackson library for serializing/deserializing to/from JSON. I need that this JSON has the smallest size as possible so I've enabled the feature ALLOW_UNQUOTED_FIELD_NAMES to eliminate all the quotes. I know that removing quotes is not standard json, but making json small is a hard requirement of the project. The generated json works, but when I've trying to read the json value I'm getting an exception:
org.codehaus.jackson.JsonParseException:
Unexpected character ('9' (code 57)):
was expecting either valid name
character (for unquoted name) or
double-quote (for quoted) to start
field name at [Source:
java.io.StringReader@1347d75; line: 1,
column: 3]
The exception above is thrown when I read this json:
{90110a2e-febd-470f-afa4-cf7e890d31b9:0,eec652ad-a4d9-4eb1-8d24-7c1a0c29449f:1}
The way I read it is:
Map<String, Object> valuesMap = oM.readValue(json, new TypeReference<Map<String, Object>>() {});
and the object mapper I use both for reading and writing the values is:
private static final ObjectMapper om = new ObjectMapper();
static {
om.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
om.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
om.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, true);
om.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
om.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
}
I'm using version 1.6.3 of Jackson, in both the sender and the receiver projects. The needed version for this feature is 1.2+ so I thought that maybe I wasn't using this version, but my receiver is a Spring application and I've checked that the library installed in libs folder is 1.6.3.
What may I be doing wrong? Maybe this feature cannot be used with maps.
I have another question, So far I'm just sending a map where the key is just a uuid value and the value is a number. May I have any problems if I send a value with special characters with ALLOW_UNQUOTED_FIELD_NAMES feature on? Will jackson escape this characters?
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…