Reefact.JsonEnumValueBinding 7.0.3

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Reefact.JsonEnumValueBinding --version 7.0.3                
NuGet\Install-Package Reefact.JsonEnumValueBinding -Version 7.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Reefact.JsonEnumValueBinding" Version="7.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Reefact.JsonEnumValueBinding --version 7.0.3                
#r "nuget: Reefact.JsonEnumValueBinding, 7.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Reefact.JsonEnumValueBinding as a Cake Addin
#addin nuget:?package=Reefact.JsonEnumValueBinding&version=7.0.3

// Install Reefact.JsonEnumValueBinding as a Cake Tool
#tool nuget:?package=Reefact.JsonEnumValueBinding&version=7.0.3                

Json Enum Naming

Presentation

Library for customizing enumeration values for serialization / deserialization in JSON format and ASP.Net binding

Just as the JsonPropertyNameAttribute allows customization of property names, JsonEnumValueAttribute performs the same task for enumeration values.

Reinforce the stability of your API contracts and take full control over the JSON serialization of your enumerations with the Reefact.JsonEnumValueBinding library. Designed for developers who demand uncompromising contract security, this library simplifies the customization of enumeration values, guaranteeing long-term compatibility with your customers.

Main features

🔧 Customized contract: The JsonEnumValueAttribute lets you explicitly name each enumeration value. You thus maintain a clear, documented relationship between your enumeration values and the corresponding JSON values in your API contracts.

Stability: With the fine-grained customization offered by the JsonEnumValueAttribute, future changes to your enumeration values internally no longer compromise client compatibility. Simplify JSON serialization without sacrificing contract clarity and stability. Contractual security is reinforced, allowing refactorings without risk of breakage.

📚 Documentation simplified: The use of the JsonEnumValueAttribute promotes the intrinsic documentation of your contracts, offering developers using your APIs immediate insight into the intentions behind each enumeration value.

🌐 Compatible with ASP.NET and System.Text.Json: Use JsonEnumValueAttribute to enforce customization when binding in the ASP.NET context, ensuring that your API contracts remain robust even in complex environments.

Usage

How to get started :

  • Install the NuGet "Reefact.JsonEnumValueBinding" package for your project
dotnet add package Reefact.JsonEnumValueBinding
  • Activate the functionality in the Startup.cs file
public class Startup {

    public void ConfigureServices(IServiceCollection services) {
        services.AddControllers()
                AddJsonEnumValueBinding(); // Binding enabled
        // ...
    }
    // ...
}
  • Apply the JsonEnumValueAttribute to enumeration values in your code.
public enum ProductStatus {
    [JsonEnumValue("available")] Available,
    [JsonEnumValue("out_of_stock")] OutOfStock,
    [JsonEnumValue("discontinued")] Discontinued
}

Thus, the example below:

using Microsoft.AspNetCore.Mvc;

public class Product {
    public [JsonPropertyName("name")] string Name { get; set; }
    public [JsonPropertyName("status")] string Status { get; set; }
}

[ApiController]
[Route("api/products")]
public sealed class ProductsController : ControllerBase {

    [HttpGet("{id}")]
    public IActionResult GetProduct(int id) {
        var product = new Product {
            Name = "Widget",
            Status = ProductStatus.OutOfStock
        };

        return Ok(product); 
    }
}

produces the following result:

{
    "name": "Widget",
    "status": "out_of_stock"
}

Binding is also supported:

[ApiController]
[Route("api/products")]
public class ProductsController : ControllerBase {

    private readonly List<Product> _products = new List<Product> {
        new Product { Name = "Widget 1", Status = ProductStatus.OutOfStock },
        new Product { Name = "Widget 2", Status = ProductStatus.Available },
        new Product { Name = "Widget 3", Status = ProductStatus.OutOfStock }
    };

    [HttpGet("{status}")]
    public IActionResult GetProductsByStatus([FromRoute] ProductStatus status) {
        var productsByStatus = _products.Where(p => p.Status == status).ToList();

        return Ok(productsByStatus);
    }
}

Next call:

curl http://localhost:5000/api/products/out_of_stock

produces the following result:

[
    {
        "name": "Widget 1",
        "status": "out_of_stock"
    },
    {
        "name": "Widget 3",
        "status": "out_of_stock"
    }
]

Conclusion

Get started today and get stable API contracts, clear documentation and peace of mind when refactoring.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0 204 12/24/2023
7.0.3 1,665 12/14/2023
7.0.1 3,943 8/29/2023
7.0.0 356 8/20/2023
6.0.0 160 8/20/2023
5.0.0 324 8/20/2023 5.0.0 is deprecated because it is no longer maintained.

Add a possibility to configure an instance of a serializer the same way as ASP.Net serializer.