Currently I'm using lift-json
to parse json into objects. This is the signature I generally use
net.liftweb.json.parse(json).extract[MyClass]
This is working fine. However, I'm looking to do a little more. Lets say My class has an object of type List[SomeTrait]
where SomeTrait
is a trait
. And I have two different implementations of that trait with the different constructor signatures, but obviously implementing the same methods differently.
Is there a way in either the JSON or the code that will detect which implementation it should use? Such that, the code parsing the json can remain the same but I can continue to add new implementations of SomeTrait
There's no easy way to do that in Lift, as far as I know. You still need to have some attribute to be able to provide the type information about the JSON object itself, so the deserializer will be able to pick up the proper instance.
I'd use Jackson JsonTypeInfo
annotation in order to mark subclasses and then use it's ObjectMapper
to do the job.
Look https://github.com/FasterXML/jackson-annotations#handling-polymorphic-types for more details.