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

QName Deserializer #543

Open
wants to merge 2 commits into
base: 2.14
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

@cowtowncoder cowtowncoder Aug 30, 2022

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.

Copy link
Author

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.

Copy link
Member

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.

Copy link
Author

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?

Copy link
Member

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. Probably FromXmlParser and XmlTokenBuffer 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.


return new QName(namespace, localPart, prefix);
}

return qName;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.dataformat.xml.deser;

import javax.xml.namespace.QName;
import java.util.*;

import com.fasterxml.jackson.databind.*;
Expand Down Expand Up @@ -81,6 +82,10 @@ public List<BeanPropertyDefinition> updateProperties(DeserializationConfig confi
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
BeanDescription beanDesc, JsonDeserializer<?> deser0)
{
if (beanDesc.getBeanClass() == QName.class) {
return new QNameDeserializer(deser0);
}

if (!(deser0 instanceof BeanDeserializerBase)) {
return deser0;
}
Expand Down
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());
}
}