Skip to content

ted-sharp/aloe-utils-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Aloe.Utils.Json

English 日本語

NuGet Version NuGet Downloads License .NET

Aloe.Utils.Json is a lightweight utility for JSON serialization and formatting in .NET applications.

Main Features

  • Simple JSON serialization and deserialization with extension methods
  • JSON string formatting with proper indentation
  • Type-safe conversion between objects and JSON
  • Try-pattern methods that don't throw exceptions
  • Support for custom JsonSerializerOptions
  • Native AOT/trimming support with JsonTypeInfo overloads

Supported Environments

  • .NET 9 and later

Install

Install via NuGet Package Manager:

Install-Package Aloe.Utils.Json

Or using .NET CLI:

dotnet add package Aloe.Utils.Json

Usage

Basic Usage

using Aloe.Utils.Json;

// Serialize an object to JSON
var person = new Person { Name = "John", Age = 30 };
string json = person.ToJson();

// Deserialize JSON to an object
var deserializedPerson = json.ToObj<Person>();

// Format a JSON string
string formattedJson = json.FormatJson();

Try-Pattern Methods - Safe Conversion Without Exceptions

using Aloe.Utils.Json;

string json = """{"Name":"John","Age":30}""";

// Try to convert without throwing exceptions
if (json.TryToObj<Person>(out var person))
{
    Console.WriteLine($"Success: {person.Name}");
}
else
{
    Console.WriteLine("Conversion failed");
}

JsonSerializerOptions - Using Custom Options

using System.Text.Json;
using Aloe.Utils.Json;

// Define custom options
var options = new JsonSerializerOptions
{
    WriteIndented = false,
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

// Serialize with custom options
var person = new Person { Name = "John", Age = 30 };
string json = person.ToJson(options);

// Deserialize with custom options
var deserializedPerson = json.ToObj<Person>(options);

// Format with custom options
string formattedJson = json.FormatJson(options);

AOT/Trimming-Compatible Usage

When using Native AOT or trimming, define a JsonSerializerContext and use the overloads that accept JsonTypeInfo.

using System.Text.Json.Serialization;
using Aloe.Utils.Json;

// Define a JsonSerializerContext
[JsonSerializable(typeof(Person))]
internal partial class AppJsonContext : JsonSerializerContext
{
}

// Serialize an object to JSON (AOT-compatible)
var person = new Person { Name = "John", Age = 30 };
string json = person.ToJson(AppJsonContext.Default.Person);

// Deserialize JSON to an object (AOT-compatible)
var deserializedPerson = json.ToObj(AppJsonContext.Default.Person);

// Try-pattern methods (AOT-compatible)
if (json.TryToObj(AppJsonContext.Default.Person, out var aotPerson))
{
    Console.WriteLine($"Success: {aotPerson.Name}");
}

Note

The library uses System.Text.Json under the hood and provides a convenient way to work with JSON data in your .NET applications.

License

MIT License

Contributing

Bug reports and feature requests are welcome on GitHub Issues. Pull requests are also appreciated.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors