Custom JSON serialization of nested objects with json4s [duplicate]
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 NotSerializer
class - case bE:BooleanExpression
part.
json scala serialization json4s
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.
add a comment |
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 NotSerializer
class - case bE:BooleanExpression
part.
json scala serialization json4s
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
add a comment |
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 NotSerializer
class - case bE:BooleanExpression
part.
json scala serialization json4s
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 NotSerializer
class - 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
json scala serialization json4s
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 24 '18 at 19:54
Robin GreenRobin Green
22.5k875155
22.5k875155
add a comment |
add a comment |
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