Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection serializers and deserializer should be contextual #155

Merged
merged 3 commits into from
Jun 7, 2020

Conversation

mincong-h
Copy link
Member

Overview

Fix #154

Collection-like serializers and deserializers should be contextual to handle properties of the element defined outside of the collection. For example, in collection List<Date>, handling @JsonFormat defined for Date:

class FrenchDates {
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", timezone = "Europe/Paris")
    List<Date> dates;
}

All the collection-like serializers and deserializers are concerned: serializers and deserializers for Set, Seq, Array, Priority-Queue, List, and their derived classes.

Serialization

VAVR-Jackson does not handle the serialization directly. We emulate the VAVR type to Java type. Then, let the serializer provider to find the right serializer to handle the logic. To support contextualization, we save the bean property in ArraySerializer, and pass this property to serializer provider for the lookup:

-                    ser = provider.findTypedValueSerializer(emulated, true, null);
+                    ser = provider.findTypedValueSerializer(emulated, true, beanProperty);

Since serializer should be immutable (see comment), by "saving the bean property", it actually means creating a new serializer with all the existing info and a new bean property.

Deserialization

VAVR-Jackson handles the deserialization directly. In Array-Deserializer, implement the add-on interface ContextualDeserializer and perform lookup to find the appropriated serializer for the elements of the collection. The logic is mainly inspired from previous PRs (#144, #147) and Jackson Databind.

Additional Notes

Other serializers and deserializers may have the same problem. I need to spend some time to them in future PRs...

@codecov-commenter
Copy link

Codecov Report

Merging #155 into master will increase coverage by 0.03%.
The diff coverage is 93.24%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #155      +/-   ##
============================================
+ Coverage     93.18%   93.21%   +0.03%     
- Complexity      309      323      +14     
============================================
  Files            47       47              
  Lines           719      752      +33     
  Branches        166      168       +2     
============================================
+ Hits            670      701      +31     
- Misses           21       22       +1     
- Partials         28       29       +1     
Impacted Files Coverage Δ Complexity Δ
...vr/jackson/datatype/serialize/ValueSerializer.java 81.81% <83.33%> (+2.87%) 4.00 <1.00> (+1.00)
...vr/jackson/datatype/serialize/VavrSerializers.java 92.77% <83.33%> (ø) 38.00 <4.00> (ø)
...ackson/datatype/deserialize/VavrDeserializers.java 93.02% <88.88%> (+0.25%) 38.00 <4.00> (ø)
...ackson/datatype/deserialize/ArrayDeserializer.java 93.93% <91.66%> (-6.07%) 13.00 <8.00> (+3.00) ⬇️
...atatype/deserialize/PriorityQueueDeserializer.java 100.00% <100.00%> (ø) 5.00 <4.00> (+2.00)
.../jackson/datatype/deserialize/SeqDeserializer.java 100.00% <100.00%> (ø) 9.00 <9.00> (+2.00)
.../jackson/datatype/deserialize/SetDeserializer.java 100.00% <100.00%> (ø) 7.00 <6.00> (+2.00)
...vr/jackson/datatype/serialize/ArraySerializer.java 100.00% <100.00%> (ø) 8.00 <5.00> (+4.00)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3af8494...798c605. Read the comment docs.

Copy link
Member

@ruslansennov ruslansennov left a comment

Choose a reason for hiding this comment

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

thanks!

@mincong-h mincong-h merged commit cea184d into vavr-io:master Jun 7, 2020
@mincong-h mincong-h deleted the issue-154 branch June 7, 2020 13:56
@mincong-h
Copy link
Member Author

Thanks for the review, @ruslansennov 🙇‍♂️

@camory
Copy link

camory commented Jun 8, 2020

Thanks for your work

@mincong-h mincong-h added this to the v1.0.0-alpha-3 milestone Jul 11, 2020
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.

Serialize of Liste of Date does not follow pattern defined in @JsonFormat
4 participants