#StackBounty: #java #jackson #objectmapper Throw error if strings are not double quoted while using jackson objectmapper deserialization

Bounty: 100

I have a JSON:

{
    "stringField" : 1234,
    "booleanField": true,
    "numberField": 1200.00
}

I use object mapper to deserialize the json into:-

@Data
Class SomeClass {
    String stringField;
    boolean booleanField;
    float numberField;
}

I would like the objectMapper to throw an error because, the values for String fields must be double quoted according to the json spec. How can i get objectMapper to throw an error?


Get this bounty!!!

#StackBounty: #java #json #jackson #jackson-databind Jackson multiple different schema for serialization of nested fields

Bounty: 50

I want to have several different schema when serializing with Jackson.
Suppose that I have the following classes:

public class Department {
      private Person head;
      private Person deputy;
      private List<Person> staff;
      // getters and setters
}

public class Person {
       private String name
       private int code;
      // getters and setters
}

Now, I want to have two different schema for Department class. The first one contains only head and deputy where head includes both name and code, but deputy has only name. The second schema should include all fields recursively.

Thus, we will have two different jsons. With the first schema:

{
    "head" : {
        "name" : "John",
        "code" : 123
     },
     "deputy" : { 
        "name" : "Jack"
     } 
}

, and with the second schema:

{
    "head" : {
        "name" : "John",
        "code" : 123
     },
     "deputy" : { 
        "name" : "Jack",
        "code" : "234"
     },
     "staff": [
        { 
            "name" : "Tom",
            "code" : "345"
         },
         { 
            "name" : "Matt",
            "code" : "456"
         },
     ]
}

QUESTION: How should I do it with Jackson?

NOTE: These classes are just examples. For this simple example, writing four different wrapper classes may be possible but think about a complex example with dozen of classes that each one has several fields. Using wrapper classes, we should generate a lot of boilerplate code.

Any help would be appreciated!


Get this bounty!!!

#StackBounty: #java #json #jackson Parsing an array of non-homogeneous JSON Objects with Jackson

Bounty: 50

I have a situation where I need to parse an array of JSON objects that are not identical.

So for example:

[
{ "type": "type1", ..... type1 contents .... },
{ "type": "type2", ..... type2 contents .... },
....
{ "type": "type1", ..... type1 contents .... }
]

The number of types is limited and the contents of each type are well can be defined but it is not possible to define a single type of object that will hold the contents.

Is there a way to parse them with Jackson?

P.S. I am trying to avoid writing a custom parser if I can help it.


Get this bounty!!!

#StackBounty: #java #json #jackson How to make Jackson to inject the Generic Class into a bean?

Bounty: 50

Lets suppose, that we have a bean like this:

public class Response<T> {
    private T data;
    private double executionDuration;
    private boolean success;
    private String version;

    //HOW TO Make Jackson to inject this?
    private Class<T> dataClass;

    public Optional<T> getData() {
        return Optional.ofNullable(data);
    }

    public double getExecutionDuration() {
        return executionDuration;
    }

    public Class<T> getDataClass() {
        return dataClass;
    }

    public String getVersion() {
        return version;
    }

    public boolean isSuccess() {
        return success;
    }
}

The deserialization happens like this:

objectMapper.readValue(json, new TypeReference<Response<SomeClass>>() {});

Can I somehow make Jackson to inject the class “SomeClass” into my bean? Injecting the type reference itself would be also ok, I think.


Get this bounty!!!