From 9257146821a5f992908395112da67b92ae9c3bb1 Mon Sep 17 00:00:00 2001 From: Dmytro Khmara Date: Sat, 25 Apr 2026 18:11:19 +0100 Subject: [PATCH] Tidy README --- README.md | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 99e2a57..3c8ca18 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # StrEnum.System.Text.Json -Allows for [StrEnum](https://github.com/StrEnum/StrEnum/) string enum JSON serialization and deserialization with System.Text.Json. +Lets you serialize and deserialize [StrEnum](https://github.com/StrEnum/StrEnum/) string enums to JSON via System.Text.Json. -The package targets .NET Standard 2.0 and can be used with System.Text.Json 4.6.0-10.\*. +Targets .NET Standard 2.0; works with System.Text.Json 4.6.0 – 10.x. ## Installation -You can install [StrEnum.System.Text.Json](https://www.nuget.org/packages/StrEnum.System.Text.Json/) using the .NET CLI: +Install [StrEnum.System.Text.Json](https://www.nuget.org/packages/StrEnum.System.Text.Json/) via the .NET CLI: ``` dotnet add package StrEnum.System.Text.Json @@ -14,7 +14,7 @@ dotnet add package StrEnum.System.Text.Json ## Usage -Create a string enum and a class that contains it: +### Defining a string enum and a model ```csharp public class Sport : StringEnum @@ -29,33 +29,31 @@ public class Race } ``` -Configure `JsonSerializerOptions` by calling the `UseStringEnums()` method and pass it to `JsonSerializer` : +### Configuring the serializer + +Call `UseStringEnums()` on a `JsonSerializerOptions` instance and pass it to `JsonSerializer`: ```csharp var options = new JsonSerializerOptions().UseStringEnums(); ``` -### Serialize to JSON: +### Serializing to JSON ```csharp -var ctct = new Race { Name = "Cape Town Cycle Tour", Sport = Sport.RoadCycling }; +var race = new Race { Name = "Cape Town Cycle Tour", Sport = Sport.RoadCycling }; -var json = JsonSerializer.Serialize(ctct, options); +var json = JsonSerializer.Serialize(race, options); ``` -The above produces: +Produces: ```json {"Name":"Cape Town Cycle Tour","Sport":"ROAD_CYCLING"} ``` -### Deserialize from JSON: - -```json -{"Name":"Cape Town Cycle Tour","Sport":"ROAD_CYCLING"} -``` +### Deserializing from JSON -The above JSON can be deserialized into a C# object that contains a StrEnum enum: +The same JSON can be deserialized back to a `Race`: ```csharp var race = JsonSerializer.Deserialize(json, options); @@ -68,4 +66,4 @@ new { Name = "Cape Town Cycle Tour", Sport = Sport.RoadCycling }; Copyright © 2025 [Dmytro Khmara](https://dmytrokhmara.com). -StrEnum is licensed under the [MIT license](LICENSE.txt). \ No newline at end of file +StrEnum is licensed under the [MIT license](LICENSE.txt).