-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
Unable to deserialize root-level Instant
value from XML
#380
Comments
Hmmh. I'll have to figure out how to test this; issue belongs somewhere else, probably XML module (I can transfer it). But one thing to note is this: since But I am interested in seeing what exactly happens here. |
For the XmlMapper, the value is serialised as XML i.e. You would expect that whatever serialised value is returned by the mapper, it would be able to deserialise it back to the object value. I.e the following should hold:
|
Yes, as a general rule, what Jackson writes, it should be able to read back. |
The default ObjectMapper which is based on JSON is able to serialise/deserialise values such as Instant without any attribute names I.E the value is the whole JSON and its not an object with fields. You would expect different implementations such as the XmlMapper to exhibit same behaviour. This is the Liskob substitution principle from SOLID. |
Right, I think we are all familiar with SOLID. Btw, typo - it's the "Liskov" substitution principle, not "Liskob". Anyway, if you can provide a full test case, that would be helpful. |
I have provided the code to reproduce in the initial description along with the API version, Java version and stack trace |
@afayes Question is not that of whether it would be good for mappers to be able to work similarly, but that of if it is possible to. XML and JSON format are structurally different. |
After looking a bit into this, I have come to conclusion that it is not and will not be feasible to support the specific use case of scalar (text, number) as root value. So although serialization of such value is easy -- for simple
deserialization is difficult as elements are represent as JSON Object equivalent token streams, roughly identical to
which looks like an Object value: this is different from how POJO properties look like and would need specialized heuristics to support. Keeping also in mind that according to original JSON specification, root value MUST be either Object or Array, and as such some json libraries do not support handling of root-level String or Number values. So. To handle |
Instant
value from XML
@cowtowncoder Thanks for your time on this. I get your point about root level scalars. In the past whilst designing REST API's I looked into the validity of returning scalars as response for JSON. As you noted, it's not supported by some libraries although more recent standards do allow it and the Jackson library conforms to that for JSON. I have not used XML for REST API's until the current client project I am working on as part of a team of devs. It's an existing code base and a recently developed controller returned an Instant as root and Spring REST client was throwing an exception when deserialising the response back to Java. After some further investigation I found that the XmlMapper is able to serialise but not deserialise. I tested with default ObjectMapper that uses JSON and it worked fine. I understand that the current implementation makes it difficult to support the deserialisation part for root Instant values for XML. Given that, root scalars are not really valid XML, you may wish to never support it. One possible suggestion is to throw an exception when trying to serialise root scalars with the XmlMapper. That way there is consistency between serialisation and deserialisation and it won't cause confusion for users of the API. Who knows, you may decided to one day support both serialisation and deserialation :) |
@afayes No problem. It is unfortunately this is currently difficult to support -- it is obviously not something impossible to support, just... rather difficult with the way things are. I'll think about exception part: it is challenging in its own way since core Jackson databind actually does not really know possible "shapes" that are acceptable, currently, so there is no reliable way to know if type X might be possible to support or not. With 3.0 there may be more support for deserializers to indicate shapes (Object, Array, various scalars) they support so perhaps this could be checked to some degree. Anyway, thank you for your suggestions! |
I changed my mind. Since I have been able to, I think, to support this for basic JDK scalar values, I think same might be possible for date/time types as well. |
Awesome! |
Unable to deserialise XML to Instant value
Jackson version: 2.10.1
Java version: Java 8
Code to reproduce:
Stack trace:
The text was updated successfully, but these errors were encountered: