Skip to content

Commit 31020c7

Browse files
committed
#37 Basic implementation of JNumber conversions
1 parent 99ee084 commit 31020c7

File tree

10 files changed

+636
-7
lines changed

10 files changed

+636
-7
lines changed

js/src/main/scala-2.10/scalajson.ast/JValue.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ final class JNumber private[ast] (val value: String) extends JValue {
146146
case jNumberRegex(_ *) => new JNumber(value)
147147
case _ => throw new NumberFormatException(value)
148148
}
149+
150+
def toInt: Option[Int] = scalajson.ast.toInt(value)
151+
152+
def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)
153+
154+
def toLong: Option[Long] = scalajson.ast.toLong(value)
155+
156+
def toDouble: Option[Double] = scalajson.ast.toDouble(value)
157+
158+
def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
149159
}
150160

151161
/** Represents a JSON Boolean value, which can either be a

js/src/main/scala-2.10/scalajson.ast/unsafe/JValue.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ final case class JNumber(value: String) extends JValue {
100100
case n if n.isInfinity => null
101101
case n => n
102102
}
103+
104+
def toInt: Option[Int] = scalajson.ast.toInt(value)
105+
106+
def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)
107+
108+
def toLong: Option[Long] = scalajson.ast.toLong(value)
109+
110+
def toDouble: Option[Double] = scalajson.ast.toDouble(value)
111+
112+
def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
103113
}
104114

105115
/** Represents a JSON Boolean value, which can either be a

js/src/main/scala/scalajson/ast/JValue.scala

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,6 @@ object JNumber {
9898
*/
9999
final case class JNumber private[ast] (value: String) extends JValue {
100100

101-
/**
102-
* Javascript specification for numbers specify a [[scala.Double]], so this is the default export method to `Javascript`
103-
*
104-
* @param value
105-
*/
106-
def this(value: Double) = this(value.toString)
107-
108101
override def toUnsafe: unsafe.JValue = unsafe.JNumber(value)
109102

110103
override def toJsAny: js.Any = value.toDouble match {
@@ -128,6 +121,16 @@ final case class JNumber private[ast] (value: String) extends JValue {
128121
case jNumberRegex(_ *) => new JNumber(value)
129122
case _ => throw new NumberFormatException(value)
130123
}
124+
125+
def toInt: Option[Int] = scalajson.ast.toInt(value)
126+
127+
def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)
128+
129+
def toLong: Option[Long] = scalajson.ast.toLong(value)
130+
131+
def toDouble: Option[Double] = scalajson.ast.toDouble(value)
132+
133+
def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
131134
}
132135

133136
/** Represents a JSON Boolean value, which can either be a

js/src/main/scala/scalajson/ast/unsafe/JValue.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ final case class JNumber(value: String) extends JValue {
9898
case n if n.isInfinity => null
9999
case n => n
100100
}
101+
102+
def toInt: Option[Int] = scalajson.ast.toInt(value)
103+
104+
def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)
105+
106+
def toLong: Option[Long] = scalajson.ast.toLong(value)
107+
108+
def toDouble: Option[Double] = scalajson.ast.toDouble(value)
109+
110+
def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
101111
}
102112

103113
/** Represents a JSON Boolean value, which can either be a

jvm/src/main/scala-2.10/scalajson.ast/JValue.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ final class JNumber private[ast] (val value: String) extends JValue {
125125
case jNumberRegex(_ *) => new JNumber(value)
126126
case _ => throw new NumberFormatException(value)
127127
}
128+
129+
def toInt: Option[Int] = scalajson.ast.toInt(value)
130+
131+
def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)
132+
133+
def toLong: Option[Long] = scalajson.ast.toLong(value)
134+
135+
def toDouble: Option[Double] = scalajson.ast.toDouble(value)
136+
137+
def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
128138
}
129139

130140
/** Represents a JSON Boolean value, which can either be a

jvm/src/main/scala-2.10/scalajson.ast/unsafe/JValue.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ final case class JNumber(value: String) extends JValue {
7676
case jNumberRegex(_ *) => new ast.JNumber(value)
7777
case _ => throw new NumberFormatException(value)
7878
}
79+
80+
def toInt: Option[Int] = scalajson.ast.toInt(value)
81+
82+
def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)
83+
84+
def toLong: Option[Long] = scalajson.ast.toLong(value)
85+
86+
def toDouble: Option[Double] = scalajson.ast.toDouble(value)
87+
88+
def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
7989
}
8090

8191
/** Represents a JSON Boolean value, which can either be a

jvm/src/main/scala/scalajson/ast/JValue.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ final case class JNumber private[ast] (value: String) extends JValue {
106106
case jNumberRegex(_ *) => new JNumber(value)
107107
case _ => throw new NumberFormatException(value)
108108
}
109+
110+
def toInt: Option[Int] = scalajson.ast.toInt(value)
111+
112+
def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)
113+
114+
def toLong: Option[Long] = scalajson.ast.toLong(value)
115+
116+
def toDouble: Option[Double] = scalajson.ast.toDouble(value)
117+
118+
def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
109119
}
110120

111121
/** Represents a JSON Boolean value, which can either be a

jvm/src/main/scala/scalajson/ast/unsafe/JValue.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ final case class JNumber(value: String) extends JValue {
7676
case jNumberRegex(_ *) => new ast.JNumber(value)
7777
case _ => throw new NumberFormatException(value)
7878
}
79+
80+
def toInt: Option[Int] = scalajson.ast.toInt(value)
81+
82+
def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)
83+
84+
def toLong: Option[Long] = scalajson.ast.toLong(value)
85+
86+
def toDouble: Option[Double] = scalajson.ast.toDouble(value)
87+
88+
def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
7989
}
8090

8191
/** Represents a JSON Boolean value, which can either be a

0 commit comments

Comments
 (0)