I have the following structure:
sealed trait BooleanExpression extends Serializable {
def value: Boolean
}
case object True extends BooleanExpression with Serializable {
val value: Boolean = true
}
case object False extends BooleanExpression with Serializable {
val value: Boolean = false
}
case class Not(e: BooleanExpression) extends BooleanExpression with Serializable {
val value = !e.value
}
And I'd like to serialize the Not class with the help of Json4s custom serializer (my implementation):
object NotSerializer extends CustomSerializer[Not](format => ( {
//deserialize
case JObject(
JField("e", null) ::
Nil
) => Not(null)
}, {
//serialize
case not: Not => JObject(
JField("not", JBool(not.e.value))
)
}))
Main class looks as follows:
object Main extends App {
implicit val formats: Formats = Serialization.formats(NoTypeHints) + NotSerializer
val not = Not(Not(Not(False)))
println(writePretty(not))
}
The class is serialized as follows:
{
"not" : true
}
What I am expecting to see is :
{
"not" : {
"not" : {
"not" : {true}
}
}
}
Can't find the bug. What am I doing wrong?
Would be much appreciated for any help.
I have updated the NotSerializer
class - case bE:BooleanExpression
part.