ToonNet.Extensions.Json 3.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ToonNet.Extensions.Json --version 3.2.0
                    
NuGet\Install-Package ToonNet.Extensions.Json -Version 3.2.0
                    
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="ToonNet.Extensions.Json" Version="3.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ToonNet.Extensions.Json" Version="3.2.0" />
                    
Directory.Packages.props
<PackageReference Include="ToonNet.Extensions.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 ToonNet.Extensions.Json --version 3.2.0
                    
#r "nuget: ToonNet.Extensions.Json, 3.2.0"
                    
#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 ToonNet.Extensions.Json@3.2.0
                    
#: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=ToonNet.Extensions.Json&version=3.2.0
                    
Install as a Cake Addin
#tool nuget:?package=ToonNet.Extensions.Json&version=3.2.0
                    
Install as a Cake Tool

ToonNet.Extensions.Json

JSON โ†” TOON format conversion extension

.NET NuGet Downloads Status


๐Ÿ“ฆ What is ToonNet.Extensions.Json?

ToonNet.Extensions.Json provides seamless bidirectional conversion between JSON and TOON formats:

  • โœ… JSON โ†’ TOON - Convert JSON strings/documents to TOON format
  • โœ… TOON โ†’ JSON - Convert TOON strings/documents to JSON format
  • โœ… System.Text.Json integration - Familiar API patterns
  • โœ… Preserves structure - Round-trip conversions keep objects, arrays and values (numbers keep their exact value)
  • โœ… Developer-friendly - Static ToonConvert (string-based) and ToonJsonConverter (document-based) helpers

Perfect for:

  • ๐Ÿค– AI/LLM Applications - Convert JSON APIs to token-efficient TOON
  • ๐Ÿ”„ Data Migration - Transform existing JSON data to TOON format
  • ๐Ÿ”— Interoperability - Work with JSON-based systems
  • ๐Ÿ“Š API Integration - Accept JSON, process as TOON, return JSON

๐Ÿš€ Quick Start

Installation

# Core package (required)
dotnet add package ToonNet.Core

# JSON extension
dotnet add package ToonNet.Extensions.Json

Basic Usage - String Conversion

using ToonNet.Core.Serialization;
using ToonNet.Extensions.Json;

// JSON โ†’ TOON string conversion
string jsonString = """
{
  "name": "Alice",
  "age": 30,
  "hobbies": ["reading", "coding"]
}
""";

string toonString = ToonConvert.FromJson(jsonString);

// Output (TOON format):
// name: Alice
// age: 30
// hobbies[2]: reading,coding

// TOON โ†’ JSON string conversion
string jsonBack = ToonConvert.ToJson(toonString);

Object Serialization via JSON

using ToonNet.Extensions.Json;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public List<string> Hobbies { get; set; }
}

var person = new Person 
{ 
    Name = "Bob", 
    Age = 25, 
    Hobbies = new List<string> { "gaming", "music" }
};

// Serialize C# object to JSON
string json = ToonConvert.SerializeToJson(person);

// Deserialize JSON to C# object (plain System.Text.Json, case-sensitive by default)
var personBack = ToonConvert.DeserializeFromJson<Person>(json);

// One-step: JSON string โ†’ C# object via TOON.
// TOON keys must match the property names; the camelCase policy maps "name" to Name.
var person2 = ToonConvert.ParseJson<Person>(jsonString,
    new ToonSerializerOptions { PropertyNamingPolicy = PropertyNamingPolicy.CamelCase });

๐Ÿ“– API Reference

String Format Conversion

// JSON string โ†’ TOON string
string toon = ToonConvert.FromJson(jsonString);
string toon = ToonConvert.FromJson(jsonString, options);  // ToonOptions

// TOON string โ†’ JSON string
string json = ToonConvert.ToJson(toonString);
string json = ToonConvert.ToJson(toonString, writerOptions);  // JsonWriterOptions
string json = ToonConvert.ToJson(toonString, writerOptions, toonOptions);  // ToonOptions for parsing the TOON input

Document Conversion (Low-level)

using ToonNet.Extensions.Json;

// JSON string โ†’ ToonDocument (options: MaxDepth, and StrictMode for duplicate keys)
ToonDocument doc = ToonJsonConverter.FromJson(jsonString);
ToonDocument doc = ToonJsonConverter.FromJson(jsonString, new ToonOptions { StrictMode = false });

// JsonElement โ†’ ToonDocument
ToonDocument doc = ToonJsonConverter.FromJson(jsonElement);

// ToonDocument โ†’ JSON string
string json = ToonJsonConverter.ToJson(document);
string json = ToonJsonConverter.ToJson(document, writerOptions);  // JsonWriterOptions

// ToonValue โ†’ JSON string
string json = ToonJsonConverter.ToJson(toonValue);
string json = ToonJsonConverter.ToJson(toonValue, writerOptions);  // JsonWriterOptions

Object Serialization

SerializeToJson and DeserializeFromJson are thin wrappers over System.Text.Json.JsonSerializer (TOON is not involved; SerializeToJson indents when no options are passed).

// C# object โ†’ JSON string
string json = ToonConvert.SerializeToJson<T>(obj);
string json = ToonConvert.SerializeToJson<T>(obj, options);  // JsonSerializerOptions

