-
-
Notifications
You must be signed in to change notification settings - Fork 221
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
QName Deserializer #543
Open
vitorpamplona
wants to merge
2
commits into
FasterXML:2.14
Choose a base branch
from
Path-Check:2.14
base: 2.14
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
QName Deserializer #543
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/fasterxml/jackson/dataformat/xml/deser/QNameDeserializer.java
This file contains 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,33 @@ | ||
package com.fasterxml.jackson.dataformat.xml.deser; | ||
|
||
import javax.xml.namespace.QName; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
|
||
|
||
public class QNameDeserializer extends JsonDeserializer { | ||
JsonDeserializer<?> originalDeserializer; | ||
public QNameDeserializer(JsonDeserializer<?> deserializer) { | ||
originalDeserializer = deserializer; | ||
} | ||
|
||
@Override | ||
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException { | ||
QName qName = (QName) originalDeserializer.deserialize(p, ctxt); | ||
|
||
if (qName.getLocalPart().indexOf(":") > 0) { | ||
String prefix = qName.getLocalPart().split(":")[0]; | ||
String localPart = qName.getLocalPart().split(":")[1]; | ||
String namespace = ((FromXmlParser)ctxt.getParser()).getStaxReader().getNamespaceContext().getNamespaceURI(prefix); | ||
|
||
return new QName(namespace, localPart, prefix); | ||
} | ||
|
||
return qName; | ||
} | ||
} |
This file contains 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
35 changes: 35 additions & 0 deletions
35
src/test/java/com/fasterxml/jackson/dataformat/xml/deser/QNameTest.java
This file contains 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,35 @@ | ||
package com.fasterxml.jackson.dataformat.xml.deser; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
|
||
import javax.xml.namespace.QName; | ||
|
||
import com.fasterxml.jackson.dataformat.xml.XmlMapper; | ||
import org.junit.Test; | ||
|
||
public class QNameTest { | ||
protected static class Parent { | ||
public Level1 level1; | ||
} | ||
|
||
protected static class Level1 { | ||
public QName name; | ||
} | ||
|
||
private final XmlMapper MAPPER = XmlMapper.builder() | ||
.defaultUseWrapper(false) | ||
.build(); | ||
|
||
@Test | ||
public void testQNameParser() throws Exception | ||
{ | ||
String xml = | ||
"<parent xmlns:t=\"urn:example:types:r1\">\n" + | ||
" <level1 name=\"t:DateTime\" />\n" + | ||
"</parent>"; | ||
|
||
Parent bean = MAPPER.readValue(xml, Parent.class); | ||
|
||
assertEquals("{urn:example:types:r1}DateTime", bean.level1.name.toString()); | ||
} | ||
} |
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 won't work if content is buffered (with
TokenBuffer
), so I am not sure this approach is valid unfortunately.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.
Humm.. interesting.. do you have a simple test case for the Buffered use case?
I am using the same design in our solution so, even if it is not a viable solution here, I would love to improve the code in our solution. Or at least know em when exactly it will fail.
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.
It's a side-effect of processing, not a feature to enable: if you create a POJO with
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
, property values will likely be buffered. Similarly for polymorphic types if Type Id is not the first value deserialized.Basically any case where stream-order is not the same as the order in which values are needed will be buffered.
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.
So, the correct way to solve this would be to change the token streams to include namespaces and prefixes available at each one of them?
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.
Yes.
TokenBuffer
, however, is not XML specific and cannot (should not) be changed.2.13 allows replacement of buffer implementation so we could have
XmlTokenBuffer
sub-class (or such) -- XML backend was the reason to add this -- but there's then the question of how to access information. ProbablyFromXmlParser
andXmlTokenBuffer
could implement an interface (to be added) for additional accessors.I have not, unfortunately, had time to take this approach any further but this would be a very valuable improvement and unblock work on solving multiple issues.
So I would be happy to help you or anybody who had time to look into improvements in this area.