You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I was coding with buildJsonObject and buildJsonArray Dsl s, I'm very exciting. But JsonPrimitive have no dsl, when I want to get a JsonPrimitive I must use JsonPrimitive() constructor. For a single value, such as 1 in Int, true in Boolean etc. JsonPrimitive is much more longer than the value, this is too fragmented, like little mouse with a big fat tail. So I make this dsl, this may look better. Could you check my code if it meets the basic specifications and suggest and improve my code? Thanks for you.
importkotlinx.serialization.json.JsonNullimportkotlinx.serialization.json.JsonPrimitiveclassJsonPrimitiveBuilder @PublishedApi internal constructor() {
var value:JsonPrimitive=JsonNulloperatorfun <T> T.unaryPlus() where T : Any? {
value =when (this) {
null->JsonNullisInt->JsonPrimitive(this)
isLong->JsonPrimitive(this)
isDouble->JsonPrimitive(this)
isFloat->JsonPrimitive(this.toDouble())
isString->JsonPrimitive(this)
isBoolean->JsonPrimitive(this)
is java.math.BigInteger->JsonPrimitive(this.toString())
is java.math.BigDecimal->JsonPrimitive(this.toString())
else->throwIllegalArgumentException("Unsupported value: $this")
}
}
}
inlinefunprimitive(block:JsonPrimitiveBuilder.() ->Any?): JsonPrimitive {
val builder =JsonPrimitiveBuilder()
builder.apply { +block() }
return builder.value
}
Hi, thanks for the suggestion. However, I'm missing the point here. You've said that JsonPrimitive(123) is too long, but primitive { 123 } is not much shorter. Are there any other supposed usages?
Thank you for your reply. This question is actually quite a headache for me
Because for constructing JSON, we can use the first to second function method or directly use the constructor dsl to automatically infer the type and convert it to JsonPrimitive, if this is serialization. But when JSON contains a value that needs to be correctly serialized by both integers and strings, a problem arises.
For example, I have a data class like this:
The reason for setting userId to JsonPrimitive is because (this is a game account): userId in the game has three types: Int, Long, and String. When parsing from my JSON file, I also need to deserialize them completely.
However, for some times, such as code testing, I need to actively assign values and construct User instances. At this time, I will write a large number of JsonPrimitive() constructors (of course, sometimes constructors are also written in some buildJsonObject and buildJsonArray dsls). In order to make it start with a lowercase letter and compensate for the lack of dsl in JsonPrimitive, I wrote a dsl. But as you said, it doesn't look shorter, and I haven't come up with a better solution yet.
If I had to force myself to think, I could only come up with this one:
When I was coding with buildJsonObject and buildJsonArray Dsl s, I'm very exciting. But JsonPrimitive have no dsl, when I want to get a JsonPrimitive I must use JsonPrimitive() constructor. For a single value, such as 1 in Int, true in Boolean etc. JsonPrimitive is much more longer than the value, this is too fragmented, like little mouse with a big fat tail. So I make this dsl, this may look better. Could you check my code if it meets the basic specifications and suggest and improve my code? Thanks for you.
Then use like this:
Looking forward to your reply!
The text was updated successfully, but these errors were encountered: