Opinions, improvements? ``` class JsonObject { ...
# javascript
s
Opinions, improvements?
Copy code
class JsonObject {
    val obj = js("({})")

    infix fun <T : Any> String.l(other: T) {
        obj[this] = other
    }
}

fun jo(builder: JsonObject.() -> Unit): Any {
    return JsonObject().apply(builder).obj as Any
}

fun main() {
    val test = jo {
        "this" l "that"
        "this other" l 1
        "user" l jo {
            "first_name" l "jane"
            "last_name" l "doe"
        }
    }
}
j
Doesn't seem to have much meaningful benefit to https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/json.html
s
Hrm, gp
j
Not trying to discourage you or anything. I'm just not sure the DSL has enough benefit compared to something that's already in the stdlib.
Copy code
val test = json(
    "test" to "that",
    "this other" to 1,
    "user" to json(
        "first_name" to "jane",
        "last_name" to "doe"
    )
)
s
Not at all, I had forgotten about that and had been using jsObject{ } from the js extensions and found it lacking. Thx for pointing that out.
s
Other than that, I would think that the snippit could be improved by naming
jo
something like
jsonObject
and the infix
l
to something like
with
?
t
or
to
t
Possibly such method will be more flexible:
Copy code
fun jso(block: dynamic.() -> Unit): dynamic {
    val o = js("({})")
    block(o)
    return o
}
Example:
Copy code
val test = jso {
    test = "that"
    other = 1
    user = jso {
        first_name = "jane"
        last_name = "doe"
    }
}
d
when do we get this i JVM ?