通过scala-pickling序列化后从json中删除tpe

时间:2015-01-22 22:40:34

标签: json scala serialization scala-pickling

是否有一种简单的方法可以在对象内部没有“tpe”字段的情况下序列化为json? 我需要将case类序列化为json结构,然后通过线路发送它们(它们将来不会被反序列化)。我有一个特定的api,所以..我不需要额外的字段。

对于下面说明的课程Person

case class Person(Name: String, Age: int, CandyLover: Boolean)

我想看到以下内容:

{
  "Name": "Paul",
  "Age": 23,
  "CandyLover": true
}

3 个答案:

答案 0 :(得分:4)

我建议你看一下喷-json或argonaut。如果你已经在使用Play,请播放-json。

Scala pickling不是真正的json库,它不是为公共API生成JSON而创建的,它不灵活,您不能先定义JSON协议,然后提供酸洗序列化以匹配协议。 Pickling用于计算机到计算机的序列化,其中没有人真正关心应用程序之间的字节。

答案 1 :(得分:1)

最简单的方法可能是将Hint以某种方式传递给JSONPickleBuilder。快速执行此操作是通过从JSONPickleFormat调用hintStaticallyElidedType()方法来覆盖PickleTools中的构建器。

以下是一种演示它的代码片段:

import org.specs2.matcher.JsonMatchers
import org.specs2.mutable.Specification
import org.specs2.specification.Scope

import scala.pickling.Defaults._
import scala.pickling.json.{JSONPickleBuilder, JSONPickleFormat, JsonFormats}
import scala.pickling.{Output, PBuilder, PickleTools, StringOutput}

class PersonJsonFormatsTest extends Specification with JsonMatchers {
  trait Context extends Scope with PersonJsonFormats

  trait PersonJsonFormats extends JsonFormats {
    override implicit val pickleFormat: JSONPickleFormat = new PersonJSONPickleFormat
  }

  class PersonJSONPickleFormat extends JSONPickleFormat {
    override def createBuilder() = new JSONPickleBuilder(this, new StringOutput) with PickleTools {
      hintStaticallyElidedType()
    }
    override def createBuilder(out: Output[String]): PBuilder = new JSONPickleBuilder(this, out) with PickleTools {
      hintStaticallyElidedType()
    }
  }

  "Pickle" should {
    "Serialize Person without $type field in resulting JSON" in new Context {
      case class Person(Name: String, Age: Int, CandyLover: Boolean)

      val pickledPersonObject = Person("Paul", 23, CandyLover = true).pickle
      println(pickledPersonObject.value)
      pickledPersonObject.value must not */("$type" → ".*".r)
    }
  }
}

println(pickledPersonObject.value)的输出将符合您的要求:

{
  "Name": "Paul",
  "Age": 23,
  "CandyLover": true
}

通过这种方式,您将与更多的泡菜更新保持一致。

P.S。如果有人知道更优雅和方便的方式达到相同的行为 - 请告诉我们: - )

答案 2 :(得分:0)

对于scala-pickling 0.10.x:

import scala.pickling._
import scala.pickling.json.{JSONPickle, JSONPickleBuilder, JSONPickleFormat, JsonFormats}
import scala.pickling.pickler.AllPicklers

object serialization extends JsonFormats with Ops with AllPicklers {
  override implicit val pickleFormat: JSONPickleFormat = new JSONPickleFormat {

    private def setHints(h: Hintable): Unit = {
      h.hintStaticallyElidedType()
      h.hintDynamicallyElidedType()
    }

    override def createBuilder(): JSONPickleBuilder = {
      val builder = super.createBuilder()
      setHints(builder)
      builder
    }

    override def createBuilder(out: Output[String]): PBuilder = {
      val builder = super.createBuilder(out)
      setHints(builder)
      builder
    }

    override def createReader(pickle: JSONPickle): PReader = {
      val reader = super.createReader(pickle)
      setHints(reader)
      reader
    }
  }
}

object SerializationTest extends App {
  import serialization._

  case class Person(firstName: String, lastName: String)

  val pickle: JSONPickle = Person("Evelyn", "Patterson").pickle
  val jsonString: String = pickle.value // {"firstName": "Evelyn","lastName": "Patterson"}

  val person: Person = jsonString.unpickle[Person]
}

对于scala-pickling 0.11.x:

import scala.pickling._
import scala.pickling.json.{JSONPickle, JSONPickleBuilder, JSONPickleFormat}
import scala.pickling.pickler.AllPicklers

object serialization extends AllPicklers {

  private final class CustomJSONPickleFormat(tag: FastTypeTag[_]) extends JSONPickleFormat {

    private def setHints(h: Hintable) {
      h.hintElidedType(tag)
    }

    override def createBuilder(): JSONPickleBuilder = {
      val b = super.createBuilder()
      setHints(b)
      b
    }
    override def createBuilder(out: Output[String]): PBuilder = {
      val b = super.createBuilder(out)
      setHints(b)
      b
    }
    override def createReader(pickle: JSONPickle): PReader = {
      val b = super.createReader(pickle)
      setHints(b)
      b
    }
  }

  implicit val staticOnly = static.StaticOnly // for compile time serialization methods generation

  implicit final class EncodeDecodeOps[T](picklee: T) {
    def encode(implicit pickler: Pickler[T]): String = {
      val pickleFormat = new CustomJSONPickleFormat(pickler.tag)
      functions.pickle(picklee)(pickleFormat, pickler).value
    }

    def decode[A](implicit c: T => String, unpickler: Unpickler[A]): A = {
      val pickleFormat = new CustomJSONPickleFormat(unpickler.tag)
      functions.unpickle[A](json.JSONPickle(picklee))(unpickler, pickleFormat)
    }
  }
}

case class Person(firstName: String, lastName: String) {
   @transient var x = "test"
}

object SerializationTest extends App {
  import serialization._

  val jsonString = Person("Lisa", "Daniels").encode

  println(jsonString)

  val person = jsonString.decode[Person]

  println(person)
}