Daniel Pitts
12/30/2024, 4:24 AM.optional()
2. Properties are optional by default, but could be made required with .required()
3. .optional()
vs .required()
always need to be specified
4. Required properties are not nullable by default: They are expected to exist in the JsonObject, and not be JsonNull.
5. Required properties are nullable by default: They are expected to exist in the JsonObject, but my be JsonNull.
6. Nullability on required properties should always be explicitly declared (eg, .nonNull()
and .nullable()
)
7. Setting an optional property to null will remove the key from the JsonObject.
8. Setting an optional property to null will set the key to JsonNull.
9. Nullability on optional properties must always be explicitly declared (eg. .unsetOnNull()
, and .storeNulls()
)
10. Some other set of behaviors not listed here.Daniel Pitts
12/30/2024, 4:26 AMval name by string("name").required().nonNull()
val age by int("age").required().nonNull()
val address by string("address").optional().storeNulls()
val favoriteColor by string("color") // default requiredness/nullability
asdf asdf
12/30/2024, 7:31 AMDaniel Pitts
12/30/2024, 4:11 PMasdf asdf
12/30/2024, 4:40 PMCLOVIS
12/30/2024, 4:50 PMnull
and "presence".
First, when I'm reading JSON, in almost all situations I find reasonable to read missing data as null
. I think that's less surprising than having to treat JSON's null
as JsonNull
or whatever other thing that Kotlin null
. For the cases where the difference is meaningful, you could have a isPresent
or has
or in
method to check the presence of a field.Daniel Pitts
12/30/2024, 4:53 PMJsonObject
class itself, there are methods for checking whether or not the key exists. It actually implements MutableMap<String, JsonElement>
. The delegates will be available only within JsonObjectWrapper
subclasses, which will also give you access to the underlying JsonObject if necessary.CLOVIS
12/30/2024, 4:54 PMDaniel Pitts
12/30/2024, 4:54 PMvar
(I wasn't paying attention and meant to use var actually)CLOVIS
12/30/2024, 4:56 PMDaniel Pitts
12/30/2024, 4:56 PMvar name by string("name").required().nonNull()
var age by int("age").required().nonNull()
var address by string("address").optional().storeNulls()
var favoriteColor by string("color") // default requiredness/nullability
Daniel Pitts
12/30/2024, 4:57 PMmyObj.name = "Bob"
myObj.age = 42
myObj.address = null // Stores JsonNull
myObj.favoriteColor = null // Removes `color` from the object.
Daniel Pitts
12/30/2024, 5:02 PM.ref()
method that returns basically a mutable optional that is backed by the object:
var address: OptionalPropertyReference<String?> by string("address").optional().storeNulls().ref()
// ...
// address.isSet
// address.clear()
// address.set("123 Mulberry Ln")
// address.set(null)
// etc...