@NullCodable is a property wrapper that encodes nil optional values as null when encoded using JSONEncoder.
On its own, JSONEncoder will omit optional properties that are nil - meaning that this:
struct Test: Codable {
var name: String? = nil
}
will be encoded as: {}.
If for some reason, you would like optional properties that are nil
to be encoded in JSON as null, then marking those properties as @NullCodable
will do so.
@NullCodable
@NullCodableis a property wrapper that encodesniloptional values asnullwhen encoded usingJSONEncoder.On its own,
JSONEncoderwill omit optional properties that arenil- meaning that this:will be encoded as:
{}.If for some reason, you would like optional properties that are nil to be encoded in JSON as
null, then marking those properties as@NullCodablewill do so.For example, adding
@NullCodablelike this:will encode as:
{\"name\": null}.