|
| 1 | +package com.avsystem.commons |
| 2 | +package serialization.nativejs |
| 3 | + |
| 4 | +import com.avsystem.commons.annotation.explicitGenerics |
| 5 | +import com.avsystem.commons.serialization.GenCodec.ReadFailure |
| 6 | +import com.avsystem.commons.serialization.* |
| 7 | +import com.avsystem.commons.serialization.json.RawJson |
| 8 | + |
| 9 | +import scala.scalajs.js |
| 10 | +import scala.scalajs.js.JSON |
| 11 | + |
| 12 | +class NativeJsonInput(value: js.Any, options: NativeFormatOptions) extends InputAndSimpleInput { self => |
| 13 | + private def read[T](expected: String)(matcher: PartialFunction[Any, T]): T = |
| 14 | + matcher.applyOrElse(value, (o: Any) => throw new ReadFailure(s"Cannot read $expected, got: ${js.typeOf(o)}")) |
| 15 | + |
| 16 | + override def readNull(): Boolean = |
| 17 | + value == null |
| 18 | + |
| 19 | + override def readString(): String = |
| 20 | + read("String") { |
| 21 | + case s: String => s |
| 22 | + } |
| 23 | + |
| 24 | + override def readDouble(): Double = |
| 25 | + read("Double") { |
| 26 | + case v: Double => v |
| 27 | + } |
| 28 | + |
| 29 | + override def readInt(): Int = |
| 30 | + read("Int") { |
| 31 | + case v: Int => v |
| 32 | + } |
| 33 | + |
| 34 | + override def readLong(): Long = { |
| 35 | + def fromString(s: String): Long = |
| 36 | + try s.toLong |
| 37 | + catch { |
| 38 | + case e: NumberFormatException => throw new ReadFailure(s"Cannot read Long", e) |
| 39 | + } |
| 40 | + read("Long") { |
| 41 | + case s: String => fromString(s) |
| 42 | + case i: Int => i |
| 43 | + case d: Double if d.isWhole => d.toLong |
| 44 | + 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 |
| 46 | + case b if js.typeOf(b) == "bigint" => fromString(b.asInstanceOf[js.BigInt].toString) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + override def readBigInt(): BigInt = { |
| 51 | + def fromString(s: String): BigInt = |
| 52 | + try BigInt(s) |
| 53 | + catch { |
| 54 | + case e: NumberFormatException => throw new ReadFailure(s"Cannot read BigInt", e) |
| 55 | + } |
| 56 | + |
| 57 | + read("BigInt") { |
| 58 | + case s: String => fromString(s) |
| 59 | + case i: Int => BigInt(i) |
| 60 | + case d: Double if d.isWhole => BigInt(d.toLong) |
| 61 | + 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 |
| 63 | + case b if js.typeOf(b) == "bigint" => fromString(b.asInstanceOf[js.BigInt].toString) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + override def readBigDecimal(): BigDecimal = { |
| 68 | + def fromString(s: String): BigDecimal = |
| 69 | + try BigDecimal(s) |
| 70 | + catch { |
| 71 | + case e: NumberFormatException => throw new ReadFailure(s"Cannot read BigDecimal", e) |
| 72 | + } |
| 73 | + read("BigDecimal") { |
| 74 | + case s: String => fromString(s) |
| 75 | + case i: Int => BigDecimal(i) |
| 76 | + case d: Double => BigDecimal(d) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + override def readBoolean(): Boolean = |
| 81 | + read("Boolean") { |
| 82 | + case v: Boolean => v |
| 83 | + } |
| 84 | + |
| 85 | + override def readList(): ListInput = |
| 86 | + read("List") { |
| 87 | + case array: js.Array[js.Any @unchecked] => new NativeJsonListInput(array, options) |
| 88 | + } |
| 89 | + |
| 90 | + override def readObject(): ObjectInput = |
| 91 | + read("Object") { |
| 92 | + case obj: js.Object => new NativeJsonObjectInput(obj.asInstanceOf[js.Dictionary[js.Any]], options) |
| 93 | + } |
| 94 | + |
| 95 | + override def readTimestamp(): Long = options.dateFormat match { |
| 96 | + case NativeDateFormat.RawString | NativeDateFormat.JsNumber => |
| 97 | + readLong() // lenient behaviour, accept any value that can be interpreted as Long |
| 98 | + case NativeDateFormat.JsDate => |
| 99 | + read("js.Date") { |
| 100 | + case v: js.Date => v.getTime().toLong |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + override def skip(): Unit = () |
| 105 | + |
| 106 | + override def readBinary(): Array[Byte] = |
| 107 | + read("Binary") { |
| 108 | + case array: js.Array[Int @unchecked] => array.iterator.map(_.toByte).toArray |
| 109 | + } |
| 110 | + |
| 111 | + override def readCustom[T](typeMarker: TypeMarker[T]): Opt[T] = |
| 112 | + typeMarker match { |
| 113 | + case RawJson => JSON.stringify(readRaw()).opt |
| 114 | + case _ => Opt.Empty |
| 115 | + } |
| 116 | + |
| 117 | + def readRaw(): js.Any = value |
| 118 | +} |
| 119 | + |
| 120 | +final class NativeJsonListInput(array: js.Array[js.Any], options: NativeFormatOptions) extends ListInput { |
| 121 | + private var it = 0 |
| 122 | + |
| 123 | + override def hasNext: Boolean = |
| 124 | + it < array.length |
| 125 | + |
| 126 | + override def nextElement(): Input = { |
| 127 | + val in = new NativeJsonInput(array(it), options) |
| 128 | + it += 1 |
| 129 | + in |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +final class NativeJsonObjectInput(dict: js.Dictionary[js.Any], options: NativeFormatOptions) extends ObjectInput { |
| 134 | + private val it = dict.iterator |
| 135 | + |
| 136 | + override def hasNext: Boolean = |
| 137 | + it.hasNext |
| 138 | + |
| 139 | + override def peekField(name: String): Opt[FieldInput] = |
| 140 | + if (dict.contains(name)) Opt(new NativeJsonFieldInput(name, dict(name), options)) else Opt.Empty |
| 141 | + |
| 142 | + override def nextField(): FieldInput = { |
| 143 | + val (key, value) = it.next() |
| 144 | + new NativeJsonFieldInput(key, value, options) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +final class NativeJsonFieldInput( |
| 149 | + val fieldName: String, |
| 150 | + value: js.Any, |
| 151 | + options: NativeFormatOptions, |
| 152 | +) extends NativeJsonInput(value, options) |
| 153 | + with FieldInput |
| 154 | + |
| 155 | +object NativeJsonInput { |
| 156 | + @explicitGenerics |
| 157 | + def read[T: GenCodec](value: js.Any, options: NativeFormatOptions = NativeFormatOptions.RawString): T = |
| 158 | + GenCodec.read[T](new NativeJsonInput(value, options)) |
| 159 | + |
| 160 | + @explicitGenerics |
| 161 | + def readString[T: GenCodec](value: String, options: NativeFormatOptions = NativeFormatOptions.RawString): T = |
| 162 | + read[T](JSON.parse(value), options) |
| 163 | +} |
0 commit comments