0

im trying to serialize an Object to send it over a socket . but the problem is when i serialize the object it will fall in a infinite recursion . i solved the problem of recursion by using the jackson @JsonBackReference and @JsonManagedReference annotations mentioned in this post but the problem is ,it still wont serialize the child object . and even when im using @JsonIgnore it will not serialize the field completely so i'll have a null field when i deserialize it back on the server .

here are my classes:

public class Repository {
    public String name;
    @JsonIgnore
    public ArrayList<UserRole> contributors = new ArrayList<UserRole>();
    public User self;
    @JsonManagedReference
    public ArrayList<FileInfo> files = new ArrayList<FileInfo>();
    public RepositoryType repositoryType;
    @JsonIgnore
    public String path;
}

public class FileInfo {

    private Date addedDate;
    private String path;
    private String name;
    @JsonBackReference
    public Repository relatedRepository; //this field wont be serialized
}

i've commented the field that wont be serialized . what should i do if i want them both to be serialized ? is there any way to do this ?

Community
  • 1
  • 1
Mohammad Siavashi
  • 1,192
  • 2
  • 17
  • 48
  • 1
    @JsonBackReference: `Linkage is handled such that the property annotated with this annotation is not serialized; and during deserialization, its value is set to instance that has the "managed" (forward) link.` – Voo Dec 17 '15 at 08:11
  • then i guess im using it correctly ?! – Mohammad Siavashi Dec 17 '15 at 08:22
  • Well you might want to actually read the documentation before just trying things out until they *seem* to work, but yes. – Voo Dec 17 '15 at 08:25
  • i've read some of the documentation but coudn't find anything useful for my case . is there any other way to serialize them both ? – Mohammad Siavashi Dec 17 '15 at 08:35
  • That's logically impossible (how do you imagine the resulting structure should look like?). Best you can do is have a reference tag so that you know how to correctly deserialize again - which is what this does. – Voo Dec 17 '15 at 08:42

0 Answers0