scoop:scoop.json:0.1.0
src/decoding_containers.scoop
Selected production source · src/decoding_containers.scoop
1
package scoop.json3
import scoop.collections.Map4
import scoop.collections.toList6
internal class JsonKeyedDecoder(private val owner: JsonDecoder, private val members: Map<String, JsonNode>)7
: KeyedDecodingContainer {8
public override val keys: List<String> get() = members.keys.toList()10
public override fun optional(name: String): Option<Decoder> =11
when (val node = members[name]) {12
case Some(val value) -> Some(JsonDecoder(value, childPath(owner.path, name)))13
case None -> None14
}16
public override fun required(name: String): Decoder {17
val result = optional(name)18
return when (result) {19
case Some(val child) -> child20
case None -> { throw DecodingException(childPath(owner.path, name), "missing required field") }21
}22
}24
public override fun end(): Unit = Unit25
}27
internal class JsonUnkeyedDecoder(private val owner: JsonDecoder, private val elements: ArrayList<JsonNode>)28
: UnkeyedDecodingContainer {29
private var cursor: Long = 0L30
public override val hasNext: Boolean get() = cursor < elements.size32
public override fun element(): Decoder {33
val path = childPath(owner.path, cursor.toString())34
if (cursor == elements.size) { throw DecodingException(path, "missing array element") }35
val child = JsonDecoder(elements[cursor], path)36
cursor = cursor + 1L37
return child38
}40
public override fun end() {41
if (cursor != elements.size) { throw DecodingException(owner.path, "extra array elements") }42
}43
}