Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add FieldSerializer

  • Loading branch information...
commit b1be4cc00dea0a43da60d282c8921e2b471202b8 1 parent 897884b
Joni Freeman authored
View
33 core/json/src/main/scala/net/liftweb/json/FieldSerializer.scala
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2009-2011 WorldWide Conferencing, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.liftweb
+package json
+
+case class FieldSerializer[A](
+ serializer: PartialFunction[JField, JField] = Map(),
+ deserializer: PartialFunction[JField, JField] = Map()
+)
+
+object FieldSerializer {
+ def rename(name: String, newName: String): PartialFunction[JField, JField] = {
+ case JField(`name`, x) => JField(newName, x)
+ }
+
+ def ignore(name: String): PartialFunction[JField, JField] = {
+ case JField(`name`, _) => JField(name, JNothing)
+ }
+}
View
2  core/json/src/main/scala/net/liftweb/json/Formats.scala
@@ -68,6 +68,8 @@ trait Formats { self: Formats =>
def ++ (newSerializers: Traversable[Serializer[_]]): Formats =
newSerializers.foldLeft(this)(_ + _)
+ def + (newSerializer: FieldSerializer[_]): Formats = error("implement")
+
def customSerializer(implicit format: Formats) =
customSerializers.foldLeft(Map(): PartialFunction[Any, JValue]) { (acc, x) =>
acc.orElse(x.serialize)
View
66 core/json/src/test/scala/net/liftweb/json/PropertySerializerExamples.scala
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2009-2011 WorldWide Conferencing, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.liftweb
+package json
+
+import org.specs.Specification
+
+// FIXME rename
+object PropertySerializerExamples extends Specification {
+ import Serialization.{read, write => swrite}
+ import FieldSerializer._
+
+ val dog = new WildDog("black")
+ dog.name = "pluto"
+ dog.owner = Owner("joe", 35)
+
+ "All fields are serialized by default" in {
+ implicit val formats = DefaultFormats + FieldSerializer[WildDog]()
+ val ser = swrite(dog)
+ ser mustEqual """{"color":"black","name":"pluto","owner":{"name":"joe","age":35}}"""
+ val dog2 = read[WildDog](ser)
+ dog2.name mustEqual dog.name
+ dog2.color mustEqual dog.color
+ dog2.owner mustEqual dog.owner
+ }
+
+ "Fields can be ignored and renamed" in {
+ val dogSerializer = FieldSerializer[WildDog](
+ rename("name", "animalname") andThen ignore("owner"),
+ rename("animalname", "name")
+ )
+
+ implicit val formats = DefaultFormats + dogSerializer
+
+ val ser = swrite(dog)
+ ser mustEqual """{"color":"black","animalname":"pluto"}"""
+ val dog2 = read[WildDog](ser)
+ dog2.name mustEqual dog.name
+ dog2.color mustEqual dog.color
+ dog2.owner mustEqual null
+ }
+}
+
+abstract class Mammal {
+ var name: String = ""
+ var owner: Owner = null
+}
+
+class WildDog(val color: String) extends Mammal {
+}
+
+case class Owner(name: String, age: Int)
Please sign in to comment.
Something went wrong with that request. Please try again.