Permalink
Browse files

Merge branch 'hotfix/0.1.5'

  • Loading branch information...
2 parents 6470cb9 + d84ee13 commit 48070d150b296eb7c1762cdcc45e006f633b1a51 @codahale committed Feb 18, 2011
View
5 CHANGELOG.md
@@ -1,3 +1,8 @@
+v0.1.5: Feb 18 2011
+===================
+
+* Added ability to actually serialize `Option` instances. (h/t Alex Cruise)
+
v0.1.4: Jan 17 2011
===================
View
2 README.md
@@ -22,7 +22,7 @@ In your [simple-build-tool](http://code.google.com/p/simple-build-tool/) project
file, add Jerkson as a dependency:
val codaRepo = "Coda Hale's Repository" at "http://repo.codahale.com/"
- val jerkson = "com.codahale" %% "jerkson" % "0.1.4" withSources()
+ val jerkson = "com.codahale" %% "jerkson" % "0.1.5" withSources()
Parsing JSON
View
2 project/build.properties
@@ -3,6 +3,6 @@
project.organization=com.codahale
project.name=jerkson
sbt.version=0.7.5.RC0
-project.version=0.1.4
+project.version=0.1.5
build.scala.versions=2.8.1
project.initialize=false
View
15 src/main/scala/com/codahale/jerkson/ser/OptionSerializer.scala
@@ -0,0 +1,15 @@
+package com.codahale.jerkson.ser
+
+import org.codehaus.jackson.JsonGenerator
+import org.codehaus.jackson.map.{SerializerProvider, JsonSerializer}
+
+class OptionSerializer extends JsonSerializer[Option[_]] {
+ def serialize(value: Option[_], json: JsonGenerator,
+ provider: SerializerProvider) = {
+ if (value.isDefined) {
+ val obj = value.get.asInstanceOf[Object]
+ val serializer = provider.findValueSerializer(obj.getClass)
+ serializer.serialize(obj, json, provider)
+ }
+ }
+}
View
28 src/main/scala/com/codahale/jerkson/ser/ScalaSerializers.scala
@@ -10,19 +10,21 @@ import org.codehaus.jackson.map._
*/
class ScalaSerializers extends Serializers {
def findSerializer(config: SerializationConfig, javaType: JavaType, beanDesc: BeanDescription, beanProp: BeanProperty) = {
- val ser = if (classOf[Seq[_]].isAssignableFrom(beanDesc.getBeanClass)) {
- new SeqSerializer
- } else if (classOf[Map[_,_]].isAssignableFrom(beanDesc.getBeanClass)) {
- new MapSerializer
- } else if (classOf[JValue].isAssignableFrom(beanDesc.getBeanClass)) {
- new JValueSerializer
- } else if (classOf[Either[_,_]].isAssignableFrom(beanDesc.getBeanClass)) {
- new EitherSerializer
- } else if (classOf[Product].isAssignableFrom(beanDesc.getBeanClass)) {
- new CaseClassSerializer(beanDesc.getBeanClass)
- } else {
- null
- }
+ val ser = if (classOf[Option[_]].isAssignableFrom(beanDesc.getBeanClass)) {
+ new OptionSerializer
+ } else if (classOf[Seq[_]].isAssignableFrom(beanDesc.getBeanClass)) {
+ new SeqSerializer
+ } else if (classOf[Map[_,_]].isAssignableFrom(beanDesc.getBeanClass)) {
+ new MapSerializer
+ } else if (classOf[JValue].isAssignableFrom(beanDesc.getBeanClass)) {
+ new JValueSerializer
+ } else if (classOf[Either[_,_]].isAssignableFrom(beanDesc.getBeanClass)) {
+ new EitherSerializer
+ } else if (classOf[Product].isAssignableFrom(beanDesc.getBeanClass)) {
+ new CaseClassSerializer(beanDesc.getBeanClass)
+ } else {
+ null
+ }
ser.asInstanceOf[JsonSerializer[Object]]
}
}
View
12 src/test/scala/com/codahale/jerkson/tests/JsonGenerationSpec.scala
@@ -98,6 +98,18 @@ object JsonGenerationSpec extends Spec {
}
}
+ class `A Some[Int]` {
+ def `should generate a JSON int` {
+ generate(Some(12)) must beEqualTo("12")
+ }
+ }
+
+ class `A None` {
+ def `should not generate anything` {
+ generate(None) must beEqualTo("")
+ }
+ }
+
class `A Seq[Int]` {
def `should generate a JSON array of ints` {
generate(Seq(1, 2, 3)) must beEqualTo("[1,2,3]")

0 comments on commit 48070d1

Please sign in to comment.