-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix #4771: Add feature for QNAME serialization and deserialization. #4909
base: 2.19
Are you sure you want to change the base?
Conversation
This commit fixes FasterXML#4771 by adding serialization and deserialization features to control whether a QName is serialized to a string using the "QName.toString()" method (the only option currently) or if it is serialized to JSON object (a new option to fix FasterXML#4771).
Hi, This is my first contribution to the jackson project. Please let me know if there is anything I can improve or change. Thanks, |
{ | ||
Map<String, String> map; | ||
try { | ||
map = p.readValueAs(STRING_MAP_TYPE_REFERENCE); |
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.
Is this the best way to deserialize the QName from a JSON object?
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.
Ideally not reading via JsonParser
, DeserializationContext
methods are better (for a few reasons).
Might be easiest to read with:
JsonNode tree = ctxt.readTree(p);
and then extract values.
But before that, should probably check this is Object value:
if (!p.hasToken(JsonToken.START_OBJECT)) { // fail
or, read tree
, check
if (!tree.isObject()) { // fail
*<p> | ||
* Feature is disabled by default. | ||
*/ | ||
READ_QNAMES_USING_VALUE_OF(true), |
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.
Hmmh. This is rather too specific to define global feature for... Let me think about this a bit.
But is this actually needed? Deserializer should be able to support both styles based on which value shape is seen?
(this does not remove need for feature for serialization, but would avoid DeserializationFeature
)
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.
That's a fair point. I can make that change if, after you think about it, this is still the right approach.
* Note: this feature should usually have same value | ||
* as {@link DeserializationFeature#READ_QNAMES_USING_VALUE_OF}. | ||
*<p> | ||
* Feature is disabled by default. |
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.
If we are to go with the addition, needs @since 2.19
tag.
public void testQNameDeserWithValueOfFeatureDisabled() throws Exception | ||
{ | ||
String qstr = a2q("{'namespaceURI':'http://abc','localPart':'tag','prefix':'prefix'}"); | ||
QName qn = MAPPER.disable(DeserializationFeature.READ_QNAMES_USING_VALUE_OF).readValue(qstr, QName.class); |
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.
Construct a new mapper, changing of shared MAPPER
should not be done (behavior not defined, concurrent unit test could fail etc)
Ok, first of all: thank you for submitting this pr @mcvayc (and thank you for already sending CLA). All in all makes sense; added some implementations suggestions. But the big question actually has to do with configurability. Some concerns (which you couldn't really have known since they are not that obvious):
So... I am trying to think of an alternative. And actually there is another mechanism, sort of designed for this. It's bit less convenient in some ways for global definitions but conceptually better: You can annotate properties
but also define global defaults:
so that would be something I'd consider as the canonical intended mechanism.
So... this is something I would suggest. Apologies for added complexity. |
@cowtowncoder, thank you for your guidance. I'll make the changes to implement this via I created this PR based on what I saw for Enum serialization jackson-databind/src/main/java/com/fasterxml/jackson/databind/SerializationFeature.java Lines 283 to 293 in 17858e9
but understand now why this doesn't fit. Thanks for clarifying. |
@mcvayc Great! Nothing wrong with the initial take; some existing implementations do things in similar way. Looking forward to v2! :) |
This commit fixes #4771 by adding serialization and deserialization features to control whether a QName is serialized to a string using the
QName.toString()
method (the only option currently) or if it is serialized to JSON object (a new option to fix #4771).