Skip to content
This repository
Browse code

Merge branch 'hotfix/0.1.5'

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

0 comments on commit 48070d1

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