Skip to content

Commit

Permalink
Update StrEnum to v2.0.0 and use the new TryParse method to match m…
Browse files Browse the repository at this point in the history
…embers by value. (#3)
  • Loading branch information
dmytro-khmara committed Jul 29, 2022
1 parent ca3fbce commit 0c374e3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@ The above produces:

### Deserialize from JSON:

```csharp
var race = JsonSerializer.Deserialize<Race>(json, options);
```json
{"Name":"Cape Town Cycle Tour","Sport":"ROAD_CYCLING"}
```

`race` is equivalent to:
The above JSON can be deserialized into a C# object that contains a StrEnum enum:

```csharp
var race = JsonSerializer.Deserialize<Race>(json, options);

// race is equivalent to:
new { Name = "Cape Town Cycle Tour", Sport = Sport.RoadCycling };
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ public override TStringEnum Read(ref Utf8JsonReader reader, Type typeToConvert,
{
var jsonValue = reader.GetString();

StringEnum<TStringEnum>.TryParse(reader.GetString()!, out var parsed);
var parsed = StringEnum<TStringEnum>.TryParse(reader.GetString()!, out var member, matchBy:MatchBy.ValueOnly);

if (parsed == null || !((string)parsed).Equals(jsonValue, StringComparison.InvariantCulture))
if (!parsed)
{
if (_noMemberFoundBehavior == NoMemberFoundBehavior.ReturnNull)
return null!;

throw new JsonException($"Requested name or value '{jsonValue}' was not found in the string enum '{typeof(TStringEnum).Name}'.");
throw new JsonException($"Requested value '{jsonValue}' was not found in the string enum '{typeof(TStringEnum).Name}'.");
}

return parsed;
return member!;
}

public override void Write(Utf8JsonWriter writer, TStringEnum value, JsonSerializerOptions options)
Expand Down
4 changes: 2 additions & 2 deletions src/StrEnum.System.Text.Json/StrEnum.System.Text.Json.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageId>StrEnum.System.Text.Json</PackageId>
Expand All @@ -19,7 +19,7 @@

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="[4.6.0,7.0.0)" />
<PackageReference Include="StrEnum" Version="[1.4.0,2.0.0)" />
<PackageReference Include="StrEnum" Version="[2.0.0,3.0.0)" />
</ItemGroup>

<ItemGroup>
Expand Down
19 changes: 16 additions & 3 deletions test/StrEnum.System.Text.Json.IntegrationTests/DeserializeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Deserialize_GivenJsonWithEnumsName_AndDefaultNoMemberBehavior_Should
var deserialize = () => JsonSerializer.Deserialize<DeserializedObject>(json, options);

deserialize.Should().Throw<JsonException>()
.WithMessage("Requested name or value 'TrailRunning' was not found in the string enum 'Sport'.");
.WithMessage("Requested value 'TrailRunning' was not found in the string enum 'Sport'.");
}

[Fact]
Expand All @@ -68,11 +68,11 @@ public void Deserialize_GivenJsonWithInvalidValue_AndDefaultNoMemberBehavior_Sho
var deserialize = () => JsonSerializer.Deserialize<DeserializedObject>(json, options);

deserialize.Should().Throw<JsonException>()
.WithMessage("Requested name or value 'Quidditch' was not found in the string enum 'Sport'.");
.WithMessage("Requested value 'Quidditch' was not found in the string enum 'Sport'.");
}

[Fact]
public void Deserialize_GivenJsonWithInvalidValue_AndReturnNullNoMemberBehavior_ShouldThrowAnException()
public void Deserialize_GivenJsonWithInvalidValue_AndReturnNullNoMemberBehavior_ShouldReturnNull()
{
var json = @"{""sport"":""Quidditch""}";

Expand All @@ -83,5 +83,18 @@ public void Deserialize_GivenJsonWithInvalidValue_AndReturnNullNoMemberBehavior_

obj.Should().BeEquivalentTo(new { Sport = (Sport?)null });
}

[Fact]
public void Deserialize_GivenJsonWithValidName_AndReturnNullNoMemberBehavior_ShouldReturnNull()
{
var json = @"{""sport"":""TrailRunning""}";

var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }
.UseStringEnums(NoMemberFoundBehavior.ReturnNull);

var obj = JsonSerializer.Deserialize<DeserializedObject>(json, options);

obj.Should().BeEquivalentTo(new { Sport = (Sport?)null });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="StrEnum" Version="1.4.0" />
<PackageReference Include="StrEnum" Version="2.0.0" />
<PackageReference Include="System.Text.Json" Version="6.0.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand Down

0 comments on commit 0c374e3

Please sign in to comment.