Skip to content

Commit

Permalink
support NaN and Infinity
Browse files Browse the repository at this point in the history
  • Loading branch information
junyulah committed Jan 22, 2019
1 parent aa8ae65 commit c4c64ca
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ Boolean | bool
None | null
Null | null
Unit | null
NaN | null
Infinity | null
AbstractSeq[_] | array
scala.collection.mutable.AbstractSeq[_] | array
Array[_] | array
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "sjson"
organization := "io.github.idata-shopee"
version := "0.1.4"
version := "0.1.5"
scalaVersion := "2.12.4"

useGpg := true
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/io/github/shopee/idata/sjson/Converter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import scala.reflect._
import scala.reflect.runtime.universe._

/**
* convert a plain scala value (string, number, map, list, true, false, null) to a specific case class
*/
* convert a plain scala value (string, number, map, list, true, false, null) to a specific case class
*/
object JSONConverter {
def convert[T: TypeTag](plain: Any) =
convertHelp(typeOf[T], plain).asInstanceOf[T]
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/io/github/shopee/idata/sjson/Stringify.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ object Stringify {
case v: java.util.Date => JSONUtil.escapeString(v.toString())

// number
case v: Double => if (v.isNaN || v.isInfinity) "null" else v.toString()
case v: Float => if (v.isNaN || v.isInfinity) "null" else v.toString()
case v: Number => v.toString()

// boolean
case v: Boolean => if (v == true) "true" else "false"
// null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class stringifyTest extends org.scalatest.FunSuite {
val d: Double = 234.134
assert(JSON.stringify(d) == "234.134")
assert(JSON.stringify(123456l) == "123456")
assert(JSON.stringify(Double.NaN) == "null")
assert(JSON.stringify(Float.NaN) == "null")
assert(JSON.stringify(1.0 / 0.0) == "null")
}

test("stringify: boolean") {
Expand Down Expand Up @@ -46,7 +49,11 @@ class stringifyTest extends org.scalatest.FunSuite {

test("stringify: java.util.Date") {
assert(
JSON.stringify(new java.util.Date(1990 - 1900, 2, 12)) == s""""${new java.util.Date(1990 - 1900, 2, 12).toString()}""""
JSON.stringify(new java.util.Date(1990 - 1900, 2, 12)) == s""""${new java.util.Date(
1990 - 1900,
2,
12
).toString()}""""
)
}

Expand Down

0 comments on commit c4c64ca

Please sign in to comment.