Custom JSON serialization of nested objects with json4s [duplicate]












2
















This question already has an answer here:




  • Deserialization of case object in Scala with JSON4S

    2 answers




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 NotSerializerclass - case bE:BooleanExpression part.










share|improve this question















marked as duplicate by Community Nov 25 '18 at 9:15


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Your code doesn't make sense. You're not serializing a boolean, you're serializing a boolean expression. So you shouldn't be casting it to a boolean. Are you sure this is the most recent version of your code?

    – Robin Green
    Nov 24 '18 at 17:17











  • @RobinGreen yes, you are right. I have uploaded the latest version of my code. Removed cast to Boolean, but now I am getting another output. I have updated the question

    – TARS
    Nov 24 '18 at 17:30
















2
















This question already has an answer here:




  • Deserialization of case object in Scala with JSON4S

    2 answers




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 NotSerializerclass - case bE:BooleanExpression part.










share|improve this question















marked as duplicate by Community Nov 25 '18 at 9:15


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Your code doesn't make sense. You're not serializing a boolean, you're serializing a boolean expression. So you shouldn't be casting it to a boolean. Are you sure this is the most recent version of your code?

    – Robin Green
    Nov 24 '18 at 17:17











  • @RobinGreen yes, you are right. I have uploaded the latest version of my code. Removed cast to Boolean, but now I am getting another output. I have updated the question

    – TARS
    Nov 24 '18 at 17:30














2












2








2









This question already has an answer here:




  • Deserialization of case object in Scala with JSON4S

    2 answers




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 NotSerializerclass - case bE:BooleanExpression part.










share|improve this question

















This question already has an answer here:




  • Deserialization of case object in Scala with JSON4S

    2 answers




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 NotSerializerclass - case bE:BooleanExpression part.





This question already has an answer here:




  • Deserialization of case object in Scala with JSON4S

    2 answers








json scala serialization json4s






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 19:55









Robin Green

22.5k875155




22.5k875155










asked Nov 24 '18 at 16:51









TARSTARS

183421




183421




marked as duplicate by Community Nov 25 '18 at 9:15


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Community Nov 25 '18 at 9:15


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    Your code doesn't make sense. You're not serializing a boolean, you're serializing a boolean expression. So you shouldn't be casting it to a boolean. Are you sure this is the most recent version of your code?

    – Robin Green
    Nov 24 '18 at 17:17











  • @RobinGreen yes, you are right. I have uploaded the latest version of my code. Removed cast to Boolean, but now I am getting another output. I have updated the question

    – TARS
    Nov 24 '18 at 17:30














  • 1





    Your code doesn't make sense. You're not serializing a boolean, you're serializing a boolean expression. So you shouldn't be casting it to a boolean. Are you sure this is the most recent version of your code?

    – Robin Green
    Nov 24 '18 at 17:17











  • @RobinGreen yes, you are right. I have uploaded the latest version of my code. Removed cast to Boolean, but now I am getting another output. I have updated the question

    – TARS
    Nov 24 '18 at 17:30








1




1





Your code doesn't make sense. You're not serializing a boolean, you're serializing a boolean expression. So you shouldn't be casting it to a boolean. Are you sure this is the most recent version of your code?

– Robin Green
Nov 24 '18 at 17:17





Your code doesn't make sense. You're not serializing a boolean, you're serializing a boolean expression. So you shouldn't be casting it to a boolean. Are you sure this is the most recent version of your code?

– Robin Green
Nov 24 '18 at 17:17













@RobinGreen yes, you are right. I have uploaded the latest version of my code. Removed cast to Boolean, but now I am getting another output. I have updated the question

– TARS
Nov 24 '18 at 17:30





@RobinGreen yes, you are right. I have uploaded the latest version of my code. Removed cast to Boolean, but now I am getting another output. I have updated the question

– TARS
Nov 24 '18 at 17:30












1 Answer
1






active

oldest

votes


















0














Instead of serialising the value, you need to serialise the contained expression. So don't create a JBool - instead, call Extraction.decompose - and don't call .value, because that evaluates the Boolean expression.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Instead of serialising the value, you need to serialise the contained expression. So don't create a JBool - instead, call Extraction.decompose - and don't call .value, because that evaluates the Boolean expression.






    share|improve this answer




























      0














      Instead of serialising the value, you need to serialise the contained expression. So don't create a JBool - instead, call Extraction.decompose - and don't call .value, because that evaluates the Boolean expression.






      share|improve this answer


























        0












        0








        0







        Instead of serialising the value, you need to serialise the contained expression. So don't create a JBool - instead, call Extraction.decompose - and don't call .value, because that evaluates the Boolean expression.






        share|improve this answer













        Instead of serialising the value, you need to serialise the contained expression. So don't create a JBool - instead, call Extraction.decompose - and don't call .value, because that evaluates the Boolean expression.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 19:54









        Robin GreenRobin Green

        22.5k875155




        22.5k875155

















            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python