Skip to content
Browse files

Add docs

  • Loading branch information...
1 parent 5d2c87f commit 0312ed48615dd21b9b8d4f6cf541a608ddd86a92 Joni Freeman committed Mar 21, 2011
View
25 core/json/README.md
@@ -513,6 +513,7 @@ Serialization supports:
* java.util.Date
* Polymorphic Lists (see below)
* Recursive types
+* Serialization of fields of a class (see below)
* Custom serializer functions for types which are not supported (see below)
Serializing polymorphic Lists
@@ -535,6 +536,30 @@ will get an extra field named 'jsonClass' (the name can be changed by overriding
ShortTypeHints outputs short classname for all instances of configured objects. FullTypeHints outputs full
classname. Other strategies can be implemented by extending TypeHints trait.
+Serializing fields of a class
+-----------------------------
+
+To enable serialization of fields, a FieldSerializer can be added for some type:
+
+ implicit val formats = DefaultFormats + FieldSerializer[WildDog]()
+
+Now the type WildDog (and all subtypes) gets serialized with all its fields (+ constructor parameters).
+FieldSerializer takes two optional parameters which can be used to intercept the field serialization:
+
+ case class FieldSerializer[A: Manifest](
+ serializer: PartialFunction[(String, Any), Option[(String, Any)]] = Map(),
+ deserializer: PartialFunction[JField, JField] = Map()
+ )
+
+Those PartialFunctions are called just before a field is serialized or deserialized. Some useful PFs to
+rename and ignore fields are provided:
+
+ val dogSerializer = FieldSerializer[WildDog](
+ renameTo("name", "animalname") orElse ignore("owner"),
+ renameFrom("animalname", "name"))
+
+ implicit val formats = DefaultFormats + dogSerializer
+
Serializing non-supported types
-------------------------------
View
14 core/json/src/main/scala/net/liftweb/json/FieldSerializer.scala
@@ -17,8 +17,20 @@
package net.liftweb
package json
+/**
+ * Serializer which serializes all fields of a class too.
+ *
+ * Serialization can be intercepted by giving two optional PartialFunctions as
+ * constructor parameters:
+ * <p>
+ * <pre>
+ * FieldSerializer[WildDog](
+ * renameTo("name", "animalname") orElse ignore("owner"),
+ * renameFrom("animalname", "name")
+ * )
+ * </pre>
+ */
case class FieldSerializer[A: Manifest](
- // FIXME Option[(String, Any)] -> JField?
serializer: PartialFunction[(String, Any), Option[(String, Any)]] = Map(),
deserializer: PartialFunction[JField, JField] = Map()
)
View
3 core/json/src/main/scala/net/liftweb/json/Formats.scala
@@ -71,6 +71,9 @@ trait Formats { self: Formats =>
def ++ (newSerializers: Traversable[Serializer[_]]): Formats =
newSerializers.foldLeft(this)(_ + _)
+ /**
+ * Adds a field serializer for a given type to this formats.
+ */
def + [A](newSerializer: FieldSerializer[A])(implicit mf: Manifest[A]): Formats = new Formats {
val dateFormat = Formats.this.dateFormat
override val typeHintFieldName = self.typeHintFieldName

0 comments on commit 0312ed4

Please sign in to comment.
Something went wrong with that request. Please try again.