用于创建json对象的Kotlin DSL(无需创建垃圾)

ilk*_*las 17 kotlin

我正在尝试创建一个用于创建JSONObjects的DSL.这是一个构建器类和一个示例用法:

import org.json.JSONObject

fun json(build: JsonObjectBuilder.() -> Unit): JSONObject {
    val builder = JsonObjectBuilder()
    builder.build()
    return builder.json
}

class JsonObjectBuilder {
    val json = JSONObject()

    infix fun <T> String.To(value: T) {
        json.put(this, value)
    }
}

fun main(args: Array<String>) {
    val jsonObject =
            json {
                "name" To "ilkin"
                "age" To 37
                "male" To true
                "contact" To json {
                    "city" To "istanbul"
                    "email" To "xxx@yyy.com"
                }
            }
    println(jsonObject)
}
Run Code Online (Sandbox Code Playgroud)

上面代码的输出是:

{"contact":{"city":"istanbul","email":"xxx@yyy.com"},"name":"ilkin","age":37,"male":true}
Run Code Online (Sandbox Code Playgroud)

它按预期工作.但它每次创建一个json对象时都会创建一个额外的JsonObjectBuilder实例.是否可以编写DSL来创建json对象而无需额外的垃圾?

mfu*_*n26 16

您可以使用Deque作为堆栈来JSONObject使用单个跟踪当前上下文JsonObjectBuilder:

fun json(build: JsonObjectBuilder.() -> Unit): JSONObject {
    return JsonObjectBuilder().json(build)
}

class JsonObjectBuilder {
    private val deque: Deque<JSONObject> = ArrayDeque()

    fun json(build: JsonObjectBuilder.() -> Unit): JSONObject {
        deque.push(JSONObject())
        this.build()
        return deque.pop()
    }

    infix fun <T> String.To(value: T) {
        deque.peek().put(this, value)
    }
}

fun main(args: Array<String>) {
    val jsonObject =
            json {
                "name" To "ilkin"
                "age" To 37
                "male" To true
                "contact" To json {
                    "city" To "istanbul"
                    "email" To "xxx@yyy.com"
                }
            }
    println(jsonObject)
}
Run Code Online (Sandbox Code Playgroud)

示例输出:

{"contact":{"city":"istanbul","email":"xxx@yyy.com"},"name":"ilkin","age":37,"male":true}
Run Code Online (Sandbox Code Playgroud)

在单个上调用jsonbuild跨多个线程JsonObjectBuilder会有问题,但这不应该是您的用例的问题.


Jam*_*ett 10

你需要DSL吗?你失去了执行String密钥的能力,但香草Kotlin并不是那么糟糕:)

JSONObject(mapOf(
        "name" to "ilkin",
        "age" to 37,
        "male" to true,
        "contact" to mapOf(
                "city" to "istanbul",
                "email" to "xxx@yyy.com"
        )
))
Run Code Online (Sandbox Code Playgroud)

  • 我同意,这个版本已经看起来像DSL了.但它会创建临时数组和对. (2认同)

Ars*_*rst 6

我不确定我是否正确回答了这个问题。你不想要一个建造者?

import org.json.JSONArray
import org.json.JSONObject

class Json() {

    private val json = JSONObject()

    constructor(init: Json.() -> Unit) : this() {
        this.init()
    }

    infix fun String.to(value: Json) {
        json.put(this, value.json)
    }

    infix fun <T> String.to(value: T) {
        json.put(this, value)
    }

    override fun toString(): String {
        return json.toString()
    }
}

fun main(args: Array<String>) {

    val json = Json {
        "name" to "Roy"
        "body" to Json {
            "height" to 173
            "weight" to 80
        }
        "cars" to JSONArray().apply {
            put("Tesla")
            put("Porsche")
            put("BMW")
            put("Ferrari")
        }
    }

    println(json)

}
Run Code Online (Sandbox Code Playgroud)

你会得到

{
  "name": "Roy",
  "body": {
    "weight": 80,
    "height": 173
  },
  "cars": [
    "Tesla",
    "Porsche",
    "BMW",
    "Ferrari"
  ]
}
Run Code Online (Sandbox Code Playgroud)