-
Notifications
You must be signed in to change notification settings - Fork 65
Fix for #123 - Add support for preservation of unknown properties #124
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
genson/src/main/java/com/owlike/genson/reflect/UnknownPropertyHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.owlike.genson.reflect; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * An interface that defines callbacks that will be called when an | ||
| * unknown properties are encountered during deserialization, as well | ||
| * as to check if there are any unknown properties that should be | ||
| * written out during serialization. | ||
| * <p> | ||
| * The main purpose of this interface is to support schema evolution | ||
| * of objects that use JSON as a long term storage format, without | ||
| * loss of unknown properties across clients and severs using different | ||
| * versions of Java classes. | ||
| * | ||
| * @author Aleksandar Seovic 2018.05.09 | ||
| */ | ||
| public interface UnknownPropertyHandler { | ||
| /** | ||
| * Called whenever a property is encountered in a JSON document | ||
| * that doesn't have a corresponding {@link PropertyMutator}. | ||
| * <p> | ||
| * Typically, the implementation of this interface concerned | ||
| * with schema evolution will handle this event by storing | ||
| * property value somewhere so it can be returned by the | ||
| * {@link #getUnknownProperties} method. | ||
| * | ||
| * @param target the object we are deserializing JSON into | ||
| * @param propName the name of the unknown property | ||
| * @param propValue the value of the unknown property | ||
| */ | ||
| void onUnknownProperty(Object target, String propName, Object propValue); | ||
|
|
||
| /** | ||
| * Return a map of unknown properties encountered during | ||
| * deserialization, keyed by property name. | ||
| * | ||
| * @param source the object we are serializing into JSON | ||
| * | ||
| * @return a map of unknown properties | ||
| */ | ||
| Map<String, Object> getUnknownProperties(Object source); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This means that if a property handler is registered it is responsible of being able to handle missing properties for all targeted types.
Another option could be to actually implement
failOnMissingPropertyas a special UnknownPropertyHandler, and chain them. That would however complicate a bit UnknownPropertyHandler as it would need to "signal" if it did handle the missing property or not. Just food for thought.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see handler and
failOnMissingPropertyas mutually exclusive options, or rather, the latter is just a special case of the former, which we want to leave as-is for backwards compatibility, but isn't strictly necessary -- anyone can write a handler that fails immediately, if that's what they want to.More likely than not, people will use either one UPH (probably the default one we provide) or none, and if they do use one they will not want to fail on missing properties, so I really don't see a point of complicating the design. In the unlikely event that they do need chained/composite UPH, they can easily write one themselves that does exactly what they need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it's a special case of unknown property handling, is the main reason why I was saying it could be reimplemented that way. The advantage I see to it is, unified logic and removal of unnecessary code.
Preserving some of the compatibility could be achieved by translating the boolean
failOnMissingPropertyinto a handler at the builder level (and remove it from the other places like Genson and BeanDescriptor, which are less likely to be directly used).I agree that the chaining part is not necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see... Yes, that would probably make sense.
We just need to document that behavior, but considering that they are already mutually exclusive, we should probably document it anyway.
Let me rework the code along those lines and I'll update the PR.