0

I have an list of things that I return as a jackson list. What I would like is this:

"things": { [
        {
          "id": "1234",

..list of students

but currently, I am getting this:

"things": {
      "HashSet": [
        {
          "id": "1234",

I am using a JsonSerializer>, which is why it adds the HashSet field. I tried adding a json property on the field, but its not allowed since it is a local variable.

I am currently using the jackson library and have looked into:

Jackson annotation - How to rename element names?

How to rename root key in JSON serialization with Jackson

but they seem to have a different issue altogether. Any ideas? Thanks!

Edit:

Added the class implementations. Note that I call owner, that contains Things. Also, my jpa annotations are there as well. Thanks.

@Entity @Table(name = "owner") 
public class Owner extends BaseEntity implements Persistence {

@Column 
private String name;

@Column 
private String description;

@Column 
private Integer capacity;

@Column
@JsonSerialize(using = ThingSerializer.class)
@JsonProperty("things")
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "owners")
private Set<Thing> tihngs = new HashSet<>();


public class ThingSerializer extends JsonSerializer<Set<Thing>> {

@Override public void serialize(Set<Thing> thingSet, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws
                                                                                                                           IOException,
                                                                                                                           JsonProcessingException {

    Set<Thing> things = new HashSet<>();

    thingSet.forEach(thing -> {

        Thing t = new Thing();

        t.setCreatedBy(thing.getCreatedBy());
        t.setCreationDate(thing.getCreationDate());
        t.setId(thing.getId());
        t.setDateModified(thing.getDateModified());
        t.setModifiedBy(thing.getModifiedBy());
        t.setStatus(thing.getStatus());

        things.add(s);
    });

    jsonGenerator.writeObject(things);

}

}

Thing Entity

@Entity
@Table(name = "thing")
public class Thing extends BaseEntity implements Persistence {

private static final long serialVersionUID = 721739537425505668L;

private String createdBy;
private Date creationDate;
.
.
.




@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(name = "ThingOwner", joinColumns = @JoinColumn(name = "thing_id") , inverseJoinColumns = @JoinColumn(name = "owner_id") )
private Set<Owner> owners = new HashSet<>();
Jay12
  • 29
  • 4

1 Answers1

0

Why don't you use ObjectMapper to serialize and deserialize your data?

Have a look at this small test, i believe it does what you want:

@Test
public void myTest() throws Exception{
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(
            mapper.getSerializationConfig().
            getDefaultVisibilityChecker().
            withFieldVisibility(JsonAutoDetect.Visibility.ANY).
            withGetterVisibility(JsonAutoDetect.Visibility.NONE).
            withSetterVisibility(JsonAutoDetect.Visibility.NONE).
            withCreatorVisibility(JsonAutoDetect.Visibility.NONE).
            withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);

    mapper.setSerializationInclusion(Include.NON_NULL);

    TestObject value = new TestObject();
    value.a = "TestAvalue";
    value.set = new HashSet<>();
    value.set.add(new SetValueObject(1, 1));
    value.set.add(new SetValueObject(2, 2));
    String test = mapper.writeValueAsString(value);
    System.out.println(test);
}


public class TestObject{
    String a;
    Set<SetValueObject> set;
}

public class SetValueObject{

    public SetValueObject(int a, int b){
        this.a = a;
        this.b = b;
    }


    int a;
    int b;
}

Output:

{"a":TestAvalue","set":[{"a":1,"b":1},{"a":2,"b":2}]}

Tested with Jackson 2.6.1

And i have modified one of my tests, so i'am not sure that you need all of this ObjectMapper config => it's just to give you an idea of another approach by using ObjectMapper.

user3227576
  • 554
  • 8
  • 22