Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ internal class JsonMessageEncoder(private val jsonConfig: JsonConfig) : MessageE
@Suppress("UNCHECKED_CAST")
val value = (fd.value as KProperty1<T, *>).get(message)

if (value == null && fd.oneofMember) continue
if (!fd.oneofMember && !jsonConfig.outputDefaultValues && fd.type.isDefaultValue(value)) continue
if (value == null && fd.type.hasPresence) continue
if (!fd.type.hasPresence && !jsonConfig.outputDefaultValues && fd.type.isDefaultValue(value)) continue

val jsonValue = value
?.takeUnless {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class OutputDefaultValuesTest {
assertEquals(JsonPrimitive(""), parsedJson["optionalBytes"])
assertEquals(JsonPrimitive(TestAllTypesProto3.NestedEnum.fromValue(0).name!!), parsedJson["optionalNestedEnum"])

assertEquals(JsonNull, parsedJson["optionalNestedMessage"])
assertFalse("optionalNestedMessage" in parsedJson)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked deeper into this, please see explanation in PR description about this.


assertEquals(JsonArray(emptyList()), parsedJson["repeatedString"])
assertEquals(JsonObject(emptyMap()), parsedJson["mapBoolBool"])

assertEquals(JsonNull, parsedJson["optionalStringWrapper"])
assertFalse("optionalStringWrapper" in parsedJson)
assertEquals(JsonPrimitive(false), parsedJson["optionalBoolWrapper"])
}

Expand All @@ -55,13 +55,13 @@ class OutputDefaultValuesTest {
@Test
fun testProto2EnumUnsetField() {
val message = MessageWithEnum()
assertEquals("{\"value\":null}", message.encodeToJsonString(jsonConfigCompactOutput))
assertEquals("{}", message.encodeToJsonString(jsonConfigCompactOutput))
}

@Test
fun testProto2EnumFieldSetToDefaultValue() {
val message = MessageWithEnum(value = MessageWithEnum.EnumType.FOO)
assertEquals("{}", message.encodeToJsonString(jsonConfigCompactOutput))
assertEquals("{\"value\":\"FOO\"}", message.encodeToJsonString(jsonConfigCompactOutput))
}

@Test
Expand Down