Skip to content

Commit fcaa8d1

Browse files
committed
#37 Basic implementation of JNumber conversions
* Also removed JNumber double constructor for Scala.js
1 parent 403d9b3 commit fcaa8d1

File tree

13 files changed

+689
-955
lines changed

13 files changed

+689
-955
lines changed

benchmark/jvm/src/test/scala/scalajson/ast/JNumberConversionBenchmark.scala

Lines changed: 0 additions & 89 deletions
This file was deleted.

benchmark/jvm/src/test/scala/scalajson/ast/EqualsHashcodeBenchmark.scala renamed to benchmark/jvm/src/test/scala/scalajson/ast/PrivateBenchmark.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package scalajson.ast
33
import benchmark.Generators
44
import org.scalameter.Bench
55

6-
object EqualsHashcodeBenchmark extends Bench.ForkedTime {
6+
/**
7+
* Created by matthewdedetrich on 17/10/16.
8+
*/
9+
object PrivateBenchmark extends Bench.ForkedTime {
710

811
performance of "privateMethods" in {
912
measure method "hashcode" in {

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

Lines changed: 15 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,15 @@ final case class JString(value: String) extends JValue {
5050
}
5151

5252
object JNumber {
53-
def apply(value: Int): JNumber =
54-
new JNumber(value.toString)(NumberFlags.intConstructed)
53+
def apply(value: Int): JNumber = new JNumber(value.toString)
5554

56-
def apply(value: Integer): JNumber =
57-
new JNumber(value.toString)(NumberFlags.intConstructed)
55+
def apply(value: Integer): JNumber = new JNumber(value.toString)
5856

59-
def apply(value: Long): JNumber =
60-
new JNumber(value.toString)(NumberFlags.longConstructed)
57+
def apply(value: Long): JNumber = new JNumber(value.toString)
6158

62-
def apply(value: BigInt): JNumber =
63-
new JNumber(value.toString())(NumberFlags.bigIntConstructed)
59+
def apply(value: BigInt): JNumber = new JNumber(value.toString())
6460

65-
def apply(value: BigDecimal): JNumber =
66-
new JNumber(value.toString())(NumberFlags.bigDecimalConstructed)
61+
def apply(value: BigDecimal): JNumber = new JNumber(value.toString())
6762

6863
/**
6964
* @param value
@@ -72,7 +67,7 @@ object JNumber {
7267
def apply(value: Double): JValue = value match {
7368
case n if n.isNaN => JNull
7469
case n if n.isInfinity => JNull
75-
case _ => new JNumber(value.toString)(NumberFlags.doubleConstructed)
70+
case _ => new JNumber(value.toString)
7671
}
7772

7873
/**
@@ -82,12 +77,12 @@ object JNumber {
8277
def apply(value: Float): JValue = value match {
8378
case n if java.lang.Float.isNaN(n) => JNull
8479
case n if n.isInfinity => JNull
85-
case _ => new JNumber(value.toString)(NumberFlags.floatConstructed)
80+
case _ => new JNumber(value.toString)
8681
}
8782

8883
def fromString(value: String): Option[JNumber] =
8984
value match {
90-
case jNumberRegex(_ *) => Some(new JNumber(value)(0))
85+
case jNumberRegex(_ *) => Some(new JNumber(value))
9186
case _ => None
9287
}
9388

@@ -102,9 +97,7 @@ object JNumber {
10297
*/
10398
// Due to a restriction in Scala 2.10, we cant override/replace the default apply method
10499
// generated by the compiler even when the constructor itself is marked private
105-
final class JNumber private[ast] (val value: String)(
106-
private[ast] val constructedFlag: Int)
107-
extends JValue {
100+
final class JNumber private[ast] (val value: String) extends JValue {
108101
override def toUnsafe: unsafe.JValue = unsafe.JNumber(value)
109102

110103
override def toJsAny: js.Any = value.toDouble match {
@@ -142,85 +135,19 @@ final class JNumber private[ast] (val value: String)(
142135

143136
def copy(value: String): JNumber =
144137
value match {
145-
case jNumberRegex(_ *) => new JNumber(value)(0)
138+
case jNumberRegex(_ *) => new JNumber(value)
146139
case _ => throw new NumberFormatException(value)
147140
}
148141

149-
def toInt: Option[Long] = {
150-
if ((constructedFlag & NumberFlags.int) == NumberFlags.int)
151-
Some(value.toInt)
152-
else {
153-
try {
154-
val asInt = value.toInt
155-
if (BigInt(value) == BigInt(asInt))
156-
Some(asInt)
157-
else
158-
None
159-
} catch {
160-
case _: NumberFormatException => None
161-
}
162-
}
163-
}
142+
def toInt: Option[Int] = scalajson.ast.toInt(value)
164143

165-
def toLong: Option[Long] = {
166-
if ((constructedFlag & NumberFlags.long) == NumberFlags.long)
167-
Some(value.toLong)
168-
else {
169-
try {
170-
val asLong = value.toLong
171-
if (BigInt(value) == BigInt(asLong))
172-
Some(asLong)
173-
else
174-
None
175-
} catch {
176-
case _: NumberFormatException => None
177-
}
178-
}
179-
}
144+
def toBigInt: Option[BigInt] = scalajson.ast.toBigInt(value)
180145

181-
def toBigInt: Option[BigInt] = {
182-
if ((constructedFlag & NumberFlags.bigInt) == NumberFlags.bigInt)
183-
Some(BigInt(value))
184-
else {
185-
try {
186-
Some(BigInt(value))
187-
} catch {
188-
case _: NumberFormatException => None
189-
}
190-
}
191-
}
192-
193-
def toBigDecimal: Option[BigDecimal] = {
194-
try {
195-
Some(BigDecimal(value))
196-
} catch {
197-
case _: NumberFormatException => None
198-
}
199-
}
146+
def toLong: Option[Long] = scalajson.ast.toLong(value)
200147

201-
def toFloat: Option[Float] = {
202-
if ((constructedFlag & NumberFlags.float) == NumberFlags.float)
203-
Some(value.toFloat)
204-
else {
205-
val asFloat = value.toFloat
206-
if (BigDecimal(value) == BigDecimal(asFloat.toDouble))
207-
Some(asFloat)
208-
else
209-
None
210-
}
211-
}
148+
def toDouble: Option[Double] = scalajson.ast.toDouble(value)
212149

213-
def toDouble: Option[Double] = {
214-
if ((constructedFlag & NumberFlags.double) == NumberFlags.double)
215-
Some(value.toDouble)
216-
else {
217-
val asDouble = value.toDouble
218-
if (BigDecimal(value) == BigDecimal(asDouble))
219-
Some(asDouble)
220-
else
221-
None
222-
}
223-
}
150+
def toBigDecimal: Option[BigDecimal] = scalajson.ast.toBigDecimal(value)
224151
}
225152

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

0 commit comments

Comments
 (0)