I am having issues serialising / deserialising a case class with an Array[Byte] field. I am using lift-json and have created a case class as below:
case class TestNestedClass(matchKey: Array[Byte],
matchId: Long,
name: String)
I am then using the Wrapper functionality to attempt to create from JSON and to JSON methods.
case class TestNestedClassWrapper(tnc: TestNestedClass) {
implicit val formats: Formats = DefaultFormats + ArrayByteSerializer
def toJson: String = {
val json = Extraction.decompose(this)
pretty(render(json))
}
}
case object TestNestedClassWrapper {
implicit val formats: Formats = DefaultFormats + ArrayByteSerializer
def apply(jsonString: String): TestNestedClassWrapper = {
read[TestNestedClassWrapper](jsonString)
}
}
The extra custom serialiser for ArrayBytes is as follows:
object ArrayByteSerializer extends Serializer[Array[Byte]] {
private val Class = classOf[Array[Byte]]
def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Array[Byte]] = {
case (TypeInfo(Class, _), json) => json match {
case JString(iv) => Bytes.toBytes(iv)
case value => throw new MappingException("Can't convert " + value + " to " + Class)
}
}
def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
case s: Array[Byte] => JString(Bytes.toString(s))
}
}
When I test these methods, it seems that they are not working correctly. The class TestNestedClass does not show up at all:
val test1 = TestNestedClass(Bytes.toBytes("1234"),1234,"test1")
val testNestedClassWrapper = TestNestedClassWrapper(test1)
val json = testNestedClassWrapper.toJson
println(json)
RES
{
"tnc":{
}
}
If I remove the Array[Byte] field I can serialise the case class successfully:
case class TestNestedClass2(matchId: Long,
name: String)
RES
{
"tnc":{
"matchId":1234,
"name":"test1"
}
}
Is there any reason, that my ArrayByteSerializer is not working?
Thanks!
--
--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
---
You received this message because you are subscribed to the Google Groups "Lift" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liftweb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
import org.apache.hadoop.hbase.util.Bytes.
If I only use Default Formats I get nothing returned back as in the case with the custom serializer:
case class TestClassWrapper(tc: TestClass) {
implicit val formats: Formats = DefaultFormats
def toJson: String = {
val json = Extraction.decompose(this)
pretty(render(json))
}
}
case object TestClassWrapper {
implicit val formats: Formats = DefaultFormats
def apply(jsonString: String): TestClassWrapper = {
read[TestClassWrapper](jsonString)
}
}Also as a question, how did you define the test cases you used for TestNestedClassi.e. this: TestNestedClass(Array(58, 23), 123, "Bacon")What are you defining the byte representation??Thanks!