Browse files

First-class support for Symbol.

  • Loading branch information...
1 parent d2e93a6 commit 1787874b8851e7ec5edd35cb0bde3c666b9048e3 Joni Freeman committed Nov 3, 2009
View
2 lift-base/lift-json/README.md
@@ -369,7 +369,7 @@ Please see other examples in src/test/scala/net/liftweb/json/SerializationExampl
Serialization supports:
* Arbitrarily deep case class graphs
-* All primitive types, including BigInts
+* All primitive types, including BigInt and Symbol
* Lists
* scala.Option
* java.util.Date
View
1 lift-base/lift-json/src/main/scala/net/liftweb/json/Extraction.scala
@@ -143,6 +143,7 @@ object Extraction {
case JInt(x) if (targetType == classOf[String]) => x.toString
case JDouble(x) if (targetType == classOf[Float]) => x.floatValue
case JDouble(x) if (targetType == classOf[String]) => x.toString
+ case JString(s) if (targetType == classOf[Symbol]) => Symbol(s)
case JString(s) if (targetType == classOf[Date]) => formats.dateFormat.parse(s).getOrElse(fail("Invalid date '" + s + "'"))
case JNull => null
case JNothing => fail("Did not find value which can be converted into " + targetType.getName)
View
1 lift-base/lift-json/src/main/scala/net/liftweb/json/Json.scala
@@ -227,6 +227,7 @@ object JsonDSL extends Implicits with Printer {
case None => JNothing
}
+ implicit def symbol2jvalue(x: Symbol) = JString(x.name)
implicit def pair2jvalue[A <% JValue](t: (String, A)) = JObject(List(JField(t._1, t._2)))
implicit def list2jvalue(l: List[JField]) = JObject(l)
implicit def jobject2assoc(o: JObject) = new JsonListAssoc(o.obj)
View
3 lift-base/lift-json/src/main/scala/net/liftweb/json/Meta.scala
@@ -117,7 +117,7 @@ private[json] object Meta {
classOf[Short], classOf[java.lang.Integer], classOf[java.lang.Long],
classOf[java.lang.Double], classOf[java.lang.Float],
classOf[java.lang.Byte], classOf[java.lang.Boolean],
- classOf[java.lang.Short], classOf[Date])
+ classOf[java.lang.Short], classOf[Date], classOf[Symbol])
def safePrimaryConstructorOf[A](cl: Class[A]): Option[JConstructor[A]] =
cl.getDeclaredConstructors.toList match {
@@ -167,6 +167,7 @@ private[json] object Meta {
case x: java.lang.Boolean => JBool(x.asInstanceOf[Boolean])
case x: java.lang.Short => JInt(BigInt(x.asInstanceOf[Short]))
case x: Date => JString(formats.dateFormat.format(x))
+ case x: Symbol => JString(x.name)
case _ => error("not a primitive " + a.asInstanceOf[AnyRef].getClass)
}
}
View
5 lift-base/lift-json/src/test/scala/net/liftweb/json/Examples.scala
@@ -60,6 +60,10 @@ object Examples extends Specification {
compact(render(nulls)) mustEqual """{"f1":null,"f2":[null,"s"]}"""
}
+ "Symbol example" in {
+ compact(render(symbols)) mustEqual """{"f1":"foo","f2":"bar"}"""
+ }
+
"Unicode example" in {
parse("[\" \\u00e4\\u00e4li\\u00f6t\"]") mustEqual JArray(List(JString(" \u00e4\u00e4li\u00f6t")))
}
@@ -156,4 +160,5 @@ object Examples extends Specification {
val nulls = ("f1" -> null) ~ ("f2" -> List(null, "s"))
val quoted = """["foo \" \n \t \r bar"]"""
+ val symbols = ("f1" -> 'foo) ~ ("f2" -> 'bar)
}
View
7 lift-base/lift-json/src/test/scala/net/liftweb/json/ExtractionExamples.scala
@@ -35,7 +35,7 @@ object ExtractionExamples extends Specification {
"Primitive extraction example" in {
val json = parse(primitives)
- json.extract[Primitives] mustEqual Primitives(124, 123L, 126.5, 127.5.floatValue, "128", 125, 129.byteValue, true)
+ json.extract[Primitives] mustEqual Primitives(124, 123L, 126.5, 127.5.floatValue, "128", 'symb, 125, 129.byteValue, true)
}
"Null extraction example" in {
@@ -101,7 +101,8 @@ object ExtractionExamples extends Specification {
"f": 127.5,
"s": "128",
"b": 129,
- "bool": true
+ "bool": true,
+ "sym":"symb"
}
"""
@@ -116,7 +117,7 @@ case class SimplePerson(name: String, address: Address)
case class Name(name: String)
-case class Primitives(i: Int, l: Long, d: Double, f: Float, s: String, sh: Short, b: Byte, bool: Boolean)
+case class Primitives(i: Int, l: Long, d: Double, f: Float, s: String, sym: Symbol, sh: Short, b: Byte, bool: Boolean)
case class OChild(name: Option[String], age: Int, mother: Option[Parent], father: Option[Parent])
case class Parent(name: String)
View
2 lift-base/lift-json/src/test/scala/net/liftweb/json/SerializationExamples.scala
@@ -39,7 +39,7 @@ object SerializationExamples extends Specification {
}
"Primitive serialization example" in {
- val primitives = Primitives(124, 123L, 126.5, 127.5.floatValue, "128", 125, 129.byteValue, true)
+ val primitives = Primitives(124, 123L, 126.5, 127.5.floatValue, "128", 's, 125, 129.byteValue, true)
val ser = swrite(primitives)
read[Primitives](ser) mustEqual primitives
}

0 comments on commit 1787874

Please sign in to comment.