1.1 Supported Types
indent parameter formats the output in indented format.
The data types include:
Stringare serialzed as strings.
Mapetc. are serialized as JSON lists.
Options are serialized as JSON lists with 0 or 1 element.
Tuples from 1 to 22 are serialized as heterogenous JSON lists.
case object, stand-alone.
nulls seraialize into JSON nulls.
uPickle only throws exceptions on unpickling. Readability and writability is recursive: a class is only serializable if all its fields are serializable. Case classes are serialized using the
unapply methos on their companion objects.
If a field is missing upon deserialization, uPickle uses the ddefault value for the type if one exists.
1.2 Case Classes
Case classes of sizes 1-22 are serialized as JSON dictionaries. A serializer should be defined in the case class’s companion object as:
implicit val rw: ReadWriter[MyCaseClass] = macroRW.
Sealed hierarchies are serialized as tagged values, the serialized object tagged with the full name of the instance’s class.
1.3 Read/Writing Other Things
Other than strings, uPickle can read/write form other sources such as
1.4 Common Operations
readwriter[T].bimap[V](toT, toV) function to create a pickler that reads/writes a type
V, using the pickler for type
T, by providing a conversion function between them.
More control can be done using
readwriter[ujson.Value].bimap[V](toujson, toV) and raw JSON AST.
@upickle.implicits.key("name") to custom the key of a field. It can also be used to custom the tagged class name.
Use a custom pickler to change project-wide configuration.
uPickle doesn’t support:
- circular object graphs
- reflective reading and writing
- read/write of untyped values such as
- read/write arbitrarily shaped objects
- read/write case classes with multiple parameter lists
Due to a Scala bug, automatic sealed trait pickling can fail unpredictably. This can be fixed by using the
uJson is uPickle’s JSON lib. It is used to manipulate JSON source and data structures without converting them into Scala case-classes.
2.1 uJson Values
You can use
ujson to construct JSON blobs, either programmatically or parsing from strings, byte arrays or files.
ujson.Js AST (
ujson.Value) is mutable. A
ujson.Value can be one of the following types:
You can use
obj etc to casts a
ujson.Value to the specific sub-type and corresponding value. Each type has a
Opt postfix method for optional values.
You can construct JSON data using the above types or primitive types:
ujson.write to serialize a uJson value into string.
2.2 Converting To/From Scala Data Types
Scala case classes or other data structures can be converted to
ujson.Js AST using
upickle.default.readJs or plian
uJson allows you convert between any of the following forms:
- case class and Scala data types
- Third party JSON ASTs such as
The case class can be sested.
ujson.Value can be stroed as a case class field.