[platform] improve JsonBuilder by adding Int key-value appender

GitOrigin-RevId: 8b375e96d7c28afe7788e28e096ea604cabfa067
This commit is contained in:
Maciej Procyk
2023-08-18 11:14:10 +02:00
committed by intellij-monorepo-bot
parent 365bdd41ef
commit 8968d00307
2 changed files with 25 additions and 2 deletions

View File

@@ -42,6 +42,12 @@ class JsonObjectBuilder(val builder: StringBuilder, var indentLevel: Int = 0, va
}
}
infix fun String.to(value: Int) {
appendNameAndValue(this) {
builder.append(value)
}
}
infix fun String.to(value: StringBuilder) {
if (value === builder) {
return
@@ -173,7 +179,7 @@ private fun appendCommaIfNeeded(builder: StringBuilder): Boolean {
}
val lastChar = builder.last()
if (lastChar == '"' || lastChar == '}' || lastChar == ']' || lastChar == 'e' /* true or false */) {
if (lastChar == '"' || lastChar == '}' || lastChar == ']' || lastChar == 'e' /* true or false */ || lastChar.isDigit()) {
builder.append(',')
return true
}

View File

@@ -24,10 +24,27 @@ class JsonBuilderTest {
stringBuilder.json {
"foo" to "bar"
"p2" to false
"p3" to true
"p4" to false
}
assertThat(stringBuilder.toString()).isEqualTo("""
{
"foo": "bar","p2": false
"foo": "bar","p2": false,"p3": true,"p4": false
}
""".trimIndent())
}
@Test
fun `several and int`() {
val stringBuilder = StringBuilder()
stringBuilder.json {
"foo" to "bar"
"p2" to 42
"p3" to 24
}
assertThat(stringBuilder.toString()).isEqualTo("""
{
"foo": "bar","p2": 42,"p3": 24
}
""".trimIndent())
}