35

I would like serialize an object such that one of the fields will be named differently based on the type of the field. For example:

public class Response {
    private Status status;
    private String error;
    private Object data;
        [ getters, setters ]
    }

Here, I would like the field data to be serialized to something like data.getClass.getName() instead of always having a field called data which contains a different type depending on the situation.

How might I achieve such a trick using Jackson?

Tim
  • 4,560
  • 2
  • 40
  • 64

5 Answers5

44

I had a simpler solution using @JsonAnyGetter annotation, and it worked like a charm.

import java.util.Collections;
import java.util.Map;

public class Response {
    private Status status;
    private String error;

    @JsonIgnore
    private Object data;

    [getters, setters]

    @JsonAnyGetter
    public Map<String, Object> any() {
        //add the custom name here
        //use full HashMap if you need more than one property
        return Collections.singletonMap(data.getClass().getName(), data);
    }
}

No wrapper needed, no custom serializer needed.

tlogbon
  • 1,212
  • 13
  • 12
  • works like a charm; esp when you have no access to serializer – Anatoly Yakimchuk Apr 18 '18 at 16:06
  • Can anyone explain to me how this is working? Primarily how does the any() method know we want to rename the data attribute. – smithzo622 Feb 08 '19 at 21:18
  • The name of the method is irrelevant, what matters is that the method returns a Map and take in no arguments, and only one method should be annotated with such. See documentation https://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonAnyGetter.html The map can contain any number of properties. In the case of my example, I only needed one property, hence, the use of Singleton map – tlogbon Feb 08 '19 at 22:18
  • How about deserializing the json back? For eg., In my scenario, i have an attribute of Map. I gave dynamic name to this attribute, and the serialization does exactly as intended. Now i want to deserialize the json to object back, but even i created JsonAnySetter, it did not work. Any recommendations? – naresh goty Oct 29 '19 at 23:22
  • @nareshgoty You use `@JsonAnySetter` there is a good example here: https://www.tutorialspoint.com/jackson_annotations/jackson_annotations_jsonanysetter.htm – tlogbon Oct 31 '19 at 00:04
  • The value can be of class `CustomObjectType` @JsonAnySetter public void setValue(String name, CustomObjectType) {/*set map values*/} – tlogbon Oct 31 '19 at 00:10
  • you'd better link to https://www.tutorialspoint.com/jackson_annotations/jackson_annotations_jsonanygetter.htm which talks about the @JsonAnyGetter annotation you mentionne in your exampe above. – maxxyme Jan 24 '20 at 07:10
38

Using a custom JsonSerializer.

public class Response {
  private String status;
  private String error;

  @JsonProperty("p")
  @JsonSerialize(using = CustomSerializer.class)
  private Object data;

  // ...
}

public class CustomSerializer extends JsonSerializer<Object> {
  public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
    jgen.writeStartObject();
    jgen.writeObjectField(value.getClass().getName(), value);
    jgen.writeEndObject();
  }
}

And then, suppose you want to serialize the following two objects:

public static void main(String... args) throws Exception {
  ObjectMapper mapper = new ObjectMapper();
  Response r1 = new Response("Error", "Some error", 20);
  System.out.println(mapper.writeValueAsString(r1));
  Response r2 = new Response("Error", "Some error", "some string");
  System.out.println(mapper.writeValueAsString(r2));
}

The first one will print:

{"status":"Error","error":"Some error","p":{"java.lang.Integer":20}}

And the second one:

{"status":"Error","error":"Some error","p":{"java.lang.String":"some string"}}

I have used the name p for the wrapper object since it will merely serve as a placeholder. If you want to remove it, you'd have to write a custom serializer for the entire class, i.e., a JsonSerializer<Response>.

João Silva
  • 89,303
  • 29
  • 152
  • 158
  • Hello, I have the same problem as the OP. I tried your solution, however what I get is: {"status":"Error","error":"Some error","p": 20}. So the @JsonProperty annotation is working but not the CustomSerializer. I'm using Wildfly 10 and have addded RESTEasy jackson provider as a provided dependency. I also tried what was suggested in this post: https://stackoverflow.com/questions/28307646/how-to-configure-jackson-in-wildfly, but still not working – user3362334 Dec 07 '17 at 01:10
6

my own solution.

@Data
@EqualsAndHashCode
@ToString
@JsonSerialize(using = ElementsListBean.CustomSerializer.class)
public class ElementsListBean<T> {

    public ElementsListBean()
    {
    }

    public ElementsListBean(final String fieldName, final List<T> elements)
    {
        this.fieldName = fieldName;
        this.elements = elements;
    }

    private String fieldName;

    private List<T> elements;

    public int length()
    {
        return (this.elements != null) ? this.elements.size() : 0;
    }

    private static class CustomSerializer extends JsonSerializer<Object> {
        public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
                JsonProcessingException
        {
            if (value instanceof ElementsListBean) {
                final ElementsListBean<?> o = (ElementsListBean<?>) value;
                jgen.writeStartObject();
                jgen.writeArrayFieldStart(o.getFieldName());
                for (Object e : o.getElements()) {
                    jgen.writeObject(e);
                }
                jgen.writeEndArray();
                jgen.writeNumberField("length", o.length());
                jgen.writeEndObject();
            }
        }
    }
}
sylvek
  • 89
  • 2
4

You can use the annotation JsonTypeInfo, which tell Jackson exactly that and you don't need to write a custom serializer. There's various way to include this information, but for your specific question you'd use As.WRAPPER_OBJECT and Id.CLASS. For example:

public static class Response {
    private Status status;
    private String error;
    @JsonTypeInfo(include = As.WRAPPER_OBJECT, use = Id.CLASS)
    private Object data;
}

This, however, will not work on primitive type, such as a String or Integer. You don't need that information for primitives anyways, since they are natively represented in JSON and Jackson knows how to handle them. The added bonus with using the annotation is that you get deserialization for free, if you ever need it. Here's an example:

public static void main(String[] args) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    Response r1 = new Response("Status", "An error", "some data");
    Response r2 = new Response("Status", "An error", 10);
    Response r3 = new Response("Status", "An error", new MyClass("data"));
    System.out.println(mapper.writeValueAsString(r1));
    System.out.println(mapper.writeValueAsString(r2));
    System.out.println(mapper.writeValueAsString(r3));
}

@JsonAutoDetect(fieldVisibility=Visibility.ANY)
public static class MyClass{
    private String data;
    public MyClass(String data) {
        this.data = data;
    }
}

and the result:

{"status":"Status","error":"An error","data":"some data"}
{"status":"Status","error":"An error","data":10}
{"status":"Status","error":"An error","data":{"some.package.MyClass":{"data":"data"}}}
Pascal Gélinas
  • 2,744
  • 19
  • 20
0

Based on @tlogbon response, Here is my solution to wrap a List of Items with a specific/dynamic filed name

public class ListResource<T> {

    @JsonIgnore
    private List<T> items;

    @JsonIgnore
    private String fieldName;

    public ListResource(String fieldName, List<T> items) {
        this.items = items;
        this.fieldName = fieldName;
    }

    @JsonAnyGetter
    public Map<String, List<T>> getMap() {
        return Collections.singletonMap(fieldName, items);
    }
razvang
  • 1,055
  • 11
  • 15