简体   繁体   中英

Scala deserializing JSON with json4s issue

when I am deserializing a JSON with the following simple code usign json4s

package main.scala

import org.json4s._
import org.json4s.jackson.JsonMethods._

  object Main {

    case class Person(name: String, age: Int)

    def main(args: Array[String]): Unit = {
      val jsValue = parse("""{"name":"john", "age": 28}""")
      implicit val formats = DefaultFormats
      val p = jsValue.extract[Person]
    }
  }

Is giving the following error.

Exception in thread "main" org.json4s.package$MappingException: scala.Predef$.refArrayOps([Ljava/lang/Object;)Lscala/collection/mutable/ArrayOps; Case classes defined in function bodies are not supported.

Does anyone know why it happens?

As it came out in the comments, this limitation has been surpassed with newer version. However you could have made this work by moving the case class definition outside your main :

package main.scala

import org.json4s._
import org.json4s.jackson.JsonMethods._

// here!
case class Person(name: String, age: Int)

object Main {

  def main(args: Array[String]): Unit = {
    val jsValue = parse("""{"name":"john", "age": 28}""")
    implicit val formats = DefaultFormats
    val p = jsValue.extract[Person]
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM