Simple, hassle-free, zero-boilerplate serialization in Scala/Java similar to Python's Pickle?

后端 未结 5 1195
无人共我
无人共我 2021-01-30 13:10

Is there a simple, hassle-free approach to serialization in Scala/Java that\'s similar to Python\'s pickle? Pickle is a dead-simple solution that\'s reasonably efficient in spa

5条回答
  •  醉酒成梦
    2021-01-30 14:04

    Another good option is the recent (2016) **netvl/picopickle**:

    • Small and almost dependency-less (the core library depends only on shapeless).
    • Extensibility: you can define your own serializators for your types and you can create custom backends, that is, you can use the same library for the different serialization formats (collections, JSON, BSON, etc.); other parts of the serialization behavior like nulls handling can also be customized.
    • Flexibility and convenience: the default serialization format is fine for most uses, but it can be customized almost arbitrarily with support from a convenient converters DSL.
    • Static serialization without reflection: shapeless Generic macros are used to provide serializers for arbitrary types, which means that no reflection is used.

    For example:

    Jawn-based pickler also provides additional functions, readString()/writeString() and readAst()/writeAst(), which [de]serialize objects to strings and JSON AST to strings, respectively:

    import io.github.netvl.picopickle.backends.jawn.JsonPickler._
    
    case class A(x: Int, y: String)
    
    writeString(A(10, "hi")) shouldEqual """{"x":10,"y":"hi"}"""
    readString[A]("""{"x":10,"y":"hi"}""") shouldEqual A(10, "hi")
    

提交回复
热议问题