Scoop API

scoop:scoop.json:0.1.0

src/decoder.scoop

Selected production source · src/decoder.scoop

1package scoop.json
3internal class JsonDecoder(internal val value: JsonNode, public override val path: String)
4 : Decoder, SingleValueDecodingContainer {
5 public override fun keyed(): KeyedDecodingContainer {
6 return when (value) {
7 case JsonNode.Record(val members) -> JsonKeyedDecoder(this, members)
8 else -> { throw DecodingException(path, "expected an object") }
9 }
10 }
12 public override fun unkeyed(): UnkeyedDecodingContainer {
13 return when (value) {
14 case JsonNode.Sequence(val elements) -> JsonUnkeyedDecoder(this, elements)
15 else -> { throw DecodingException(path, "expected an array") }
16 }
17 }
19 public override fun singleValue(): SingleValueDecodingContainer = this
21 public override fun isNull(): Boolean {
22 return when (value) {
23 case JsonNode.Null -> true
24 else -> false
25 }
26 }
28 public override fun readBoolean(): Boolean {
29 return when (value) {
30 case JsonNode.BooleanValue(val result) -> result
31 else -> { throw DecodingException(path, "expected a Boolean") }
32 }
33 }
35 public override fun readString(): String {
36 return when (value) {
37 case JsonNode.Text(val result) -> result
38 else -> { throw DecodingException(path, "expected a String") }
39 }
40 }
42 public override fun readNull() {
43 when (value) {
44 case JsonNode.Null -> { return }
45 else -> { throw DecodingException(path, "expected null") }
46 }
47 }
49 private fun number(expected: String): String {
50 return when (value) {
51 case JsonNode.Number(val text) -> text
52 else -> { throw DecodingException(path, expected) }
53 }
54 }
56 public override fun readLong(): Long {
57 val text = number("expected an integer")
58 val negative = text.getCharAt(0L) == '-'
59 val magnitude = integerMagnitude(text, negative, path)
60 if (negative) {
61 if (magnitude > 9223372036854775808UL) {
62 throw DecodingException(path, "integer is out of range")
63 }
64 return -magnitude.toLong()
65 }
66 if (magnitude > 9223372036854775807UL) {
67 throw DecodingException(path, "integer is out of range")
68 }
69 return magnitude.toLong()
70 }
72 public override fun readULong(): ULong {
73 val text = number("expected an integer")
74 if (text.getCharAt(0L) == '-') { throw DecodingException(path, "expected an unsigned integer") }
75 return integerMagnitude(text, false, path)
76 }
78 public override fun readFloat(): Float {
79 return when (parseFloat(number("expected a number"))) {
80 case Some(val result) -> result
81 case None -> { throw DecodingException(path, "Float is out of range") }
82 }
83 }
85 public override fun readDouble(): Double {
86 return when (parseDouble(number("expected a number"))) {
87 case Some(val result) -> result
88 case None -> { throw DecodingException(path, "Double is out of range") }
89 }
90 }
93internal fun integerMagnitude(text: String, negative: Boolean, path: String): ULong {
94 var result: ULong = 0UL
95 var first: Boolean = true
96 for (character in text) {
97 if (first && negative) {
98 first = false
99 continue
100 }
101 first = false
102 if (character < '0' || character > '9') {
103 throw DecodingException(path, "expected an integer without fraction or exponent")
104 }
105 val digit = (character.code - 48).toULong()
106 if (result > (18446744073709551615UL - digit) / 10UL) {
107 throw DecodingException(path, "integer is out of range")
108 }
109 result = result * 10UL + digit
110 }
111 return result