Skip to content

Commit

Permalink
#37 Basic implementation of JNumber conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
mdedetrich committed Jul 26, 2017
1 parent 99ee084 commit 31020c7
Show file tree
Hide file tree
Showing 10 changed files with 636 additions and 7 deletions.
10 changes: 10 additions & 0 deletions js/src/main/scala-2.10/scalajson.ast/JValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,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 js/src/main/scala-2.10/scalajson.ast/unsafe/JValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ final case class JNumber(value: String) extends JValue {
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
10 changes: 10 additions & 0 deletions js/src/main/scala/scalajson/ast/unsafe/JValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ final case class JNumber(value: String) extends JValue {
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

0 comments on commit 31020c7

Please sign in to comment.