Skip to content
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

Skip null fields in ProtoBuf serialization #588

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions runtime/commonMain/src/kotlinx/serialization/Tagged.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,27 @@ abstract class TaggedEncoder<Tag : Any?> : Encoder, CompositeEncoder {

// ---- Implementation of low-level API ----

fun encodeElement(desc: SerialDescriptor, index: Int): Boolean {
fun <T: Any?> encodeElement(desc: SerialDescriptor, index: Int, value: T): Boolean {
val tag = desc.getTag(index)
val shouldWriteElement = shouldWriteElement(desc, tag, index)
val shouldWriteElement = shouldWriteElement(desc, tag, index, value)
if (shouldWriteElement) {
pushTag(tag)
}
return shouldWriteElement
}

fun encodeNullElement(desc: SerialDescriptor, index: Int): Boolean {
val tag = desc.getTag(index)
val shouldWriteElement = shouldWriteNullElement(desc, tag, index)
if (shouldWriteElement) {
pushTag(tag)
}
return shouldWriteElement
}

// For format-specific behaviour, invoked only on
open fun shouldWriteElement(desc: SerialDescriptor, tag: Tag, index: Int) = true
open fun <T: Any?> shouldWriteElement(desc: SerialDescriptor, tag: Tag, index: Int, value: T) = true
open fun shouldWriteNullElement(desc: SerialDescriptor, tag: Tag, index: Int) = true

final override fun encodeNotNullMark() = encodeTaggedNotNullMark(currentTag)
final override fun encodeNull() = encodeTaggedNull(popTag())
Expand Down Expand Up @@ -122,12 +132,13 @@ abstract class TaggedEncoder<Tag : Any?> : Encoder, CompositeEncoder {
final override fun encodeStringElement(desc: SerialDescriptor, index: Int, value: String) = encodeTaggedString(desc.getTag(index), value)

final override fun <T : Any?> encodeSerializableElement(desc: SerialDescriptor, index: Int, serializer: SerializationStrategy<T>, value: T) {
if (encodeElement(desc, index))
if (encodeElement(desc, index, value))
encodeSerializableValue(serializer, value)
}

final override fun <T : Any> encodeNullableSerializableElement(desc: SerialDescriptor, index: Int, serializer: SerializationStrategy<T>, value: T?) {
if (encodeElement(desc, index))
if (value == null && encodeNullElement(desc, index)
|| value != null && encodeElement(desc, index, value))
encodeNullableSerializableValue(serializer, value)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,17 @@ private class JsonTreeMapOutput(json: Json, nodeConsumer: (JsonElement) -> Unit)
return JsonObject(content)
}

override fun shouldWriteElement(desc: SerialDescriptor, tag: String, index: Int): Boolean = true
override fun <T: Any?> shouldWriteElement(desc: SerialDescriptor, tag: String, index: Int, value: T): Boolean = true
override fun shouldWriteNullElement(desc: SerialDescriptor, tag: String, index: Int): Boolean = true
}

private class JsonTreeListOutput(json: Json, nodeConsumer: (JsonElement) -> Unit) :
AbstractJsonTreeOutput(json, nodeConsumer) {
private val array: ArrayList<JsonElement> = arrayListOf()
override fun elementName(desc: SerialDescriptor, index: Int): String = index.toString()

override fun shouldWriteElement(desc: SerialDescriptor, tag: String, index: Int): Boolean = true
override fun <T: Any?> shouldWriteElement(desc: SerialDescriptor, tag: String, index: Int, value: T): Boolean = true
override fun shouldWriteNullElement(desc: SerialDescriptor, tag: String, index: Int): Boolean = true

override fun putElement(key: String, element: JsonElement) {
val idx = key.toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class ProtoBuf(context: SerialModule = EmptyModule) : AbstractSerialFormat(conte
else -> throw SerializationException("Primitives are not supported at top-level")
}

override fun shouldWriteNullElement(desc: SerialDescriptor, tag: ProtoDesc, index: Int): Boolean =
desc.kind !is StructureKind.CLASS

override fun encodeTaggedInt(tag: ProtoDesc, value: Int) = encoder.writeInt(value, tag.first, tag.second)
override fun encodeTaggedByte(tag: ProtoDesc, value: Byte) = encoder.writeInt(value.toInt(), tag.first, tag.second)
override fun encodeTaggedShort(tag: ProtoDesc, value: Short) = encoder.writeInt(value.toInt(), tag.first, tag.second)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ class ProtobufReaderTest {
if (isNative()) return // todo: support update on Native
ProtoBuf.loads(TestIntWithList.serializer(), "500308960150045005") shouldBe TestIntWithList(150, listOf(3, 4, 5))
}

@Test
fun readObjectWithNullableMissingFields() {
ProtoBuf.loads(TestInnerNullable.serializer(), "1a0308ab02") shouldBe t7
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ class ProtobufWriterTest {
fun writeNumbers() {
ProtoBuf.dumps(TestNumbers.serializer(), t6).toLowerCase() shouldBe "0d9488010010ffffffffffffffff7f"
}

@Test
fun writeNullableFields() {
ProtoBuf.dumps(TestInnerNullable.serializer(), t7).toLowerCase() shouldBe "1a0308ab02"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ data class TestString(@SerialId(2) val b: String)
@Serializable
data class TestInner(@SerialId(3) val a: TestInt)

@Serializable
data class TestInnerNullable(@SerialId(3) val a: TestInt, @SerialId(4) val b: TestInt? = null)

@Serializable
data class TestComplex(@SerialId(42) val b: Int, @SerialId(2) val c: String)

Expand All @@ -53,3 +56,4 @@ val t3e = TestString("")
val t4 = TestInner(t1)
val t5 = TestComplex(42, "testing")
val t6 = TestNumbers(100500, Long.MAX_VALUE)
val t7 = TestInnerNullable(t1, null)