Can Jackson reuse Key-Strings?

363 views
Skip to first unread message

Fabian Lange

unread,
Jan 17, 2017, 3:42:10 PM1/17/17
to jackson-user
Hi,
I was wondering if there is some kind of "String" pooling possible with Jackson, if it might be a good idea to add support for it, or if thats more of a case for a module?

The use case is to parse large amounts of similar json documents into memory. As the keys are always the same, they end up taking a lot of memory when de-serializing to Map<String, Object>, but I assume same holds true for JsonNodes.
I searched a bit and could not find if it was ever discussed to add some sort of caching level for keys (possibly also for values, but keys are much more obvious), which during de-serialization realizes that the (immutable) value is identical to one which has been read already.

I am not voting for String.intern() as it has its own problems, maybe some pluggable Key Cache?

Rationale is that this way one could hold on many more deserialized documents in memory.

Fabian

brharr...@gmail.com

unread,
Jan 17, 2017, 4:17:32 PM1/17/17
to jackson-user
Yes, see INTERN_FIELD_NAMES

Brian

Tatu Saloranta

unread,
Jan 17, 2017, 4:30:22 PM1/17/17
to jackson-user
I am also bit puzzled by the original question, so perhaps it is good to indicate that by default Jackson core (JsonParser) does two things to ALL json property keys:

1. Canonicalizes them so that there is only one instance of each distinct key (like "property"): no new Strings are allocated. This is controlled by `JsonFactory.Feature.CANONICALIZE_FIELD_NAMES)`
2. Each canonicalized instance is also intern()ed if `JsonFactory.Feature.INTERN_FIELD_NAMES` is enabled.

Both features are enabled by default, but may be disabled. Note, however, that if canonicalization is disabled, no intern()ing will occur (since intern()ing is done by symbol table; and since consistent intern()ing would be counter-productive).

Now: `ObjectNode` and `Map` will simply use property name as reported by `JsonParser`, and it being canonicalized (usually) there won't be duplicate Strings. There is nothing for node or map handling that participates in this.

-+ Tatu +-


--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user+unsubscribe@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fabian Lange

unread,
Jan 17, 2017, 4:43:24 PM1/17/17
to jackson-user
Hi Tatu,

ist this the KeyDeserializer used for maps?
com.fasterxml.jackson.databind.deser.std.StdKeyDeserializer.StringKD

So yeah it seems that I missed the intern feature.
Fabian
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.

Fabian Lange

unread,
Jan 17, 2017, 4:53:25 PM1/17/17
to jackson-user
Hi Again,
not sure what I did there, but of course you are correct. I now was able to verify the keys (also in maps) are indeed the same object reference.

Fabian
Reply all
Reply to author
Forward
0 new messages