Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Basic implementation of JNumber conversions #42

Merged
merged 13 commits into from
Dec 11, 2017
18 changes: 10 additions & 8 deletions js/src/main/scala-2.10/scalajson.ast/JValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,6 @@ object JNumber {
// Due to a restriction in Scala 2.10, we cant override/replace the default apply method
// generated by the compiler even when the constructor itself is marked private
final class JNumber private[ast] (val value: String) extends JValue {

/**
* Javascript specification for numbers specify a [[scala.Double]], so this is the default export method to `Javascript`
*
* @param value
*/
def this(value: Double) = this(value.toString)

override def toUnsafe: unsafe.JValue = unsafe.JNumber(value)

override def toJsAny: js.Any = value.toDouble match {
Expand Down Expand Up @@ -146,6 +138,16 @@ final class JNumber private[ast] (val value: String) extends JValue {
case jNumberRegex(_ *) => new JNumber(value)
case _ => throw new NumberFormatException(value)
}

def toInt: Option[Int] = scalajson.ast.toInt(value)

def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)

def toLong: Option[Long] = scalajson.ast.toLong(value)

def toDouble: Option[Double] = scalajson.ast.toDouble(value)

def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
}

/** Represents a JSON Boolean value, which can either be a
Expand Down
14 changes: 10 additions & 4 deletions js/src/main/scala-2.10/scalajson.ast/unsafe/JValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,21 @@ final case class JNumber(value: String) extends JValue {
case _ => throw new NumberFormatException(value)
}

def this(value: Double) = {
this(value.toString)
}

override def toJsAny: js.Any = value.toDouble match {
case n if n.isNaN => null
case n if n.isInfinity => null
case n => n
}

def toInt: Option[Int] = scalajson.ast.toInt(value)

def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)

def toLong: Option[Long] = scalajson.ast.toLong(value)

def toDouble: Option[Double] = scalajson.ast.toDouble(value)

def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
}

/** Represents a JSON Boolean value, which can either be a
Expand Down
17 changes: 10 additions & 7 deletions js/src/main/scala/scalajson/ast/JValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,6 @@ object JNumber {
*/
final case class JNumber private[ast] (value: String) extends JValue {

/**
* Javascript specification for numbers specify a [[scala.Double]], so this is the default export method to `Javascript`
*
* @param value
*/
def this(value: Double) = this(value.toString)

override def toUnsafe: unsafe.JValue = unsafe.JNumber(value)

override def toJsAny: js.Any = value.toDouble match {
Expand All @@ -128,6 +121,16 @@ final case class JNumber private[ast] (value: String) extends JValue {
case jNumberRegex(_ *) => new JNumber(value)
case _ => throw new NumberFormatException(value)
}

def toInt: Option[Int] = scalajson.ast.toInt(value)

def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)

def toLong: Option[Long] = scalajson.ast.toLong(value)

def toDouble: Option[Double] = scalajson.ast.toDouble(value)

def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
}

/** Represents a JSON Boolean value, which can either be a
Expand Down
14 changes: 10 additions & 4 deletions js/src/main/scala/scalajson/ast/unsafe/JValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,21 @@ final case class JNumber(value: String) extends JValue {
case _ => throw new NumberFormatException(value)
}

def this(value: Double) = {
this(value.toString)
}

override def toJsAny: js.Any = value.toDouble match {
case n if n.isNaN => null
case n if n.isInfinity => null
case n => n
}

def toInt: Option[Int] = scalajson.ast.toInt(value)

def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)

def toLong: Option[Long] = scalajson.ast.toLong(value)

def toDouble: Option[Double] = scalajson.ast.toDouble(value)

def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
}

/** Represents a JSON Boolean value, which can either be a
Expand Down
10 changes: 10 additions & 0 deletions jvm/src/main/scala-2.10/scalajson.ast/JValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ final class JNumber private[ast] (val value: String) extends JValue {
case jNumberRegex(_ *) => new JNumber(value)
case _ => throw new NumberFormatException(value)
}

def toInt: Option[Int] = scalajson.ast.toInt(value)

def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)

def toLong: Option[Long] = scalajson.ast.toLong(value)

def toDouble: Option[Double] = scalajson.ast.toDouble(value)

def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
}

/** Represents a JSON Boolean value, which can either be a
Expand Down
10 changes: 10 additions & 0 deletions jvm/src/main/scala-2.10/scalajson.ast/unsafe/JValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ final case class JNumber(value: String) extends JValue {
case jNumberRegex(_ *) => new ast.JNumber(value)
case _ => throw new NumberFormatException(value)
}

def toInt: Option[Int] = scalajson.ast.toInt(value)

def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)

def toLong: Option[Long] = scalajson.ast.toLong(value)

def toDouble: Option[Double] = scalajson.ast.toDouble(value)

def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
}

/** Represents a JSON Boolean value, which can either be a
Expand Down
10 changes: 10 additions & 0 deletions jvm/src/main/scala/scalajson/ast/JValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ final case class JNumber private[ast] (value: String) extends JValue {
case jNumberRegex(_ *) => new JNumber(value)
case _ => throw new NumberFormatException(value)
}

def toInt: Option[Int] = scalajson.ast.toInt(value)

def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)

def toLong: Option[Long] = scalajson.ast.toLong(value)

def toDouble: Option[Double] = scalajson.ast.toDouble(value)

def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
}

/** Represents a JSON Boolean value, which can either be a
Expand Down
10 changes: 10 additions & 0 deletions jvm/src/main/scala/scalajson/ast/unsafe/JValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ final case class JNumber(value: String) extends JValue {
case jNumberRegex(_ *) => new ast.JNumber(value)
case _ => throw new NumberFormatException(value)
}

def toInt: Option[Int] = scalajson.ast.toInt(value)

def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)

def toLong: Option[Long] = scalajson.ast.toLong(value)

def toDouble: Option[Double] = scalajson.ast.toDouble(value)

def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
}

/** Represents a JSON Boolean value, which can either be a
Expand Down
Loading