// JSON string โ†’ C# object
T obj = ToonConvert.DeserializeFromJson<T>(jsonString);
T obj = ToonConvert.DeserializeFromJson<T>(jsonString, options);  // JsonSerializerOptions

// JSON string โ†’ TOON โ†’ C# object (one step)
T obj = ToonConvert.ParseJson<T>(jsonString);
T obj = ToonConvert.ParseJson<T>(jsonString, options);  // ToonSerializerOptions

๐ŸŽฏ Real-World Examples

Example 1: AI/LLM Token Optimization

using ToonNet.Core.Serialization;
using ToonNet.Extensions.Json;

// Receive JSON from API
string apiResponse = await httpClient.GetStringAsync("/api/products");

// Convert to TOON (fewer tokens for LLM)
string toonData = ToonConvert.FromJson(apiResponse);

// Use in LLM prompt
string prompt = $"""
You are a product analyst. Here is the product catalog:

{toonData}

Recommend the best products for a software developer.
""";

// Uniform arrays of objects become TOON tables, which usually take fewer tokens than JSON

Example 2: Data Migration

// Load existing JSON configuration
string jsonConfig = File.ReadAllText("appsettings.json");

// Convert to TOON format
string toonConfig = ToonConvert.FromJson(jsonConfig);

// Save as TOON (more human-readable)
File.WriteAllText("appsettings.toon", toonConfig);

// Later: Load TOON and convert back if needed
string toonContent = File.ReadAllText("appsettings.toon");
var config = ToonSerializer.Deserialize<AppSettings>(toonContent);

Example 3: Roundtrip Verification

// Original JSON
string originalJson = """{"discount": 35.00, "active": true}""";

// JSON โ†’ TOON โ†’ JSON
string toonString = ToonConvert.FromJson(originalJson);
string roundtripJson = ToonConvert.ToJson(toonString);

// Normalize both for comparison
var original = JsonSerializer.Deserialize<object>(originalJson);
var roundtrip = JsonSerializer.Deserialize<object>(roundtripJson);

// Semantic equivalence preserved (format may differ)
// roundtripJson is {"discount":35,"active":true}

๐Ÿ”„ Format Conversion Behavior

Type Mapping

JSON Type TOON Type Notes
object ToonObject Key-value pairs
array ToonArray Ordered items
string ToonString UTF-8 text
number ToonNumber Exact decimal when it fits, otherwise double
true/false ToonBoolean Boolean values
null ToonNull Null/undefined
  • FromJson limits nesting with ToonOptions.MaxDepth (default 100; the root is at depth 0, as in TOON), accepts comments and trailing commas (as in appsettings.json), and rejects duplicate keys with a JsonException in strict mode (the default); with StrictMode = false the last value wins.
  • ToJson writes non-ASCII text as is ("ร‡aฤŸrฤฑ", not "\u00C7a\u011Fr\u0131"); HTML-sensitive characters such as < stay escaped. Pass your own JsonWriterOptions to change the encoder or indentation.

Semantic Equivalence

Important: Format conversions preserve semantic equivalence, not exact formatting:

// JSON: {"price": 35.00}
// TOON: price: 35
// JSON (roundtrip): {"price":35}  โ† Format differs, value identical

JSON numbers are converted with the TOON number rules: the exact value is kept when it fits in a decimal (so 12345678901234567890 and 1299.99 round-trip unchanged), smaller or larger magnitudes use the nearest double, and a number that is not finite as a double (e.g. 1e400) is kept as a string. As TOON spec ยง2 requires, numbers are written in canonical form, so 35.00 becomes 35 and 1.0 becomes 1 (same value). ToonJsonConverter.FromJson(string) limits the nesting to the MaxDepth of the ToonOptions (default 100, the root at depth 0) and accepts comments and trailing commas, as in appsettings.json files.

See Roundtrip Guarantees for details.


๐Ÿ”’ Thread-Safety

  • ToonSerializer and JSON conversion methods are safe to call concurrently across threads.
  • Shared metadata/name caches use ConcurrentDictionary for concurrent access.
  • Cache entries are created on demand and retained for the process lifetime (no eviction).
  • Do not mutate a single ToonSerializerOptions instance concurrently across threads.

Core:

Other Extensions:

Web Integration:

Development:


๐Ÿ“š Documentation


๐Ÿงช Testing

# Run JSON conversion tests
cd tests/ToonNet.Tests
dotnet test --filter "FullyQualifiedName~ToonJsonConverter"

๐Ÿ“„ License

MIT License - See LICENSE file for details.


๐Ÿค Contributing

Contributions welcome! Please read CONTRIBUTING.md first.


Part of the ToonNet serialization library family.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  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 is compatible.  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
4.0.1 56 10/21/2026
4.0.0 130 10/15/2026
3.3.0 83 10/14/2026
3.2.0 82 10/14/2026
3.1.0 83 10/14/2026
3.0.0 108 10/13/2026
2.0.0 85 10/8/2026
1.4.0 1,208 2/8/2026
1.3.0 132 2/4/2026
1.2.0 141 2/1/2026
1.1.0 145 1/28/2026
1.0.0 153 1/12/2026