Skip to content

Commit

Permalink
Closes #149
Browse files Browse the repository at this point in the history
  • Loading branch information
christophercurrie committed Nov 23, 2014
1 parent 2db5a3f commit 3361de9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fasterxml.jackson.module.scala

import com.fasterxml.jackson.module.scala.deser.{ScalaStdValueInstantiatorsModule, UntypedObjectDeserializerModule}
import com.fasterxml.jackson.module.scala.deser.{ScalaNumberDeserializersModule, UntypedObjectDeserializerModule}
import com.fasterxml.jackson.module.scala.introspect.ScalaAnnotationIntrospectorModule
import com.fasterxml.jackson.module.scala.modifiers.EitherModule

Expand All @@ -26,7 +26,7 @@ class DefaultScalaModule
with TupleModule
with MapModule
with SetModule
with ScalaStdValueInstantiatorsModule
with ScalaNumberDeserializersModule
with ScalaAnnotationIntrospectorModule
with UntypedObjectDeserializerModule
with EitherModule
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.fasterxml.jackson
package module.scala
package deser

import com.fasterxml.jackson.core.JsonToken.{START_ARRAY, VALUE_NUMBER_FLOAT, VALUE_NUMBER_INT, VALUE_STRING}
import com.fasterxml.jackson.core.{JsonParser, JsonToken}
import com.fasterxml.jackson.databind.{JsonDeserializer, BeanDescription, DeserializationConfig, JavaType, DeserializationContext}
import com.fasterxml.jackson.databind.DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS
import com.fasterxml.jackson.databind.deser.Deserializers
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer

import scala.reflect.ClassTag
import scala.reflect.classTag

private abstract class BigNumberDeserializer[T >: Null : ClassTag](creator: (String) => T)
extends StdScalarDeserializer[T](classTag[T].runtimeClass)
{
override def deserialize(jp: JsonParser, ctxt: DeserializationContext): T = {
val t = jp.getCurrentToken
t match {
case VALUE_NUMBER_INT | VALUE_NUMBER_FLOAT => creator(jp.getText.trim)
case VALUE_STRING =>
val text = jp.getText.trim
if (text.isEmpty) null else try {
creator(text)
}
catch {
case e: IllegalArgumentException => throw ctxt.weirdStringException(text, _valueClass, "not a valid representation")
}
case START_ARRAY if ctxt.isEnabled(UNWRAP_SINGLE_VALUE_ARRAYS) =>
jp.nextToken()
val value = deserialize(jp, ctxt)
if (jp.nextToken() != JsonToken.END_ARRAY) {
throw ctxt.wrongTokenException(jp, JsonToken.END_ARRAY, "Attempted to unwrap array for single value but there was more than a single value in the array")
}
value
case _ =>
throw ctxt.mappingException(_valueClass, t)
}
}
}

private object BigDecimalDeserializer extends BigNumberDeserializer(BigDecimal.apply)

private object BigIntDeserializer extends BigNumberDeserializer(BigInt.apply)

private object NumberDeserializers extends Deserializers.Base
{
val BigDecimalClass = BigDecimalDeserializer.handledType()
val BigIntClass = BigIntDeserializer.handledType()

override def findBeanDeserializer(tpe: JavaType, config: DeserializationConfig, beanDesc: BeanDescription): JsonDeserializer[_] =
tpe.getRawClass match {
case BigDecimalClass => BigDecimalDeserializer
case BigIntClass => BigIntDeserializer
case _ => null
}
}

private [scala] trait ScalaNumberDeserializersModule extends JacksonModule {
this += NumberDeserializers
}

This file was deleted.

0 comments on commit 3361de9

Please sign in to comment.