adding helper for serialization format of object mapper to be array#358
adding helper for serialization format of object mapper to be array#358xerial merged 6 commits intomsgpack:developfrom
Conversation
|
Just curiosity. How do you ensure the order of elements in an array when serializing objects? |
|
good question, checking now Jackson's code to see what guarantees it offers (if any). |
|
as far as I see, ObjectMapper uses java reflection api to receive pojo's fields (POJOPropertiesCollector calling AnnotatedClass#resolveFields, which eventually uses Class#getDeclaredFields). |
|
AFAK there is no guarantee of the field order returned by reflection. msgpack v6 has If the fields are inherited from parent/ancestor classes, I guess the field order will be more complicated. |
|
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); |
There was a problem hiding this comment.
Do we need to add a test for '55' (age) as well?
There was a problem hiding this comment.
you're right, added
|
@marenzo FYI. You can run code style check with |
|
@xerial yep, sorry for the multiple commits. fixed |
|
@marenzo One more thing. I think we should add a test to confirm that the generated data is actually has an array type. |
|
@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)? |
|
@marenzo It's up to you. Simply we need to make sure that we can produce array type data. |
|
@xerial done. also made some small code style fixes in the test if you don't mind :) |
|
Nice work! I'll merge this and release a new version. |
|
Cool, thanks! |
|
Released 0.8.5, which also includes the configuration of STR8 format. This will be available soon from the Maven central. |
|
Do I need to use Jackson to be able to pack a custom class in the array format? Can I just annotate with |
|
@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). |
|
@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? |
|
@starthal yes, you should use msgpack-jackson package, and check the following doc: |
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.