Jackson JSON deserializing wrapper objects

249 views Asked by At

I am reading a REST-API, which returns a result in the following fashion

{
  "total": 2,
  "results": [
    {
      "name": "object-name",
      "id": 123
    },
    {
      "name": "another-object-name",
      "id": 321
    }
  ]
}

In addition to the properties name and id, elements of results also include other properties, depending on object type. All objects in results are of the same type. There is no property which indicates what object type the element is.

I am working with Apache Beam, and am using AutoValue together with JacksonJson to parse these objects. For example, the object Circle look like this:

@JsonDeserialize(builder = AutoValue_Circle.Builder.class)
@AutoValue
public abstract class Circle implements Serializable {

  public static Circle.Builder builder() {
    return Circle.Builder.builder();
  }

  @JsonProperty("name")
  public abstract String name();

  @JsonProperty("id")
  public abstract int id();

  @JsonProperty("radius")
  public abstract double radius();

  @JsonPOJOBuilder(buildMethodName = "build")
  @AutoValue.Builder
  public abstract static class Builder {

    @JsonCreator
    public static Builder builder() {
      return new AutoValue_Circle.Builder();
    }

    @JsonProperty("name")
    abstract Builder name(String name);

    @JsonProperty("id")
    abstract Builder id(int id);

    @JsonProperty("radius")
    abstract Builder radius(double radius);

    abstract Circle build();
  }
}

I know what type is contained in results. For example making a GET request from /api/circles will return a list of circles.

I am using Apache beam pipelines to handle this:

// jsonData is a PCollection<String>, which holds JSONs
PCollection<ApiWrapperObj> data = jsonData.apply(ParseJsons.of(ApiWrapperObj.class).setCoder(SerializableCoder.of(ApiWrapperObj.class));

So I need to make a deserializer for the wrapper object. Is there a way for me to use a Generic Class for example?

0

There are 0 answers