Byrone.Xenia.JSON 0.1.1

dotnet add package Byrone.Xenia.JSON --version 0.1.1
                    
NuGet\Install-Package Byrone.Xenia.JSON -Version 0.1.1
                    
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="Byrone.Xenia.JSON" Version="0.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Byrone.Xenia.JSON" Version="0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Byrone.Xenia.JSON" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Byrone.Xenia.JSON --version 0.1.1
                    
#r "nuget: Byrone.Xenia.JSON, 0.1.1"
                    
#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.
#:package Byrone.Xenia.JSON@0.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Byrone.Xenia.JSON&version=0.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Byrone.Xenia.JSON&version=0.1.1
                    
Install as a Cake Tool

Xenia.JSON

Helpers for working with JSON content and (de)serializing JSON using the built-in System.Text.Json package.

Usage

IJson<T>

Interface for simpler JSON (de)serialization using Source Generation. Requires you to implement a static JsonTypeInfo<T> TypeInfo which will be used when using the custom JsonSerializer provided by this package.

using Byrone.Xenia;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;

// Person.cs
public readonly struct Person : IJson<Person>
{
    public static JsonTypeInfo<Person> TypeInfo =>
        AppJsonSerializerContext.Default.Person!;
    
    public required string FirstName { get; init; }
    
    public required string LastName { get; init; }
    
    [JsonPropertyName("dob")] public required System.DateOnly DateOfBirth { get; init; }
    
    [JsonIgnore] public int Age =>
        (int)((System.DateTime.UtcNow - this.DateOfBirth.ToDateTime(default)).TotalDays / 365.242199);
}

// AppJsonSerializerContext.cs
[JsonSourceGenerationOptions(
	GenerationMode = JsonSourceGenerationMode.Metadata,
	DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
	PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower
)]
[JsonSerializable(typeof(Person))]
internal sealed partial class AppJsonSerializerContext : JsonSerializerContext;

JsonSerializer

Custom methods that call the built-in System.Text.Json.JsonSerializer for classes/structs implementing IJson<T>. It will use the implemented TypeInfo property to (de)serialize JSON using Source Generation.

using Byrone.Xenia;

var utf8Json = "..."u8;
var person = JsonSerializer.Deserialize<Person>(utf8Json);

var writer = new Utf8JsonWriter(stream);
JsonSerializer.Serialize(writer, person);

Extension methods

Extension methods for the Request struct for deserializing the request body as a JSON value. This method doesn't validate if the request contains valid JSON data, you should do this yourself by validating the Content-Type header of the request.

using Byrone.Xenia;

static IResponse RequestHandler(in Request request)
{
    var person = request.GetBodyAsJson<Person>();
}
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
0.1.1 183 10/29/2024