Skip to content

Commit

Permalink
Add null to jsonBody
Browse files Browse the repository at this point in the history
  • Loading branch information
lhwdev committed May 5, 2022
1 parent 9db6d58 commit ff37e40
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions api-base/src/jvmMain/kotlin/com/lhwdev/fetch/jsonUtils.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.lhwdev.fetch

import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.*


@StructureBuilder
Expand All @@ -14,15 +11,23 @@ class JsonObjectScope {
infix fun String.set(value: Number) {
content[this] = JsonPrimitive(value)
}

infix fun String.set(value: String) {
content[this] = JsonPrimitive(value)
}

infix fun String.set(value: Boolean) {
content[this] = JsonPrimitive(value)
}

infix fun String.set(value: Nothing?) {
content[this] = JsonNull
}

inline infix fun String.jsonObject(block: JsonObjectScope.() -> Unit) {
content[this] = com.lhwdev.fetch.jsonObject(block)
}

infix fun String.jsonArray(block: JsonArrayScope.() -> Unit) {
content[this] = com.lhwdev.fetch.jsonArray(block)
}
Expand All @@ -38,15 +43,23 @@ class JsonArrayScope {
fun add(value: Number) {
content += JsonPrimitive(value)
}

fun add(value: String) {
content += JsonPrimitive(value)
}

fun add(value: Boolean) {
content += JsonPrimitive(value)
}

fun add(value: Nothing?) {
content += JsonNull
}

inline fun addJsonObject(block: JsonObjectScope.() -> Unit) {
content += jsonObject(block)
}

inline fun addJsonArray(block: JsonArrayScope.() -> Unit) {
content += jsonArray(block) as JsonElement
}
Expand Down
2 changes: 1 addition & 1 deletion api-base/src/jvmMain/kotlin/com/lhwdev/fetch/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ operator fun URL.get(childPath: String? = null) = URL(
this,
buildString {
append(path.removeSuffix("/"))
if(childPath != null) append("/$childPath")
if(childPath != null) append("/${childPath.removePrefix("/")}")
}
)

Expand Down

0 comments on commit ff37e40

Please sign in to comment.