Skip to content
Open
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 @@ -135,6 +135,7 @@ abstract class JsonIntegration<F, A> {

private fun mapKeyJsonFormatter(protoAdapter: ProtoAdapter<*>): JsonFormatter<*> = when (protoAdapter) {
ProtoAdapter.STRING -> StringJsonFormatter
ProtoAdapter.BOOL -> BooleanAsStringJsonFormatter
ProtoAdapter.INT32,
ProtoAdapter.SINT32,
ProtoAdapter.SFIXED32,
Expand Down Expand Up @@ -236,6 +237,17 @@ abstract class JsonIntegration<F, A> {
override fun toStringOrNumber(value: Int) = value.toString()
}

/** Encodes a boolean value with quotes, like `"true"`. */
private object BooleanAsStringJsonFormatter : JsonFormatter<Boolean> {
override fun fromString(value: String) = when (value) {
"true" -> true
"false" -> false
else -> throw IllegalArgumentException("expected true or false but was $value")
}

override fun toStringOrNumber(value: Boolean) = value.toString()
}

/** Encodes a byte string as base64. */
private object ByteStringJsonFormatter : JsonFormatter<ByteString> {
override fun toStringOrNumber(value: ByteString) = value.base64()
Expand Down
4 changes: 4 additions & 0 deletions wire-tests/fixtures/shared/proto/proto3/map_types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ message MapTypes {
map<fixed64, fixed64> map_fixed64_fixed64 = 10;
map<uint64, uint64> map_uint64_uint64 = 11;
}

message MapBoolKeys {
map<bool, string> map_bool_string = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import squareup.proto3.CamelCase.NestedCamelCase
import squareup.proto3.ContainsFieldMask
import squareup.proto3.FreeDrinkPromotion
import squareup.proto3.FreeGarlicBreadPromotion
import squareup.proto3.MapBoolKeys
import squareup.proto3.MapTypes
import squareup.proto3.Pizza
import squareup.proto3.PizzaDelivery
Expand Down Expand Up @@ -913,6 +914,17 @@ class WireJsonTest {
)
}

@Test fun mapBoolKeys() {
val value = MapBoolKeys.Builder()
.map_bool_string(mapOf(true to "yes", false to "no"))
.build()
val fieldName = if (jsonLibrary.preservingProtoFieldNames) "map_bool_string" else "mapBoolString"
val json = """{"$fieldName":{"true":"yes","false":"no"}}"""

assertJsonEquals(json, jsonLibrary.toJson(value, MapBoolKeys::class.java))
assertThat(jsonLibrary.fromJson(json, MapBoolKeys::class.java)).isEqualTo(value)
}

@Test fun all32MaxValue() {
val value = All32.Builder()
.my_int32(Int.MAX_VALUE)
Expand Down
Loading