2

I have an external key-value storage, which contains a set of binary values per key. These binary values are Scala objects serialized using Jackson 2.5 with a ScalaPlugin.

To preserve set semantics in the storage, binary representation of an object should be stable, i.e. serializing it two times should result in the same sequence of bytes. Does Jackson have this guarantee? If it uses JSON serialization as an intermediate step for example, there is no node ordering, so binary serialization will not be stable.

This may get even more tricky if an object contains unordered structures internally (like sets or maps). Does Jackson handle that?

If not, are there sound alternatives?


Update:

I have found com.fasterxml.jackson.databind.SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS which resolves a part of the question.

Tim
  • 2,008
  • 16
  • 22
  • 1
    The emails [here](https://groups.google.com/forum/#!topic/jackson-user/VZhiGEoyPBM) suggest the order is mostly arbitrary, but the SORT_PROPERTIES_ALPHABETICALLY-feature mentioned in the answers for [this question](http://stackoverflow.com/q/27577701/3080094) might help. – vanOekel Aug 28 '15 at 21:16
  • It works! If you care to make it an answer I'll accept it. – Tim Aug 28 '15 at 21:43

1 Answers1

3

You can use the option SORT_PROPERTIES_ALPHABETICALLY as mentioned in the answers of this question to achieve stable serialization (together with the option ORDER_MAP_ENTRIES_BY_KEYS that you already mentioned).

Some more background is explained in these emails:
"... in absence of explicit declaration (via @JsonPropertyOrder), and general mechanisms (sort-alphabetically; Object Id and Type Id preceding other properties), there is no defined ordering."
and
"One thing to note is that Oracle did change behavior of JDK 7, such that previously stable ordering of methods and fields returned by Introspection become arbitrary." ... "Existing default ordering is based on simple traversal and ordering that JDK provides; ...".
(copied from the email from Tatu Saloranta).

Community
  • 1
  • 1
vanOekel
  • 6,358
  • 1
  • 21
  • 56