0

I am looking for a way in Jackson to allow serialization of a relationship depending on the root element. For example, I have a relationship that is not a true parent/child. Both entities can be queried.

Address {
    @JsonBackReference
    Company company;
    String line1;
    String city;
    String state;
    String zip;
    String country;
}

Company {
    Address mainAddress;

    List<Address> locations;
    String name;
}

The JsonBackReference makes it so there is no circular reference when serializing to json. However, this also causes me to not know the company of an address. If I am listing companies then the addresses come back and everything is good. But if I am listing addresses I would want to see the company be serialized. Is there a way to achieve this?

SchraderMJ11
  • 613
  • 6
  • 12

1 Answers1

0

You need to use Jackson's @JsonTypeInfo for serializing and deserializing. Please look at Jackson's documentation for more details. Try to avoid 1.9.8 version if you have static inner classes (This has a bug and leads to StackOverflowError).

Chris
  • 5,584
  • 9
  • 40
  • 58
  • Can you expand on this a little? I'm not trying to solve which implementation is deserialized with this example. When serializing an Address I want it's clients, but not the clients addresses. And when I'm deserializing a Client I want it's address, but not the address' client. – SchraderMJ11 Oct 01 '12 at 15:51