Moiz Raja
Moiz Raja

Reputation: 5762

Hierarchical POJOs not being properly serialized to JSON by RESTEasy/Jackson

I have two POJOs defined as follows,

public class VertexDefinition {
    private final String name;
    private final Vertex vertex;

    public VertexDefinition(String name, Vertex vertex) {
        this.name = name;
        this.vertex = vertex;
    }

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

    @JsonProperty("properties")
    public Iterable<PropertyDefinition> getProperties() {
        if(vertex == null) {
            return Collections.emptySet();
        }
        return Iterables.transform(vertex.getPropertyKeys(), new Function<String, PropertyDefinition>() {
            @Nullable @Override public PropertyDefinition apply(@Nullable String s)  {
                return new PropertyDefinition(vertex, s);
            }
        });
    }

    @JsonProperty("propertyKeys")
    public Iterable<String> getPropertyKeys() {
        if (vertex == null) {
            return Collections.emptySet();
        }
        return vertex.getPropertyKeys();
    }

}

public class PropertyDefinition {

    private final Vertex vertex;
    private final String propertyName;

    public PropertyDefinition(Vertex vertex, String propertyName) {
        this.vertex = vertex;
        this.propertyName = propertyName;
    }

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

    @JsonProperty("type")
    public String getType() {
        final Object property = vertex.getProperty(propertyName);

        if (property != null) {
            return property.getClass().getTypeName();
        }

        return "(unknown)";
    }
}

My Rest method looks as follows,

public Iterable<VertexDefinition> getSchema() {
    .....
}

When I make a request I get a json response as follows,

 [
   {
       "name" : "Foo",
       "properties" : [],
       "propertyKeys" : [
          "a",
          "b",
          "c"
       ]
   },
   {
       "name" : "Bar",
       "properties" : [],
       "propertyKeys" : [
          "a",
          "b",
          "c"
       ]
   }
]

In short I get an empty array returned for properties while the propertyKeys is filled in.

What am I doing wrong?

Upvotes: 0

Views: 183

Answers (1)

Dana
Dana

Reputation: 310

I don't think deserialization into an iterable works as you've tried. Could you try something like this instead in your getProperties method?

List<PropertyDefinition> propertyDefinitions = Arrays.asList(mapper.readValue(json, PropertyDefinition[].class))

Upvotes: 1

Related Questions