0

I'm currently facing the following problem: Imagine I have an abstract Class A for filtering specific entities. So I have a PassThroughFilter extending A, which does nothing (NIL-Element). Through a decorator pattern I have an other abstract class A_Dec, which holds the property type. Further, I have classes Band C both extending A_Dec. They use the type for various things, so they are logically different. A sample filter then could be: C(type=XC, decorator=B(type=XB, decorator=A))

Not I want to create a JSON to save to current filter. Which is easy to do, BUT for the JSON:

{type=X, decorator={}}

where {} represents AI cannot distinguish between B and C.

An easy fix would be to create a local variable with the name of the class or something other unique. But is there a proper way to handle this?

Thank you in advance!

  • Try to use `JsonTypeInfo` and `JsonSubTypes` annotations from `Jackson` library. See: [Jackson polymorphic JSON deserialization of an object with an interface property?](https://stackoverflow.com/questions/21485923/java-jackson-polymorphic-json-deserialization-of-an-object-with-an-interface-pr), [What @JsonTypeInfo.ID to choose for property = “type.id” for deserialization, JsonTypeInfo.Id.CUSTOM?](https://stackoverflow.com/questions/25671806/what-jsontypeinfo-id-to-choose-for-property-type-id-for-deserialization-js) – Michał Ziober Sep 23 '19 at 09:20
  • Since I'm not using Jackson, but Gson I can't use the annotations. But still thanks! – Timo Bergerbusch Sep 23 '19 at 09:23
  • If you are looking for a solution for `Gson`, take a look on [How to work around Gson serialization giving different results when using generic wildcards?](https://stackoverflow.com/questions/55222355/how-to-work-around-gson-serialization-giving-different-results-when-using-generi/55227392#55227392) and especially on `TypeAwareListJsonAdapter` class. – Michał Ziober Sep 23 '19 at 10:40

0 Answers0