-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for "UseStringProtoEnumValueNames" option
- Loading branch information
Showing
12 changed files
with
200 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/Protobuf.System.Text.Json/InternalConverters/ProtoEnumConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Text.Encodings.Web; | ||
using System.Text.Json; | ||
using Google.Protobuf; | ||
using Google.Protobuf.Reflection; | ||
|
||
namespace Protobuf.System.Text.Json.InternalConverters; | ||
|
||
internal class ProtoEnumConverter : InternalConverter | ||
{ | ||
private readonly Dictionary<string, int> _lookup; | ||
private readonly Dictionary<int, JsonEncodedText> _reversedLookup; | ||
private readonly Type _clrType; | ||
|
||
public ProtoEnumConverter(EnumDescriptor fieldInfoEnumType, JavaScriptEncoder? encoder) | ||
{ | ||
_clrType = fieldInfoEnumType.ClrType; | ||
_lookup = fieldInfoEnumType.Values.ToDictionary(x => x.Name, x => x.Number); | ||
_reversedLookup = fieldInfoEnumType.Values.ToDictionary(x => x.Number, x => JsonEncodedText.Encode(x.Name, encoder)); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) | ||
{ | ||
var intValue = (int) value; | ||
if (_reversedLookup.TryGetValue(intValue, out var stringValue)) | ||
{ | ||
writer.WriteStringValue(stringValue); | ||
} | ||
else | ||
{ | ||
writer.WriteNumberValue(intValue); | ||
} | ||
} | ||
|
||
public override void Read(ref Utf8JsonReader reader, IMessage obj, Type typeToConvert, JsonSerializerOptions options, IFieldAccessor fieldAccessor) | ||
{ | ||
if (reader.TokenType == JsonTokenType.String) | ||
{ | ||
if (reader.GetString() is { } stringValue) | ||
{ | ||
if (_lookup.TryGetValue(stringValue, out var value)) | ||
{ | ||
fieldAccessor.SetValue(obj, value); | ||
return; | ||
} | ||
|
||
throw new JsonException($"'{stringValue}' is not a valid value for type {_clrType.FullName}."); | ||
} | ||
} | ||
else if (reader.TokenType == JsonTokenType.Number) | ||
{ | ||
if (reader.TryGetInt32(out var value)) | ||
{ | ||
fieldAccessor.SetValue(obj, value); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
..._serialize_enum_value_as_number_when_using_proto_value_name_is_not_possible.approved.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"enumField": 99 | ||
} |
3 changes: 3 additions & 0 deletions
3
...eWithEnumFieldTests.Should_serialize_enum_value_using_proto_enum_value_name.approved.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"enumField": "FIRST_OPTION" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Protobuf.System.Text.Json.Tests.Utils; | ||
|
||
public class TestHelper | ||
{ | ||
public static JsonSerializerOptions CreateJsonSerializerOptions() | ||
public static JsonSerializerOptions CreateJsonSerializerOptions(Action<JsonProtobufSerializerOptions>? configure = null) | ||
{ | ||
var jsonSerializerOptions = new JsonSerializerOptions(); | ||
jsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; | ||
jsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; | ||
jsonSerializerOptions.AddProtobufSupport(); | ||
var jsonSerializerOptions = new JsonSerializerOptions | ||
{ | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull | ||
|
||
}; | ||
jsonSerializerOptions.AddProtobufSupport(configure ?? (_ => { })); | ||
return jsonSerializerOptions; | ||
} | ||
} |