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
5 changes: 4 additions & 1 deletion src/Protobuf.System.Text.Json/FieldTypeResolver.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Google.Protobuf;
using Google.Protobuf.Reflection;
using Google.Protobuf.WellKnownTypes;
using Type = System.Type;
Expand Down Expand Up @@ -32,6 +33,8 @@ public static Type ResolverFieldType(FieldDescriptor fieldDescriptor, Dictionary
return typeof(bool);
case FieldType.String:
return typeof(string);
case FieldType.Bytes:
return typeof(ByteString);
case FieldType.Message when fieldDescriptor.MessageType.ClrType is { } clrType:
Comment thread
Havret marked this conversation as resolved.
if (clrType == typeof(DoubleValue))
return typeof(double?);
Expand All @@ -58,7 +61,7 @@ public static Type ResolverFieldType(FieldDescriptor fieldDescriptor, Dictionary
return propertyTypeLookup[fieldDescriptor.PropertyName];
default:
throw new ArgumentOutOfRangeException(nameof(fieldDescriptor),
$"FieldType: '{fieldDescriptor}' is not supported.");
$"FieldType: '{fieldDescriptor.FieldType}' is not supported.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static void AddProtobufSupport(this JsonSerializerOptions options, Action
{
options.Converters.Add(new TimestampConverter());
}
options.Converters.Add(new ByteStringConverter());
options.Converters.Add(new ProtobufJsonConverterFactory(jsonProtobufSerializerOptions));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Google.Protobuf;

namespace Protobuf.System.Text.Json.WellKnownTypesConverters;

internal class ByteStringConverter : JsonConverter<ByteString?>
{
public override ByteString? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.GetString() is { } base64String)
{
return ByteString.FromBase64(base64String);
}

return null;
}

public override void Write(Utf8JsonWriter writer, ByteString? value, JsonSerializerOptions options)
{
if (value != null)
{
writer.WriteStringValue(value.ToBase64());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"sfixed32Property": 0,
"sfixed64Property": 0,
"boolProperty": false,
"stringProperty": ""
"stringProperty": "",
"bytesProperty": ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"sfixed32Property": 0,
"sfixed64Property": 0,
"boolProperty": false,
"stringProperty": ""
"stringProperty": "",
"bytesProperty": ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"sfixed32Property": 0,
"sfixed64Property": 0,
"boolProperty": false,
"stringProperty": ""
"stringProperty": "",
"bytesProperty": ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"sfixed32property": 0,
"sfixed64property": 0,
"boolproperty": false,
"stringproperty": ""
"stringproperty": "",
"bytesproperty": ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"a": 1
}
}
},
"mapStringToBytesType": {
"string_key": "YWJj"
}
}
7 changes: 5 additions & 2 deletions test/Protobuf.System.Text.Json.Tests/MessageWithMapsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using System.Text.Json.Protobuf.Tests;
using Google.Protobuf;
using Protobuf.System.Text.Json.Tests.Utils;
using Shouldly;
using SmartAnalyzers.ApprovalTestsExtensions;
Expand All @@ -22,7 +23,8 @@ public void Should_serialize_message_with_map_field()
{
MapStringToInt = {["a"] = 1}
}
}
},
MapStringToBytesType = { ["string_key"] = ByteString.CopyFromUtf8("abc") }
};
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();

Expand All @@ -47,7 +49,8 @@ public void Should_deserialize_message_with_map_field()
{
MapStringToInt = {["a"] = 1}
}
}
},
MapStringToBytesType = { ["string_key"] = ByteString.CopyFromUtf8("abc") }
};
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ option csharp_namespace = "System.Text.Json.Protobuf.Tests";
message MessageWithMaps {
map<int32, string> map_int_to_string = 1;
map<string, NestedMessageAsKey> map_string_to_complex_type = 2;
map<string, bytes> map_string_to_bytes_type = 3;
}

message NestedMessageAsKey {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ message SimpleMessage {

string string_property = 14;

// TODO: Support bytes property
// bytes bytes_property = 15;
bytes bytes_property = 15;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"sfixed32Property": 9,
"sfixed64Property": 10,
"boolProperty": true,
"stringProperty": "hello"
"stringProperty": "hello",
"bytesProperty": "YWJj"
}
11 changes: 5 additions & 6 deletions test/Protobuf.System.Text.Json.Tests/SimpleMessageTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using System.Text.Json.Protobuf.Tests;
using Google.Protobuf;
using Protobuf.System.Text.Json.Tests.Utils;
using Shouldly;
using SmartAnalyzers.ApprovalTestsExtensions;
Expand Down Expand Up @@ -28,9 +29,8 @@ public void Should_serialize_message_with_primitive_types()
Sfixed32Property = 9,
Sfixed64Property = 10,
BoolProperty = true,
StringProperty = "hello"
// TODO: Support bytes property
// BytesProperty = ByteString.CopyFromUtf8("abc")
StringProperty = "hello",
BytesProperty = ByteString.CopyFromUtf8("abc")
};
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();

Expand Down Expand Up @@ -61,9 +61,8 @@ public void Should_deserialize_message_with_primitive_types()
Sfixed32Property = 9,
Sfixed64Property = 10,
BoolProperty = true,
StringProperty = "hello"
// TODO: Support bytes property
// BytesProperty = ByteString.CopyFromUtf8("abc")
StringProperty = "hello",
BytesProperty = ByteString.CopyFromUtf8("abc")
};
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();

Expand Down