Json4s

在写spark项目时遇到scala对象与json对象之间转换的问题,花点时间总结一下。

Json4s介绍

Json4s下面有两个类型的包

  • Lift Json
import org.json4s._
import org.json4s.native.JsonMethods._

native package具有lift-json的所有功能。

  • Jackson
import org.json4s._
import org.josn4s.jackson.JsonMethods._

与native package不同的是,jackson包含了大部分jackson-module-scala的功能,也可以使用lift-json下的所有功能。在这个项目中我们使用jackson。

Json AST

lift-json library 的核心就是Json AST,它将Json对象转换成了语法树。

sealed abstract class JValue
case object JNothing extends JValue // 'zero' for JValue
case object JNull extends JValue
case class JString(s: String) extends JValue
case class JDouble(num: Double) extends JValue
case class JDecimal(num: BigDecimal) extends JValue
case class JInt(num: BigInt) extends JValue
case class JBool(value: Boolean) extends JValue
case class JObject(obj: List[JField]) extends JValue
case class JArray(arr: List[JValue]) extends JValue

type JField = (String, JValue)

转换的常见方式有:

  • Json DSL(implicit)
  • String parse
  • case class

下面一一介绍。

Json4s安装

  • SBT users
  • native support
val json4sNative = "org.json4s" %% "json4s-native" % "{latestVersion}"
  • jackson support
val json4sJackson = "org.json4s" %% "json4s-jackson" % "{latestVersion}"
  • Maven users
  • native support

  org.json4s
  json4s-native_${scala.version}
  {latestVersion}

  • jackson support

  org.json4s
  json4s-jackson_${scala.version}
  {latestVersion}

JSON转换

这里我们都以jackson为例,native support请参考http://json4s.org/
一个有效的json对象可以转换成AST格式

String parse
import org.json4s._
import org.json4s.jackson.JsonMethods._
parse("""{ "numbers" : [1, 2, 3, 4] }""")
parse("""{"name":"Toy","price":35.35}""", useBigDecimalForDouble = true)
DSL Parse

任意scala原始对象与json原始对象之间的转换

#List to json
scala> val json = List(1, 2, 3)
scala> compact(render(json))
res0: String = [1,2,3]
#Map to json
scala> val json = ("name" -> "joe")
scala> compact(render(json))
res1: String = {"name":"joe"}
#当有多行数据时,用~符号连接
scala> val json = ("name" -> "joe") ~ ("age" -> 35)
scala> compact(render(json))
res2: String = {"name":"joe","age":35}
case class

当你有自定义的数据类型想进行转换时,可以使用下面的方法

type DslConversion = T => JValue
object JsonExample extends App {
  import org.json4s._
  import org.json4s.JsonDSL._
  import org.json4s.jackson.JsonMethods._
  
  #定义类 Winner, Lotto
  case class Winner(id: Long, numbers: List[Int])
  case class Lotto(id: Long, winningNumbers: List[Int], winners: List[Winner], drawDate: Option[java.util.Date])

  val winners = List(Winner(23, List(2, 45, 34, 23, 3, 5)), Winner(54, List(52, 3, 12, 11, 18, 22)))
  val lotto = Lotto(5, List(2, 45, 34, 23, 7, 5, 3), winners, None)
  
  #转换json
  val json =
    ("lotto" ->
      ("lotto-id" -> lotto.id) ~
      ("winning-numbers" -> lotto.winningNumbers) ~
      ("draw-date" -> lotto.drawDate.map(_.toString)) ~
      ("winners" ->
        lotto.winners.map { w =>
          (("winner-id" -> w.id) ~
           ("numbers" -> w.numbers))}))

  println(compact(render(json)))
}
scala> JsonExample
{"lotto":{"lotto-id":5,"winning-numbers":[2,45,34,23,7,5,3],"winners":
[{"winner-id":23,"numbers":[2,45,34,23,3,5]},{"winner-id":54,"numbers":[52,3,12,11,18,22]}]}}

如果想将结果打印出来,可以使用

scala> pretty(render(JsonExample.json))

{
  "lotto":{
    "lotto-id":5,
    "winning-numbers":[2,45,34,23,7,5,3],
    "winners":[{
      "winner-id":23,
      "numbers":[2,45,34,23,3,5]
    },{
      "winner-id":54,
      "numbers":[52,3,12,11,18,22]
    }]
  }
}

你可能感兴趣的:(Json4s)