Jackson deserialization fails after serializing an object using writeValueAsString

convert nested json to java object using jackson
failed to evaluate jackson deserialization for type
cannot deserialize from object value (no delegate- or property-based creator)
no string-argument constructor/factory method to deserialize from string value
cannot deserialize instance of `java.lang.string` out of start_array token
no creators, like default construct, exist lombok
no string-argument constructor/factory method to deserialize from string value spring boot
deserialize json java

Using the com.fasterxml.jackson.databind.ObjectMapper class(com.fasterxml.jackson.core:jackson-databind:2.9.5) I am trying to deserialize an object of the following class:

class MyClass {

    String name;

    MyClass(String name) {
        this.name = name;
    }
}

The code I am currently executing is the following:

    MyClass myClass = new MyClass("test");
    objectMapper.registerModule(new ParameterNamesModule())
        .registerModule(new Jdk8Module())
        .registerModule(new JavaTimeModule())
        .configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
        .setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

    String text = objectMapper.writeValueAsString(myClass);
    objectMapper.readValue(text, MyClass.class);

which fails on the last line throwing the exception:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.pckge.MyClass (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{"name":"test"}"; line: 1, column: 2]

My goal would be to configure the object mapper so to successfully deserialize the object without using annotations such as JsonCreator or JsonProperties on the constructor of MyClass:

  • Is this doable?
  • Which configuration I am missing?

Thanks a lot!

EDIT 1: Reply by @Dean solves the issue. However I would like to avoid to have the default constructor also.

Jackson uses Getter and Setters to set values. So you need to modify your class:

class MyClass {

    String name;

    MyClass(String name) {
      this.name = name;
   }

   MyClass(){}

   public void setName(String name) {
     this.name = name;
   }

  public String getName(String name) {
     return this.name;
  }
}

The most common Jackson exceptions - the cause of the problem and the Getting Started with Custom Deserialization in Jackson When we try to deserialize a JSON String to User an Exception can not instantiate from JSON object (need to add /enable type information?) writeValueAsString(user);. Home » Java » Jackson deserialization fails after serializing an object using writeValueAsString Jackson deserialization fails after serializing an object using writeValueAsString Posted by: admin October 22, 2018 Leave a comment

The proper way to solve this without a default constructor is to add JsonCreator and JsonProperty annotations to your class constructor.

class MyClass {

    String name;

    @JsonCreator
    MyClass(@JsonProperty("name") String name) {
        this.name = name;
    }
    ...
}

A complete set of examples for serialization and deserialization with writeValueAsString(personName); log.info("Serializing a plain String: that the consumer is expecting an object so it fails at deserialization. As a result, the property hobbies is ignored since there is no field with such name in the class  This is because we don't have an empty constructor that Jackson can use during its deserialization logic (using reflection). There are two common ways of making a plain Java object candidate for JSON deserialization: including an empty constructor or using Jackson annotations.

if you are using loombok remove the @Builder annotation of the class, was my problem

Learn how to use Jackson to serialize java objects. Contents Deserialize Array of Objects from JSON; Summary Use the writeValueAsString() method. Parsing the following JSON fails: This is called the Tree Model since it comprises of a tree of JsonNodes, including ObjectNode and ArrayNode. Reading and converting JSON to a Java object is just as easy with Jackson. Accomplished in just a couple of lines: ObjectMapper mapper = new ObjectMapper(); User user = mapper.readValue(new File(jsonFile), User.class); The target class (User in this case) needs a no-arguments default constructor defined – failing which you get this exception:

Deserializing JSON into object with overloaded methods using Jackson catch (​JsonProcessingException e) { System.out.println("Failed to serialize result");  The most complex case is serializing a Map<Object, Object>, but most of the work is already done. Let's use Jackson's MapSerializer for our map, and MyPairSerializer from the previous section for the map's key and value types:

Configuration object that defines basic global settings for the serialization process Method called to locate deserializer for the passed root-level value. Since 2.5: replaced by a fluent form of the method; addMixIn(Class, Class) . trying to construct serializer: this may be useful in figuring out what the actual problem is. In this tutorial, we're going to look at how to serialize Java objects to XML data using Jackson 2.x and deserialize it back to a POJO. We'll focus on the basic operation that doesn't require a lot of complexity or customization. XmlMapper is the main class from Jackson 2.x that helps us in serialization, so we shall need to create an instance

Java Object to JSON. 49. 2.2. The @JsonRawValue annotation can instruct Jackson to serialize a property exactly as is. In the writeValueAsString(​TypeEnumWithValue.TYPE1); Since Jackson 2.4, a new optional argument namespace is available to use Now, let's see how the infinite recursion problem is handled:. Jackson JSON Generic Serializing and Deserializing Methods August 8, 2015 August 22, 2015 Morgan Kenyon I was using the Jackson JSON library this week and wanting to be able to write a serialize and deserialize method that would work on a generic object.

Comments
  • This solves the issue. What if we do not want to create the default constructor?
  • Unfortunately it will require the default constructor.