assume I have one base class B and two subclasses S1 and S2.
I want serialize and deserialize class S1 and S2.
Therefore I have two questions:
Is it possible two write a JsonFormat for class B and use it in class S1 and S2 for serialization?
I want two deserialize class S1 and S2. But I do not know if the json String represents class S1 or S2, so I think I can not use spray-json method convertTo because I have to know the exact type I want to deserialize.
My solution to 2. would be to write a wrapper class which contains the a string for the type (S1 or S2) and the json String for S1 or S2. Is this the best way two go or are there other better ways to do it?
Thanks in advance
It's possible but your format won't (and can't) serialize any details of
S1
orS2
that aren't part ofB
.I'd suggest having separate formats for
S1
andS2
and then a custom format forB
that delegates to the appropriate format: