-
Notifications
You must be signed in to change notification settings - Fork 626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make skipping null values default behavior for protobuf #397
Comments
Protobuf case can be more complicated than JSON case since JSON can distinguish between Imagine you deserialize data class with Protobuf and it has field There is also a problem with Seems like the proper solution is to skip default values only if they're |
Unlike JSON, protobuf relies on explicitly defined schema, hence I believe some of the uncertainties you mention can be resolved by looking at the schema. Let's take the following message: message Employee {
int32 id = 1;
optional string name = 2;
optional bool is_contractor = 3 [default = false];
} With this schema, message recipients should always expect data class Employee(
@SerialId(1) val id: Int,
@SerialId(2) @Optional val name: String? = null,
@SerialId(3) @Optional val is_contractor: Boolean = false
) Also, since protobuf always has schema, the code is usually generated, and the generator is responsible for ensuring that correct semantics are preserved for all fields. For instance, To sum up, the deserializer should always rely on the default value of a field if it's missing on the wire, and the code generator is responsible for properly translating proto messages into data classes, preserving the semantics of individual fields. |
What about the case:
with
In this case when receiving an input stream without Reference: https://developers.google.com/protocol-buffers/docs/proto#optional
It may induce questionable versioning story, but that's really dependent on the application usage of protobuf and is really application's own problem. On the flip side (serialize for a class with optional field and default value), I think the enhancement behavior, if considered, can extend beyond just skipping if default value is null. If there is a default value, then the field is considered optional to kotlinx.serialization. Why restrict to just skipping null default values? As long as the object instance's optional field has a value that |
Porting a project using the *Java ProtoBuf library* to *kotlinx.serialization* can become quite hard since optional properties are not supported in the same way. On the one hand it is not possible to omit optional properties during serialization (this is possible by not setting an optional property while building the message in Java). On the other hand it is not possible to check if a property has been omitted by the sender during de-serialization (this is possible via `Message.hasA()` in Java). Allowing to set `ProtoBuf.shouldEncodeElementDefault()` to `false` allows to create `@Serializable data classes` that support optional properties during serialization and de-serialization. Additionally, it makes `null` values possible for optional properties as well. Closes Kotlin#397 Make skipping null values default behavior for protobuf Closes Kotlin#71 NULLs are not supported when writing to protobuf
Porting a project using the *Java ProtoBuf library* to *kotlinx.serialization* can become quite hard since optional properties are not supported in the same way. On the one hand it is not possible to omit optional properties during serialization (this is possible by not setting an optional property while building the message in Java). On the other hand it is not possible to check if a property has been omitted by the sender during de-serialization (this is possible via `Message.hasA()` in Java). Allowing to set `ProtoBuf.shouldEncodeElementDefault()` to `false` allows to create `@Serializable data classes` that support optional properties during serialization and de-serialization. Additionally, it makes `null` values possible for optional properties as well. Closes Kotlin#397 Make skipping null values default behavior for protobuf Closes Kotlin#71 NULLs are not supported when writing to protobuf
Based on #58.
My understanding is that since
0.10.0
there is support for skipping optional values, which can be enabled for JSON withJson(encodeDefaults = false)
, but it's not clear how to enable this behavior forProtoBuf
. In my opinion this should also be the default behavior for protobuf, since the only way to transfer null values is to omit them in the binary representation.The text was updated successfully, but these errors were encountered: