0

Here's the current style I've got for simple messages, using Jackson:

public class FooRequest {
    public final String foo;
    public final int bar;

    @JsonCreator
    public FooRequest(
            @JsonProperty("foo") String foo,
            @JsonProperty("bar") int bar) {
        this.foo = foo;
        this.bar = bar;
    }
}

Is there a class annotation out there that will handle the boiler plate constructor?

  • I think this blog post is relevant, Dag, [Jackson without annotations](https://manosnikolaidis.wordpress.com/2015/08/25/jackson-without-annotations/). – DavidS Apr 21 '16 at 17:24
  • I think you should clarify _in your question_ (not comments) exactly what you want. Do you want to be able to serialize objects that don't have constructors, or do you not want to have to annotate your classes, or something else? – DavidS Apr 21 '16 at 17:25

3 Answers3

0

A quick Google search brought up this:

Creating a json object using jackson

Looks like you can handle the construction with JsonNodeFactory.

final JsonNodeFactory factory = JsonNodeFactory.instance;

Is that what you were looking for?

Community
  • 1
  • 1
0

You can try using Jackson annotation. The Jackson annotation @JsonCreator is used to tell Jackson that the Java object has a constructor (a "creator") which can match the fields of a JSON object to the fields of the Java object.

Let's consider the following JSON object that we want to deserialize.

{
    "name":"Mr.Bond",
    "age":"30"
}

You can unmarshall the message by annotating the constructor with @JsonCreator and using the @JsonProperty

public class UserProfileCreator {
    public int age;
    public String name;

    @JsonCreator
    public UserProfileCreator(
        @JsonProperty("age") int age, 
        @JsonProperty("name") String name) {
            this.age = age;
            this.name = name;
    }
}

How will it work?

Let's write a small test program.

@Test
public void deserialize()
    throws JsonProcessingException, IOException {

    String json = "{"age":30,"name":"Mr.Bond"}";
    UserProfileCreator obj = new ObjectMapper().reader(UserProfileCreator.class).readValue(json);
    assertEquals("Mr.Bond", obj.name);
}
The Roy
  • 2,178
  • 1
  • 17
  • 33
0

Google's gson library doesn't require you to use annotations or constructors:

import com.google.gson.Gson;
import java.util.Date;
public class Test {
    public static void main(String[] args) {
        Gson gson = new Gson();

        String toJson = gson.toJson(new Thing());
        System.out.println(toJson);
        // prints {"appleType":"granny","date":"Apr 21, 2016 10:31:17 AM","numPies":6}

        Thing fromJson = gson.fromJson(toJson, Thing.class);
        System.out.println(fromJson);
        // prints com.mycompany.mavenproject1.Thing@506c589e
    }
}
class Thing {
    String appleType = "granny";
    Date date = new Date();
    int numPies = 6;
}

Jackson may have similar features, but I don't know.

DavidS
  • 5,022
  • 2
  • 28
  • 55