summaryrefslogtreecommitdiff
path: root/runtime/common/src/main/kotlin/kotlinx/serialization/json/JsonElementBuilders.kt
blob: 2f1f25983196e2da46ffad2f00d206eb6ddb3f3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright 2017-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

@file:Suppress("RedundantVisibilityModifier")

package kotlinx.serialization.json

/**
 * Builds [JsonObject] with given [init] builder.
 */
public fun json(init: JsonObjectBuilder.() -> Unit): JsonObject {
    val builder = JsonObjectBuilder()
    builder.init()
    return JsonObject(builder.content)
}

/**
 * Builds [JsonArray] with given [init] builder.
 */
public fun jsonArray(init: JsonArrayBuilder.() -> Unit): JsonArray {
    val builder = JsonArrayBuilder()
    builder.init()
    return JsonArray(builder.content)
}

/**
 * DSL builder for a [JsonArray].
 */
public class JsonArrayBuilder internal constructor() {

    internal val content: MutableList<JsonElement> = mutableListOf()

    /**
     * Adds [this] value to the current [JsonArray] as [JsonPrimitive].
     */
    public operator fun String?.unaryPlus() {
        content.add(JsonPrimitive(this))
    }

    /**
     * Adds [this] value to the current [JsonArray] as [JsonPrimitive].
     */
    public operator fun Number?.unaryPlus() {
        content.add(JsonPrimitive(this))
    }

    /**
     * Adds [this] value to the current [JsonArray] as [JsonPrimitive].
     */
    public operator fun Boolean?.unaryPlus() {
        content.add(JsonPrimitive(this))
    }

    /**
     * Adds [this] value to the current [JsonArray].
     */
    public operator fun JsonElement.unaryPlus() {
        this@JsonArrayBuilder.content.add(this)
    }
}

/**
 * DSL builder for a [JsonObject].
 */
public class JsonObjectBuilder internal constructor() {

    internal val content: MutableMap<String, JsonElement> = linkedMapOf()

    /**
     * Adds given [value] to the current [JsonObject] with [this] as a key.
     */
    public infix fun String.to(value: JsonElement) {
        require(content[this] == null) {"Key $this is already registered in builder"}
        content[this] = value
    }

    /**
     * Adds given [value] as [JsonPrimitive] to the current [JsonObject] with [this] as a key.
     */
    public infix fun String.to(value: Number?) {
        require(content[this] == null) {"Key $this is already registered in builder"}
        content[this] = JsonPrimitive(value)
    }

    /**
     * Adds given [value] as [JsonPrimitive] to the current [JsonObject] with [this] as a key.
     */
    public infix fun String.to(value: Boolean?) {
        require(content[this] == null) {"Key $this is already registered in builder"}
        content[this] = JsonPrimitive(value)
    }

    /**
     * Adds given [value] as [JsonPrimitive] to the current [JsonObject] with [this] as a key.
     */
    public infix fun String.to(value: String?) {
        require(content[this] == null) {"Key $this is already registered in builder"}
        content[this] = JsonPrimitive(value)
    }
}