Skip to content

Commit

Permalink
Merge branch 'release/0.3.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
codahale committed Jun 10, 2011
2 parents 6c9645e + 9965f5e commit efb35cb
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
v0.3.2: Jun 09 2011
===================

* Added `Json.parse[A](Reader)`.
* Fix `NullPointerException` when deserializing `Map` instances from weird JSON
values.


v0.3.1: Jun 05 2011
===================

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Go ahead and add Jerkson as a dependency:
<dependency>
<groupId>com.codahale</groupId>
<artifactId>jerkson_${scala.version}</artifactId>
<version>0.3.1</version>
<version>0.3.2</version>
</dependency>
</dependencies>
```
Expand All @@ -50,6 +50,8 @@ parse[List[Int]]("[1,2,3]") //=> List(1,2,3)
parse[Map[String, Int]]("""{"one":1,"two":2}""") //=> Map("one"->1,"two"->2)

// Parse JSON objects as case classes
// (Nested/inner case classes aren't supported.)
// (Parsing case classes isn't supported in the REPL.)
case class Person(id: Long, name: String)
parse[Person]("""{"id":1,"name":"Coda"}""") //=> Person(1,"Coda")

Expand Down
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.codahale</groupId>
<artifactId>jerkson_2.9.0-1</artifactId>
<version>0.3.1</version>
<version>0.3.2</version>
<name>Jerkson for Scala ${scala.version}</name>

<properties>
Expand Down Expand Up @@ -119,6 +119,13 @@
</args>
<charset>UTF-8</charset>
</configuration>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>jline</artifactId>
<version>${scala.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom_2.8.1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.codahale</groupId>
<artifactId>jerkson_2.8.1</artifactId>
<version>0.3.1</version>
<version>0.3.2</version>
<name>Jerkson for Scala ${scala.version}</name>

<properties>
Expand Down
9 changes: 9 additions & 0 deletions src/main/scala/com/codahale/jerkson/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ trait Parser extends Factory {
new StreamingIterator[A](parser, mf)
}

/**
* Parse a streaming JSON array of particular types, returning an iterator
* of the elements of the stream.
*/
def stream[A](input: Reader)(implicit mf: Manifest[A]): Iterator[A] = {
val parser = factory.createJsonParser(input)
new StreamingIterator[A](parser, mf)
}

private[jerkson] def parse[A](parser: JsonParser, mf: Manifest[A]): A = {
try {
if (mf.erasure == classOf[Option[_]]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class ImmutableMapDeserializer[CC[A, B] <: Map[A, B] with MapLike[A, B, CC[A, B]
jp.nextToken()
}

if (jp.getCurrentToken != JsonToken.FIELD_NAME &&
jp.getCurrentToken != JsonToken.END_OBJECT) {
throw ctxt.mappingException(valueType.getRawClass)
}

while (jp.getCurrentToken != JsonToken.END_OBJECT) {
val name = jp.getCurrentName
jp.nextToken()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class IntMapDeserializer(valueType: JavaType,
jp.nextToken()
}

if (jp.getCurrentToken != JsonToken.FIELD_NAME &&
jp.getCurrentToken != JsonToken.END_OBJECT) {
throw ctxt.mappingException(valueType.getRawClass)
}

while (jp.getCurrentToken != JsonToken.END_OBJECT) {
try {
val name = jp.getCurrentName.toInt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class LongMapDeserializer(valueType: JavaType,
jp.nextToken()
}

if (jp.getCurrentToken != JsonToken.FIELD_NAME &&
jp.getCurrentToken != JsonToken.END_OBJECT) {
throw ctxt.mappingException(valueType.getRawClass)
}

while (jp.getCurrentToken != JsonToken.END_OBJECT) {
try {
val name = jp.getCurrentName.toLong
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class MutableMapDeserializer(valueType: JavaType,
jp.nextToken()
}

if (jp.getCurrentToken != JsonToken.FIELD_NAME &&
jp.getCurrentToken != JsonToken.END_OBJECT) {
throw ctxt.mappingException(valueType.getRawClass)
}

while (jp.getCurrentToken != JsonToken.END_OBJECT) {
val name = jp.getCurrentName
jp.nextToken()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ class ImmutableCollectionSupportSpec extends Spec {
}
}

class `An immutable.HashMap[String, Any]` {
def `generates a JSON object` = {
generate(HashMap[String, Any]("one" -> 1)) must beEqualTo("""{"one":1}""")
}

def `is parsable from a JSON object with int field values` = {
parse[HashMap[String, Any]]("""{"one":1}""") must beEqualTo(HashMap("one" -> 1))
}

def `is parsable from an empty JSON object` = {
parse[HashMap[String, Any]]("{}") must beEqualTo(HashMap.empty)
}

def `is not parsable from an empty JSON object in a JSON array` = {
parse[HashMap[String, Any]]("[{}]") must throwA[ParsingException]
}
}

class `An immutable.Map[Int, String]` {
def `generates a JSON object` = {
generate(Map(1 -> "one")) must beEqualTo("""{"1":"one"}""")
Expand All @@ -158,6 +176,30 @@ class ImmutableCollectionSupportSpec extends Spec {
}
}

class `An immutable.Map[Int, Any]` {
def `is not parsable from an empty JSON object in a JSON array` = {
parse[Map[Int, Any]]("[{}]") must throwA[ParsingException]
}
}

class `An immutable.IntMap[Any]` {
def `is not parsable from an empty JSON object in a JSON array` = {
parse[IntMap[Any]]("[{}]") must throwA[ParsingException]
}
}

class `An immutable.LongMap[Any]` {
def `is not parsable from an empty JSON object in a JSON array` = {
parse[LongMap[Any]]("[{}]") must throwA[ParsingException]
}
}

class `An immutable.Map[Long, Any]` {
def `is not parsable from an empty JSON object in a JSON array` = {
parse[Map[Long, Any]]("[{}]") must throwA[ParsingException]
}
}

class `An immutable.Map[Long, String]` {
def `generates a JSON object` = {
generate(Map(1L -> "one")) must beEqualTo("""{"1":"one"}""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.codahale.jerkson.tests
import com.codahale.simplespec.Spec
import com.codahale.jerkson.Json._
import scala.collection.mutable._
import com.codahale.jerkson.ParsingException

class MutableCollectionSupportSpec extends Spec {
class `A mutable.ResizableArray[Int]` {
Expand Down Expand Up @@ -149,6 +150,12 @@ class MutableCollectionSupportSpec extends Spec {
}
}

class `A mutable.Map[String, Any]` {
def `is not parsable from an empty JSON object in a JSON array` = {
parse[Map[String, Any]]("[{}]") must throwA[ParsingException]
}
}

class `A mutable.HashMap[String, Int]` {
def `generates a JSON object` = {
generate(HashMap("one" -> 1)) must beEqualTo("""{"one":1}""")
Expand Down

0 comments on commit efb35cb

Please sign in to comment.