Build a Bi‑Directional TOON Parser in C#: Convert TOON JSON with Ease

Published: (December 8, 2025 at 03:49 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Introduction

Toon (Token‑Oriented Object Notation) is a newly promoted format designed specifically for saving LLM tokens.
It is compact, human‑readable, extremely lightweight, and optimized for AI workflows where every token counts.

Compared to JSON, TOON reduces noise, avoids unnecessary braces/quotes, and stores structured data in a flat and token‑efficient way. With the rise of AI prompting and agent‑based architectures, TOON is quickly becoming a favorite among developers and ML engineers.

In this guide you will learn how to build a complete bi‑directional TOON parser in C#:

  • TOON → JSON
  • JSON → TOON
  • Fully automatic, scalable, and easy to extend

The solution works on .NET 6, .NET 8, and .NET 9, and requires no external dependencies.

Step 1: Create the ToonConverter Class

We will build a utility class that contains:

  • ParseToon() – converts TOON into a C# dictionary
  • ToonToJson() – converts TOON into JSON
  • JsonToToon() – converts JSON back into TOON

Create a new file named ToonConverter.cs and insert the following code:

using System.Text.Json;
using System.Text.Json.Nodes;

public static class ToonConverter
{
    // --------------------------------------
    // 1. TOON → OBJECT (Dictionary)
    // --------------------------------------
    public static Dictionary>> ParseToon(string toon)
    {
        var result = new Dictionary>>();
        var lines = toon.Split('\n', StringSplitOptions.RemoveEmptyEntries)
                        .Select(x => x.Trim()).ToList();

        int i = 0;

        while (i  f.Trim()).ToArray();

            var rows = new List>();

            while (i  v.Trim()).ToArray();

                var obj = new Dictionary();
                for (int f = 0; f  x.Key).ToList();

            sb.Append($"{name}[{arr.Count}]{{");
            sb.Append(string.Join(",", fields));
            sb.AppendLine("}:");

            // Add rows
            foreach (JsonNode? row in arr)
            {
                var obj = row!.AsObject();
                sb.AppendLine(string.Join(",", fields.Select(f => obj[f]!.ToString())));
            }

            sb.AppendLine();
        }

        return sb.ToString().Trim();
    }
}

Step 2: Try TOON → JSON Conversion

Example TOON format

Characters[3]{Name,Age,Role}:
Alice,25,Warrior
Bob,30,Mage
Charlie,22,Rogue

Items[2]{ItemName,Quantity,Price}:
Sword,5,100
Potion,20,10

Convert it

string toonData = @"
 Characters[3]{Name,Age,Role}:
 Alice,25,Warrior
 Bob,30,Mage
 Charlie,22,Rogue

 Items[2]{ItemName,Quantity,Price}:
 Sword,5,100
 Potion,20,10
";

string json = ToonConverter.ToonToJson(toonData);
Console.WriteLine(json);

Output

TOON to JSON result

Formatted JSON

The JSON is clean, structured, and ready for APIs or storage.

Step 3: JSON → TOON Conversion

Given JSON

{
  "Characters": [
    {
      "Name": "Alice",
      "Age": "25",
      "Role": "Warrior"
    },
    {
      "Name": "Bob",
      "Age": "30",
      "Role": "Mage"
    },
    {
      "Name": "Charlie",
      "Age": "22",
      "Role": "Rogue"
    }
  ],
  "Items": [
    {
      "ItemName": "Sword",
      "Quantity": "5",
      "Price": "100"
    },
    {
      "ItemName": "Potion",
      "Quantity": "20",
      "Price": "10"
    }
  ]
}

Convert it back

string toonFromJson = ToonConverter.JsonToToon(json);
Console.WriteLine(toonFromJson);

Output

JSON to TOON result

The round‑trip conversion works perfectly.

Step 4: Integrate Into Your Project

This parser is well‑suited for:

  • AI agents
  • LLM prompt optimization
  • Memory‑efficient data transfer
  • Chat‑based structured data
  • Internal DSLs
  • Config files for AI workflows

Because TOON is extremely tiny, your prompts stay clean and token‑friendly.

Summary

In this article we covered how to:

  • Create a lightweight bi‑directional TOON parser
  • Convert TOON → JSON
  • Convert JSON → TOON
  • Handle multi‑block, multi‑row structured data

TOON is fast becoming the JSON‑alternative for the AI era, and having your own parser gives you maximum flexibility for future workflows.

Back to Blog

Related posts

Read more »