Skip to content

Commit 61e6dc7

Browse files
committed
Support all primitive types
1 parent f03df0c commit 61e6dc7

4 files changed

Lines changed: 92 additions & 7 deletions

File tree

src/System.Text.Json.Protobuf/ProtobufConverter.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ProtobufConverter()
2121
{
2222
Accessor = fieldDescriptor.Accessor,
2323
IsRepeated = fieldDescriptor.IsRepeated,
24-
FieldType = GetFieldType(fieldDescriptor),
24+
FieldType = GetFieldType(fieldDescriptor.FieldType),
2525
JsonName = fieldDescriptor.JsonName,
2626
};
2727
_fieldsLookup.Add(fieldDescriptor.JsonName, fieldInfo);
@@ -90,16 +90,34 @@ public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOption
9090
writer.WriteEndObject();
9191
}
9292

93-
private Type GetFieldType(FieldDescriptor fieldDescriptor)
93+
private Type GetFieldType(FieldType fieldType)
9494
{
95-
switch (fieldDescriptor.FieldType)
95+
switch (fieldType)
9696
{
97+
case FieldType.Double:
98+
return typeof(double);
99+
case FieldType.Float:
100+
return typeof(float);
97101
case FieldType.Int32:
102+
case FieldType.SInt32:
103+
case FieldType.SFixed32:
98104
return typeof(int);
99105
case FieldType.Int64:
106+
case FieldType.SInt64:
107+
case FieldType.SFixed64:
100108
return typeof(long);
109+
case FieldType.UInt32:
110+
case FieldType.Fixed32:
111+
return typeof(uint);
112+
case FieldType.UInt64:
113+
case FieldType.Fixed64:
114+
return typeof(ulong);
115+
case FieldType.Bool:
116+
return typeof(bool);
117+
case FieldType.String:
118+
return typeof(string);
101119
default:
102-
throw new ArgumentOutOfRangeException();
120+
throw new ArgumentOutOfRangeException(nameof(fieldType), $"FieldType: '{fieldType}' is not supported.");
103121
}
104122
}
105123
}

test/System.Text.Json.Protobuf.Tests/Protos/simple_message.proto

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,34 @@ syntax = "proto3";
33
option csharp_namespace = "System.Text.Json.Protobuf.Tests";
44

55
message SimpleMessage {
6-
int32 int_32_property = 1;
6+
double double_property = 1;
7+
8+
float float_property = 2;
9+
10+
int32 int_32_property = 3;
11+
12+
int64 int_64_property = 4;
13+
14+
uint32 uint_32_property = 5;
15+
16+
uint32 uint_64_property = 6;
17+
18+
sint32 sint_32_property = 7;
19+
20+
sint64 sint_64_property = 8;
21+
22+
fixed32 fixed_32_property = 9;
23+
24+
fixed64 fixed_64_property = 10;
25+
26+
sfixed32 sfixed_32_property = 11;
27+
28+
sfixed64 sfixed_64_property = 12;
29+
30+
bool bool_property = 13;
31+
32+
string string_property = 14;
733

8-
int64 int_64_property = 2;
34+
// TODO: Support bytes property
35+
// bytes bytes_property = 15;
936
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
{
2+
"doubleProperty": 2.5,
3+
"floatProperty": 3.5,
24
"int32Property": 1,
3-
"int64Property": 2
5+
"int64Property": 2,
6+
"uint32Property": 3,
7+
"uint64Property": 4,
8+
"sint32Property": 5,
9+
"sint64Property": 6,
10+
"fixed32Property": 7,
11+
"fixed64Property": 8,
12+
"sfixed32Property": 9,
13+
"sfixed64Property": 10,
14+
"boolProperty": true,
15+
"stringProperty": "hello"
416
}

test/System.Text.Json.Protobuf.Tests/SimpleMessageTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,22 @@ public void Should_serialize_message_with_primitive_types()
1313
// Arrange
1414
var msg = new SimpleMessage
1515
{
16+
DoubleProperty = 2.5d,
17+
FloatProperty = 3.5f,
1618
Int32Property = 1,
1719
Int64Property = 2,
20+
Uint32Property = 3,
21+
Uint64Property = 4,
22+
Sint32Property = 5,
23+
Sint64Property = 6,
24+
Fixed32Property = 7,
25+
Fixed64Property = 8,
26+
Sfixed32Property = 9,
27+
Sfixed64Property = 10,
28+
BoolProperty = true,
29+
StringProperty = "hello"
30+
// TODO: Support bytes property
31+
// BytesProperty = ByteString.CopyFromUtf8("abc")
1832
};
1933
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();
2034

@@ -32,8 +46,22 @@ public void Should_deserialize_message_with_primitive_types()
3246
// Arrange
3347
var msg = new SimpleMessage
3448
{
49+
DoubleProperty = 2.5d,
50+
FloatProperty = 3.5f,
3551
Int32Property = 1,
3652
Int64Property = 2,
53+
Uint32Property = 3,
54+
Uint64Property = 4,
55+
Sint32Property = 5,
56+
Sint64Property = 6,
57+
Fixed32Property = 7,
58+
Fixed64Property = 8,
59+
Sfixed32Property = 9,
60+
Sfixed64Property = 10,
61+
BoolProperty = true,
62+
StringProperty = "hello"
63+
// TODO: Support bytes property
64+
// BytesProperty = ByteString.CopyFromUtf8("abc")
3765
};
3866
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();
3967

0 commit comments

Comments
 (0)