Skip to content
This repository
Browse code

Rename

  • Loading branch information...
commit 411fca46b813259b8b58fcc0af7055c4582450e8 1 parent b1be4cc
authored March 19, 2011
65  core/json/src/test/scala/net/liftweb/json/FieldSerializerExamples.scala
... ...
@@ -0,0 +1,65 @@
  1
+/*
  2
+ * Copyright 2009-2011 WorldWide Conferencing, LLC
  3
+ *
  4
+ * Licensed under the Apache License, Version 2.0 (the "License");
  5
+ * you may not use this file except in compliance with the License.
  6
+ * You may obtain a copy of the License at
  7
+ *
  8
+ *     http://www.apache.org/licenses/LICENSE-2.0
  9
+ *
  10
+ * Unless required by applicable law or agreed to in writing, software
  11
+ * distributed under the License is distributed on an "AS IS" BASIS,
  12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+ * See the License for the specific language governing permissions and
  14
+ * limitations under the License.
  15
+ */
  16
+
  17
+package net.liftweb
  18
+package json
  19
+
  20
+import org.specs.Specification
  21
+
  22
+object FieldSerializerExamples extends Specification {  
  23
+  import Serialization.{read, write => swrite}
  24
+  import FieldSerializer._
  25
+
  26
+  val dog = new WildDog("black")
  27
+  dog.name = "pluto"
  28
+  dog.owner = Owner("joe", 35)
  29
+
  30
+  "All fields are serialized by default" in {
  31
+    implicit val formats = DefaultFormats + FieldSerializer[WildDog]()
  32
+    val ser = swrite(dog)
  33
+    ser mustEqual """{"color":"black","name":"pluto","owner":{"name":"joe","age":35}}"""
  34
+    val dog2 = read[WildDog](ser) 
  35
+    dog2.name mustEqual dog.name
  36
+    dog2.color mustEqual dog.color
  37
+    dog2.owner mustEqual dog.owner
  38
+  }
  39
+
  40
+  "Fields can be ignored and renamed" in {
  41
+    val dogSerializer = FieldSerializer[WildDog](
  42
+      rename("name", "animalname") andThen ignore("owner"),
  43
+      rename("animalname", "name")
  44
+    )
  45
+
  46
+    implicit val formats = DefaultFormats + dogSerializer
  47
+
  48
+    val ser = swrite(dog)
  49
+    ser mustEqual """{"color":"black","animalname":"pluto"}"""
  50
+    val dog2 = read[WildDog](ser) 
  51
+    dog2.name mustEqual dog.name
  52
+    dog2.color mustEqual dog.color
  53
+    dog2.owner mustEqual null
  54
+  }
  55
+}
  56
+
  57
+abstract class Mammal {
  58
+  var name: String = ""
  59
+  var owner: Owner = null
  60
+}
  61
+
  62
+class WildDog(val color: String) extends Mammal {
  63
+}
  64
+
  65
+case class Owner(name: String, age: Int)

0 notes on commit 411fca4

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