Improve to deserialize when not exists default constructor
See original GitHub issueHi. Current jackson is fail to deserialize when not implement default constructor and none of the arguments match. (If enable FAIL_ON_UNKNOWN_PROPERTIES
).
for example. this code is serialized to { "str": "content" }
. but this value is not deserialize. because constructor argument name is not match str
and someStr
. so jackson can’t create object.
class SomeClass {
private String str;
public SomeClass(String someStr) {
this.str = someStr;
}
public String getStr() {
return str;
}
}
The possible solution to this case is, Inject the value after forcing the object to create. (if enabled visibility field
).
If using objenesis, it is possible force create to object. http://objenesis.org/
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
ObjectMapper can't deserialize without default constructor ...
RELEASE and ObjectMapper bean was able to deserialize such object from JSON. After the upgrade to Spring Boot 2.0.0.M7 I receive following ...
Read more >apex - JSON.deserialize won't call default constructor
deserialize (jsonString, apexType) . I could not find any documentation that specifies that, but I expected the no-arg constructor to be called.
Read more >Jackson Deserialization and the Builder Pattern
Address` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at ...
Read more >Using default constructor when deserializing - Google Groups
It seems that with the latest release the default (no parameters) constructors are no longer called upon deserialization. Is this correct?
Read more >Serialization of Classes - Boost C++ Libraries
In addition to the deserialization of pointers, these overrides are used in the deserialization of STL containers whose element type has no default...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ok good! Actually I also realized that there might be better way than using
AnnotationIntrospector
: whenBeanDeserializerModifier
is registered, itsupdateBuilder()
gets built, passingBeanDeserializerBuilder
. It hasValueInstantiator
initialized, and modifier could check if it can actually create an instance or not (methodcanInstantiate()
checks all possibilities, but there are other methods for more granular). If not, then it could create a new one, override default one. That will be used by otherwise standardBeanDeserializer
.I think this is doable, and you are probably right that others would also find it useful!
I would not add a dependency, for sure, but my concern is more whether it is good idea to even add code that does what Objenesis does. I know roughly how it is likely done, by-passing construction same way as JDK serialization does – and the idea of by-passing Object initialization is dangerous and possibly security risk. It is also likely to break with JDK 9 or later.
In fact, I think that a better way to do this would be to create a module that allows this functionality: that could easily depend on Objenesis (or use equivalent code). It could probably provide an
AnnotationIntrospector
that definesfindValueInstantiator()
, and constructValueInstantiator
that implementscreateUsingDefault(ctxt)
(andcanCreateUsingDefault()
) as expected.Module could then return
null
for cases where other instantiator options exist.The main challenge might be that of recognizing cases where NOT to create
ValueInstantiator
however.