Skip to content

Commit d4e77b0

Browse files
committed
Review suggestions, code deduplication
1 parent 0c8ad07 commit d4e77b0

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

core/js/src/main/scala/com/avsystem/commons/serialization/nativejs/NativeJsonInput.scala

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import scala.scalajs.js.JSON
1111

1212
class NativeJsonInput(value: js.Any, options: NativeFormatOptions) extends InputAndSimpleInput { self =>
1313
private def read[T](expected: String)(matcher: PartialFunction[Any, T]): T =
14-
matcher.applyOrElse(value, (_: Any) => throw new ReadFailure(s"$expected expected."))
14+
matcher.applyOrElse(value, (o: Any) => throw new ReadFailure(s"Cannot read $expected, got: ${js.typeOf(o)}"))
1515

1616
override def readNull(): Boolean =
1717
value == null
@@ -37,13 +37,13 @@ class NativeJsonInput(value: js.Any, options: NativeFormatOptions) extends Input
3737
catch {
3838
case e: NumberFormatException => throw new ReadFailure(s"Cannot read Long", e)
3939
}
40-
(value: Any) match {
40+
read("Long") {
4141
case s: String => fromString(s)
4242
case i: Int => i
4343
case d: Double if d.isWhole => d.toLong
4444
case b: js.BigInt => fromString(b.toString)
45+
// for some reason pattern match on js.BigInt type does not seem to work, check type manually
4546
case b if js.typeOf(b) == "bigint" => fromString(b.asInstanceOf[js.BigInt].toString)
46-
case o => throw new ReadFailure(s"Cannot read Long, got: ${js.typeOf(o)}")
4747
}
4848
}
4949

@@ -54,13 +54,13 @@ class NativeJsonInput(value: js.Any, options: NativeFormatOptions) extends Input
5454
case e: NumberFormatException => throw new ReadFailure(s"Cannot read BitInt", e)
5555
}
5656

57-
(value: Any) match {
57+
read("BitInt") {
5858
case s: String => fromString(s)
5959
case i: Int => BigInt(i)
6060
case d: Double if d.isWhole => BigInt(d.toLong)
6161
case b: js.BigInt => fromString(b.toString)
62+
// for some reason pattern match on js.BigInt type does not seem to work, check type manually
6263
case b if js.typeOf(b) == "bigint" => fromString(b.asInstanceOf[js.BigInt].toString)
63-
case o => throw new ReadFailure(s"Cannot read BitInt, got: ${js.typeOf(o)}")
6464
}
6565
}
6666

@@ -70,11 +70,10 @@ class NativeJsonInput(value: js.Any, options: NativeFormatOptions) extends Input
7070
catch {
7171
case e: NumberFormatException => throw new ReadFailure(s"Cannot read BigDecimal", e)
7272
}
73-
(value: Any) match {
73+
read("BitInt") {
7474
case s: String => fromString(s)
7575
case i: Int => BigDecimal(i)
7676
case d: Double => BigDecimal(d)
77-
case o => throw new ReadFailure(s"Cannot read BigDecimal, got: ${js.typeOf(o)}")
7877
}
7978
}
8079

@@ -85,12 +84,12 @@ class NativeJsonInput(value: js.Any, options: NativeFormatOptions) extends Input
8584

8685
override def readList(): ListInput =
8786
read("List") {
88-
case array: js.Array[js.Any @unchecked] => new JsonListInput(array, options)
87+
case array: js.Array[js.Any @unchecked] => new NativeJsonListInput(array, options)
8988
}
9089

9190
override def readObject(): ObjectInput =
9291
read("Object") {
93-
case obj: js.Object => new JsonObjectInput(obj.asInstanceOf[js.Dictionary[js.Any]], options)
92+
case obj: js.Object => new NativeJsonObjectInput(obj.asInstanceOf[js.Dictionary[js.Any]], options)
9493
}
9594

9695
override def readTimestamp(): Long = options.dateFormat match {
@@ -105,7 +104,7 @@ class NativeJsonInput(value: js.Any, options: NativeFormatOptions) extends Input
105104
override def skip(): Unit = ()
106105

107106
override def readBinary(): Array[Byte] =
108-
read("List") {
107+
read("Binary") {
109108
case array: js.Array[Int @unchecked] => array.iterator.map(_.toByte).toArray
110109
}
111110

@@ -118,21 +117,21 @@ class NativeJsonInput(value: js.Any, options: NativeFormatOptions) extends Input
118117
def readRaw(): js.Any = value
119118
}
120119

121-
final class JsonListInput(list: js.Array[js.Any], options: NativeFormatOptions) extends ListInput {
122-
var it = 0
120+
final class NativeJsonListInput(array: js.Array[js.Any], options: NativeFormatOptions) extends ListInput {
121+
private var it = 0
123122

124123
override def hasNext: Boolean =
125-
it < list.length
124+
it < array.length
126125

127126
override def nextElement(): Input = {
128-
val in = new NativeJsonInput(list(it), options)
127+
val in = new NativeJsonInput(array(it), options)
129128
it += 1
130129
in
131130
}
132131
}
133132

134-
final class JsonObjectInput(dict: js.Dictionary[js.Any], options: NativeFormatOptions) extends ObjectInput {
135-
val it = dict.iterator
133+
final class NativeJsonObjectInput(dict: js.Dictionary[js.Any], options: NativeFormatOptions) extends ObjectInput {
134+
private val it = dict.iterator
136135

137136
override def hasNext: Boolean =
138137
it.hasNext

0 commit comments

Comments
 (0)