I'm having trouble writing a custom serialiser for Json4s to handle the following situation. I have case classes:
trait Condition
case class BasicExpression (field:String, operator:String, value:String) extends Condition
case class BooleanExpression (val leftExpr: Condition, val logicalOperator:String,
val rightExpr: Condition) extends Condition
and I want to be able to read the JSON for both BasicExpression
and BooleanExpression
using, for example:
var jsonStringBasic:String = """ {"field":"name","operator":"=","value":"adam"}""";
var jsonStringBoolean:String = """{"leftExpr":{"leftExpr":{"field":"field1", "operator":"=", "value":"value1"}, "logicalOperator":"AND", "rightExpr":{"field":"field2","operator":">","value":"500"}}, "logicalOperator":"AND", "rightExpr": {"field":"field3","operator":"<","value":"10000"}}""";
var jValueBasic:JValue = parse(jsonStringBasic, false);
var readCBasic = jValueBasic.extract[Condition];
I understand how the custom serialiser works for reading the BasicExpression
, and I could use SimpleTypeHints
, but it'd be good to not have to bloat the JSON for every Condition
. I could also try extract[BooleanExpression]
and if that fails try extract[BasicExpression]
, but that seems ugly. Is it possible to write the custom serialiser to handle the fact that a BooleanCondition
will itself contain another Condition
recursively so I can extract[Condition]
?