Skip to content

adding helper for serialization format of object mapper to be array#358

Merged
xerial merged 6 commits intomsgpack:developfrom
marenzo:array_format_support
Apr 11, 2016
Merged

adding helper for serialization format of object mapper to be array#358
xerial merged 6 commits intomsgpack:developfrom
marenzo:array_format_support

Conversation

@marenzo
Copy link
Copy Markdown
Contributor

@marenzo marenzo commented Apr 7, 2016

related to the tip that @komamitsu gave in #313 issue, about adding @JsonFormat(shape=JsonFormat.Shape.ARRAY) annotation to made a pojo being serialized as array. The issue with that is most of the times you have tons of pojo's around, and you can't enforce all of them to have this annotation, so I've made some global helper to enforce this serialization format on all of the objects serialized by object mapper.

this helper also was made because jackson doesn't provide any programmatic way to do such thing.

@xerial
Copy link
Copy Markdown
Member

xerial commented Apr 8, 2016

Just curiosity. How do you ensure the order of elements in an array when serializing objects?

@marenzo
Copy link
Copy Markdown
Contributor Author

marenzo commented Apr 8, 2016

good question, checking now Jackson's code to see what guarantees it offers (if any).
how did msgpack 0.6 worked in that matter? AFAIK, javassist doesn't provided guarantees about fields order

@marenzo
Copy link
Copy Markdown
Contributor Author

marenzo commented Apr 8, 2016

as far as I see, ObjectMapper uses java reflection api to receive pojo's fields (POJOPropertiesCollector calling AnnotatedClass#resolveFields, which eventually uses Class#getDeclaredFields).
the JVM doesn't provide any guarantees about fields order, although it seems that the deceleration order is being saved (can test more this statement).

@xerial
Copy link
Copy Markdown
Member

xerial commented Apr 10, 2016

AFAK there is no guarantee of the field order returned by reflection. msgpack v6 has index annotation to assign field order.

If the fields are inherited from parent/ancestor classes, I guess the field order will be more complicated.

@marenzo
Copy link
Copy Markdown
Contributor Author

marenzo commented Apr 10, 2016

Jackson have annotation called 'JsonPropertyOrder', which gives you the ability to define by yourself the fields order. So If someone would like to define manually the order, it's possible.

Basically I'll use this utility, as I'll need my pojos to be serialized without their scheme, so I thought this will be also common use for people who want to make a migration, or more of that, for people who want to keep serializing without scheme.

String scheme = new String(bytes, Charset.forName("UTF-8"));
assertThat(scheme, not(containsString("name")));
UsingCustomConstructorPojo value = objectMapper.readValue(bytes, UsingCustomConstructorPojo.class);
assertEquals("komamitsu", value.name);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add a test for '55' (age) as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, added

@xerial
Copy link
Copy Markdown
Member

xerial commented Apr 11, 2016

@marenzo FYI. You can run code style check with ./sbt jcheckStyle

@marenzo
Copy link
Copy Markdown
Contributor Author

marenzo commented Apr 11, 2016

@xerial yep, sorry for the multiple commits. fixed

@xerial
Copy link
Copy Markdown
Member

xerial commented Apr 11, 2016

@marenzo One more thing. I think we should add a test to confirm that the generated data is actually has an array type.

@marenzo
Copy link
Copy Markdown
Contributor Author

marenzo commented Apr 11, 2016

@xerial changing the test to use NestedListPojo would be fine, or we want some new pojo that will contains some primitives and data structures (list, map, set)?

@xerial
Copy link
Copy Markdown
Member

xerial commented Apr 11, 2016

@marenzo It's up to you. Simply we need to make sure that we can produce array type data.

@marenzo
Copy link
Copy Markdown
Contributor Author

marenzo commented Apr 11, 2016

@xerial done. also made some small code style fixes in the test if you don't mind :)

@xerial
Copy link
Copy Markdown
Member

xerial commented Apr 11, 2016

Nice work! I'll merge this and release a new version.

@xerial xerial merged commit 9aa99f6 into msgpack:develop Apr 11, 2016
@marenzo
Copy link
Copy Markdown
Contributor Author

marenzo commented Apr 11, 2016

Cool, thanks!

@xerial
Copy link
Copy Markdown
Member

xerial commented Apr 11, 2016

Released 0.8.5, which also includes the configuration of STR8 format. This will be available soon from the Maven central.

@starthal
Copy link
Copy Markdown

Do I need to use Jackson to be able to pack a custom class in the array format? Can I just annotate with @Message and @index and expect the fields to be (de)serialized in the right order?

@marenzo
Copy link
Copy Markdown
Contributor Author

marenzo commented Apr 20, 2016

@starthal those are annotation of the previous version (0.6.x), and this PR is provided same serialization ability of array format also in the new client (>=0.8.5).

@starthal
Copy link
Copy Markdown

starthal commented Apr 20, 2016

@marenzo If I use version 0.8.5 what do I need to do in order to (de)serialize a POJO in the array format? Do I need to be using Jackson?

@marenzo
Copy link
Copy Markdown
Contributor Author

marenzo commented Jul 5, 2016

@starthal yes, you should use msgpack-jackson package, and check the following doc:
https://github.com/msgpack/msgpack-java/tree/develop/msgpack-jackson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants