Scoop API

scoop:scoop.json:0.1.0

src/encoder.scoop

Selected production source · src/encoder.scoop

1package scoop.json
3internal enum JsonEncodingContainer {
4 Unselected,
5 Single,
6 Record(JsonKeyedEncoder),
7 Sequence(JsonUnkeyedEncoder)
8}
10internal class JsonEncoder(public override val path: String)
11 : Encoder, SingleValueEncodingContainer {
12 private var container: JsonEncodingContainer = JsonEncodingContainer.Unselected
13 private var output: Option<JsonNode> = None
14 private var committed: Boolean = false
16 internal fun checkWritable() {
17 if (committed) { throw EncodingException(path, "a value is already committed") }
18 }
20 internal fun begin() {
21 checkWritable()
22 output = None
23 }
25 internal fun finish(value: JsonNode) {
26 checkWritable()
27 output = Some(value)
28 }
30 internal fun result(): JsonNode {
31 return when (output) {
32 case Some(_) -> {
33 committed = true
34 output!!
35 }
36 case None -> { throw EncodingException(path, "a value is incomplete") }
37 }
38 }
40 public override fun keyed(): KeyedEncodingContainer {
41 return when (container) {
42 case JsonEncodingContainer.Record(val view) -> view
43 case JsonEncodingContainer.Unselected -> {
44 val view = JsonKeyedEncoder(this)
45 container = JsonEncodingContainer.Record(view)
46 view
47 }
48 else -> { throw EncodingException(path, "a different container is already selected") }
49 }
50 }
52 public override fun unkeyed(): UnkeyedEncodingContainer {
53 return when (container) {
54 case JsonEncodingContainer.Sequence(val view) -> view
55 case JsonEncodingContainer.Unselected -> {
56 val view = JsonUnkeyedEncoder(this)
57 container = JsonEncodingContainer.Sequence(view)
58 view
59 }
60 else -> { throw EncodingException(path, "a different container is already selected") }
61 }
62 }
64 public override fun singleValue(): SingleValueEncodingContainer {
65 when (container) {
66 case JsonEncodingContainer.Single -> { return this }
67 case JsonEncodingContainer.Unselected -> { container = JsonEncodingContainer.Single }
68 else -> { throw EncodingException(path, "a different container is already selected") }
69 }
70 return this
71 }
73 private fun write(value: JsonNode) {
74 checkWritable()
75 when (container) {
76 case JsonEncodingContainer.Single -> {
77 when (output) {
78 case Some(_) -> { throw EncodingException(path, "a value was written twice") }
79 case None -> { output = Some(value) }
80 }
81 }
82 else -> { throw EncodingException(path, "a single-value container was not selected") }
83 }
84 }
86 internal fun writeNumberToken(text: String): Unit = write(JsonNode.Number(text))
88 public override fun writeBoolean(value: Boolean): Unit = write(JsonNode.BooleanValue(value))
89 public override fun writeLong(value: Long): Unit = write(JsonNode.Number(value.toString()))
90 public override fun writeULong(value: ULong): Unit = write(JsonNode.Number(value.toString()))
91 public override fun writeFloat(value: Float) {
92 if (!value.isFinite()) { throw EncodingException(path, "expected a finite Float") }
93 write(JsonNode.Number(value.toString()))
94 }
95 public override fun writeDouble(value: Double) {
96 if (!value.isFinite()) { throw EncodingException(path, "expected a finite Double") }
97 write(JsonNode.Number(value.toString()))
98 }
99 public override fun writeString(value: String): Unit = write(JsonNode.Text(value))
100 public override fun writeNull(): Unit = write(JsonNode.Null)