Skip to content

Commit

Permalink
Merge pull request #2 from jonas/encode-nan-as-null
Browse files Browse the repository at this point in the history
Encode NaN as null
  • Loading branch information
OlegIlyenko authored Oct 14, 2016
2 parents 29a2073 + b4f96cd commit a68feee
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/main/scala/sangria/marshalling/circe.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ object circe {
case v: Boolean Json.fromBoolean(v)
case v: Int Json.fromInt(v)
case v: Long Json.fromLong(v)
case v: Float Json.fromDouble(v).get
case v: Double Json.fromDouble(v).get
case v: Float Json.fromDoubleOrNull(v)
case v: Double Json.fromDoubleOrNull(v)
case v: BigInt Json.fromBigInt(v)
case v: BigDecimal Json.fromBigDecimal(v)
case v throw new IllegalArgumentException("Unsupported scalar value: " + v)
Expand Down Expand Up @@ -55,17 +55,18 @@ object circe {
def getListValue(node: Json) = node.asArray.get

def isDefined(node: Json) = !node.isNull
def getScalarValue(node: Json) =
if (node.isBoolean)
node.asBoolean.get
else if (node.isNumber) {
val num = node.asNumber.get

(num.toBigInt orElse num.toBigDecimal).get
} else if (node.isString)
node.asString.get
else
throw new IllegalStateException(s"$node is not a scalar value")
def getScalarValue(node: Json) = {
def invalidScalar = throw new IllegalStateException(s"$node is not a scalar value")

node.fold(
jsonNull = invalidScalar,
jsonBoolean = identity,
jsonNumber = num num.toBigInt orElse num.toBigDecimal getOrElse invalidScalar,
jsonString = identity,
jsonArray = _ invalidScalar,
jsonObject = _ invalidScalar
)
}

def getScalaScalarValue(node: Json) = getScalarValue(node)

Expand Down Expand Up @@ -102,4 +103,4 @@ object circe {
case Left(error) throw InputParsingError(Vector(error.getMessage))
}
}
}
}

0 comments on commit a68feee

Please sign in to comment.