1

Take the following example:

import org.json4s.native.JsonMethods._
import org.json4s._

implicit val formats = DefaultFormats

case class A(name: String)
case class B(age: Int)
val json = parse("""[ {"name": "mark"}, { "age": 27 }, 5 ]""")
json.extract[Tuple3[A, B, Int]]

This errors out:

org.json4s.package$MappingException: No usable value for _1 No usable value for name Did not find value which can be converted into java.lang.String

1 Answer 1

2

Json4s scalaz seems to have tuple support. I am not sure if there is any built in way to do this in json4s. I generally solved this issue something like this

implicit val formats = DefaultFormats

class MySerializer extends CustomSerializer[Tuple3[A,B,Int]](format => (
    {
        case JArray(x :: y :: z :: Nil ) => {
                ( x.extract[A], y.extract[B], z.extract[Int])}
    },
    {
        case x:Tuple3[A,B,Int] => null
    }
))

And then from your code do something like this

implicit val formats = DefaultFormats + new MySerializer
val json = parse("""[ {"name": "mark"}, { "age": 27 }, 5 ]""")
json.extract[Tuple3[A,B,Int]]
Sign up to request clarification or add additional context in comments.

1 Comment

Yea, I ended up going the custom serializer route myself. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.