Commits

Joni Freeman committed d3c410f

Customer serializer should not fail if paranamer fails

Comments (0)

Files changed (4)

framework/lift-base/lift-json/project/build.properties

-#Project Properties
-#Wed Nov 11 19:59:21 EET 2009
+#
+#Mon Jun 28 18:14:37 EEST 2010
+project.organization=net.liftweb
 project.name=json
-project.organization=net.liftweb
-sbt.version=0.5.7-p1
+scala.version=2.7.7
 project.version=2.0-SNAPSHOT
-scala.version=2.7.7
+sbt.version=0.7.4
+build.scala.versions=2.7.7

framework/lift-base/lift-json/project/build/src/LiftJson.scala

   val specs      = "org.scala-tools.testing" % "specs" % "1.6.1"
   val scalacheck = "org.scala-tools.testing" % "scalacheck" % "1.6"
 
-  override def crossScalaVersions = Set("2.7.7", "2.8.0.Beta1-RC1")
+  override def crossScalaVersions = List("2.7.7", "2.8.0.Beta1-RC1")
 
   override def ivyXML =
     <publications>

framework/lift-base/lift-json/src/main/scala/net/liftweb/json/Meta.scala

         def clean(name: String) = name match {
           case Name(text, junk) => text
         }
-        val names = paranamer.lookupParameterNames(c).map(clean)
-        val types = c.getParameterTypes
-        val ptypes = c.getGenericParameterTypes
-        zip3(names.toList, types.toList, ptypes.toList)
+        try {
+          val names = paranamer.lookupParameterNames(c).map(clean)
+          val types = c.getParameterTypes
+          val ptypes = c.getGenericParameterTypes
+          zip3(names.toList, types.toList, ptypes.toList)
+        } catch {
+          case e: ParameterNamesNotFoundException => Nil
+        }
       }
 
       cachedConstructorArgs.memoize(constructor, argsInfo(_))

framework/lift-base/lift-json/src/test/scala/net/liftweb/json/SerializationExamples.scala

 object CustomClassExamples extends Specification {
   import Serialization.{read, write => swrite}
   import JsonAST._
+  import java.util.regex.Pattern
 
   class IntervalSerializer extends Serializer[Interval] {
     private val IntervalClass = classOf[Interval]
     }
   }
 
-  implicit val formats = Serialization.formats(NoTypeHints) + new IntervalSerializer
+  class PatternSerializer extends Serializer[Pattern] {
+    private val PatternClass = classOf[Pattern]
+
+    def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Pattern] = {
+      case (TypeInfo(PatternClass, _), json) => json match {
+        case JObject(JField("$pattern", JString(s)) :: Nil) => Pattern.compile(s)
+        case x => throw new MappingException("Can't convert " + x + " to Pattern")
+      }
+    }
+
+    def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
+      case x: Pattern => JObject(JField("$pattern", JString(x.pattern)) :: Nil)
+    }
+  }
+
+  implicit val formats = 
+    Serialization.formats(NoTypeHints) + new IntervalSerializer + new PatternSerializer
+
   val i = new Interval(1, 4)
   val ser = swrite(i)
   ser mustEqual """{"start":1,"end":4}"""
   val i2 = read[Interval](ser) 
   i2.startTime mustEqual i.startTime
   i2.endTime mustEqual i.endTime
+
+  val p = Pattern.compile("^Curly")
+  val pser = swrite(p)
+  pser mustEqual """{"$pattern":"^Curly"}"""
+  read[Pattern](pser).pattern mustEqual p.pattern
 }
 
 class Interval(start: Long, end: Long) {