Skip to content

Commit

Permalink
chore: add simple JSON builder
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Nov 14, 2022
1 parent 4f753bc commit 2b9b18b
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package au.com.dius.pact.core.support.json

import au.com.dius.pact.core.support.Json

sealed class JsonBuilder(open val root: JsonValue) {
class JsonObjectBuilder(override val root: JsonValue.Object = JsonValue.Object()): JsonBuilder(root) {
operator fun set(name: String, value: Any?) {
root[name] = Json.toJson(value)
}

fun `object`(function: (JsonObjectBuilder) -> Unit): JsonValue.Object {
return build(function)
}

fun array(function: (JsonArrayBuilder) -> Unit): JsonValue.Array {
val builder = JsonArrayBuilder()
function(builder)
return builder.root
}
}

class JsonArrayBuilder(override val root: JsonValue.Array = JsonValue.Array()): JsonBuilder(root) {
operator fun set(i: Int, value: Any?) {
root[i] = Json.toJson(value)
}

fun push(value: Any?) {
root.append(Json.toJson(value))
}

operator fun plusAssign(value: Any?) {
push(value)
}

fun `object`(function: (JsonObjectBuilder) -> Unit): JsonValue.Object {
return build(function)
}
}

companion object {
fun build(function: (JsonObjectBuilder) -> Unit): JsonValue.Object {
val builder = JsonObjectBuilder()
function(builder)
return builder.root
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package au.com.dius.pact.core.support.json

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.`is`
import org.junit.jupiter.api.Test

class JsonBuilderTest {
@Test
fun testEmptyBuilder() {
val json = JsonBuilder.build {
it
}

val expected = JsonValue.Object()

assertThat(json, `is`(equalTo(expected)))
}

@Test
fun testBasicJson() {
val json = JsonBuilder.build {
it["integer"] = 100
it["decimal"] = 100.22
it["string"] = "100.22"
it["bool"] = true
it["null"] = null
}

val expected = JsonValue.Object(mutableMapOf(
"bool" to JsonValue.True,
"decimal" to JsonValue.Decimal(100.22),
"integer" to JsonValue.Integer(100),
"null" to JsonValue.Null,
"string" to JsonValue.StringValue("100.22")
))

assertThat(json, `is`(equalTo(expected)))
}

@Test
fun testChildObjectJson() {
val json = JsonBuilder.build {
it["child"] = it.`object` { child ->
child["integer"] = 100
child["decimal"] = 100.22
child["string"] = "100.22"
child["bool"] = true
child["null"] = null
}
}

val expected = JsonValue.Object(mutableMapOf("child" to JsonValue.Object(mutableMapOf(
"bool" to JsonValue.True,
"decimal" to JsonValue.Decimal(100.22),
"integer" to JsonValue.Integer(100),
"null" to JsonValue.Null,
"string" to JsonValue.StringValue("100.22")
))))

assertThat(json, `is`(equalTo(expected)))
}

@Test
fun testChildArrayJson() {
val json = JsonBuilder.build {
it["child"] = it.array { child ->
child.push(100.22)
child += "100.22"
child.push(null)
child[2] = 100
}
}

val expected = JsonValue.Object(mutableMapOf("child" to JsonValue.Array(
mutableListOf(
JsonValue.Decimal(100.22),
JsonValue.StringValue("100.22"),
JsonValue.Integer(100),
)
)))

assertThat(json, `is`(equalTo(expected)))
}

@Test
fun testChildMultiLevel() {
val json = JsonBuilder.build {
it["child"] = it.array { child ->
child.push(child.`object` { child2 ->
child2["term"] = true
})
}
}

val expected = JsonValue.Object(mutableMapOf("child" to JsonValue.Array(
mutableListOf(JsonValue.Object("term" to JsonValue.True))
)))

assertThat(json, `is`(equalTo(expected)))
}
}

0 comments on commit 2b9b18b

Please sign in to comment.