Hi, when using directly JsonObject is there any w...
# serialization
a
Hi, when using directly JsonObject is there any way to detect if a given property is string? and get result? eg val json:JsonObject; json["propertyStr"]?.primitive.stringOrNull ? I can see that there is booleanOrNull doubleOrNull but no stringOrNull, tried toString() but it produces quoted string
r
(json[key] as? JsonLiteral).isString
Then once you know it’s a
String
use
content
, not
toString()
Copy code
val JsonPrimitive.stringOrNull: String?
    get() = (this as? JsonLiteral)?.run { if (isString) content else null }
a
thanks
question why it's excluded from a library
r
You usually don’t deal with json like that. You use serializers with models.