question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve to deserialize when not exists default constructor

See original GitHub issue

Hi. 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:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Dec 13, 2019

Ok good! Actually I also realized that there might be better way than using AnnotationIntrospector: when BeanDeserializerModifier is registered, its updateBuilder() gets built, passing BeanDeserializerBuilder. It has ValueInstantiator initialized, and modifier could check if it can actually create an instance or not (method canInstantiate() 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 standard BeanDeserializer.

I think this is doable, and you are probably right that others would also find it useful!

1reaction
cowtowncodercommented, Dec 13, 2019

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 defines findValueInstantiator(), and construct ValueInstantiator that implements createUsingDefault(ctxt) (and canCreateUsingDefault()) 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found