Bharath
Bharath

Reputation: 19

Jackson JsonMappingException .No String-argument constructor/factory method to deserialize from String value

I am trying to deserialize following JSON:

 {
    "name": "myName",
    "decoder": "myDecoder",
    "id": 123,
    "definition": {
        "AND": [
            "and-condition-1",
            "and-condition-2",
            {
                "OR": [
                    "or-condition-1",
                    "or-condition-2"
                ]
            }
        ]
    }
}

I have deserialized the file:

  ObjectMapper mapper = new ObjectMapper();
  RuleDefinition ruleDefinition = mapper.readValue(new File(fileName), RuleDefinition.class);

in to following Object-Structure:

RuleDefinition.java

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class RuleDefinition {

    @JsonProperty("name")
    private String name;

    @JsonProperty("decoder")
    private String decoder;

    @JsonProperty("id")
    private int id;

    @JsonProperty("definition")
    private Definition definition;

    @JsonProperty("name")
    public String getName() {
        return name;
    }

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

    @JsonProperty("decoder")
    public String getDecoder() {
        return decoder;
    }

    @JsonProperty("decoder")
    public void setDecoder(String decoder) {
        this.decoder = decoder;
    }

    @JsonProperty("id")
    public int getId() {
        return id;
    }

    @JsonProperty("id")
    public void setId(int id) {
        this.id = id;
    }

    @JsonProperty("definition")
    public Definition getDefinition() {
        return definition;
    }

    @JsonProperty("definition")
    public void setDefinition(Definition definition) {
        this.definition = definition;
    }

    @Override
    public String toString() {
        return "RuleDefinition{" +
                "name='" + name + '\'' +
                ", decoder='" + decoder + '\'' +
                ", id=" + id +
                ", definition=" + definition +
                '}';
    }
}

Definition.Java

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Definition {

    @JsonProperty("AND")
    private List<AND> AND;

    public List<AND> getAND() {
        return AND;
    }

    public void setAND(List<AND> AND) {
        this.AND = AND;
    }

    @Override
    public String toString() {
        return "Definition{" +
                "AND=" + AND +
                '}';
    }
}

AND.java

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class AND {

    @JsonProperty("OR")
    private List<String> Or;

    @JsonProperty("OR")
    public List<String> getOR() {
        return Or;
    }

    @JsonProperty("OR")
    public void setOR(List<String> Or) {
        this.Or = Or;
    }


    @Override
    public String toString() {
        return "AND{" +
                "Or=" + Or +
                '}';
    }
}

and i got the following error

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.model.AND: no String-argument constructor/factory method to deserialize from String value ('and-condition-1')

I see that the issue is because of 'and-condition-1' which is just a value, how do I deserialize these, I wanted to know should I write a custom deserializer (or) Is there any work around?

Upvotes: 0

Views: 7217

Answers (1)

Jose Martinez
Jose Martinez

Reputation: 11992

I think the problem is that in your JSON the AND object is both a string and an object. You should create a JsonDeserializer for it.

enter link description here enter link description here

Upvotes: 1

Related Questions