etoon 0.3.0

Fast TOON (Token-Oriented Object Notation) encoder. 8x faster than toons, 2.7x faster than the official TS SDK.
Documentation
{
  "version": "1.5",
  "category": "encode",
  "description": "Key folding with safe mode, depth control, collision avoidance",
  "tests": [
    {
      "name": "encodes folded chain to primitive (safe mode)",
      "input": {
        "a": {
          "b": {
            "c": 1
          }
        }
      },
      "expected": "a.b.c: 1",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4"
    },
    {
      "name": "encodes folded chain with inline array",
      "input": {
        "data": {
          "meta": {
            "items": ["x", "y"]
          }
        }
      },
      "expected": "data.meta.items[2]: x,y",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4"
    },
    {
      "name": "encodes folded chain with tabular array",
      "input": {
        "a": {
          "b": {
            "items": [
              { "id": 1, "name": "A" },
              { "id": 2, "name": "B" }
            ]
          }
        }
      },
      "expected": "a.b.items[2]{id,name}:\n  1,A\n  2,B",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4"
    },
    {
      "name": "skips folding when segment requires quotes (safe mode)",
      "input": {
        "data": {
          "full-name": {
            "x": 1
          }
        }
      },
      "expected": "data:\n  \"full-name\":\n    x: 1",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4"
    },
    {
      "name": "skips folding on sibling literal-key collision (safe mode)",
      "input": {
        "data": {
          "meta": {
            "items": [1, 2]
          }
        },
        "data.meta.items": "literal"
      },
      "expected": "data:\n  meta:\n    items[2]: 1,2\ndata.meta.items: literal",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4",
      "note": "Collision avoidance: folding would create duplicate key"
    },
    {
      "name": "encodes partial folding with flattenDepth=2",
      "input": {
        "a": {
          "b": {
            "c": {
              "d": 1
            }
          }
        }
      },
      "expected": "a.b:\n  c:\n    d: 1",
      "options": {
        "keyFolding": "safe",
        "flattenDepth": 2
      },
      "specSection": "13.4"
    },
    {
      "name": "encodes full chain with flattenDepth=Infinity (default)",
      "input": {
        "a": {
          "b": {
            "c": {
              "d": 1
            }
          }
        }
      },
      "expected": "a.b.c.d: 1",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4"
    },
    {
      "name": "encodes standard nesting with flattenDepth=0 (no folding)",
      "input": {
        "a": {
          "b": {
            "c": 1
          }
        }
      },
      "expected": "a:\n  b:\n    c: 1",
      "options": {
        "keyFolding": "safe",
        "flattenDepth": 0
      },
      "specSection": "13.4",
      "note": "flattenDepth=0 disables all folding"
    },
    {
      "name": "encodes standard nesting with flattenDepth=1 (no practical effect)",
      "input": {
        "a": {
          "b": {
            "c": 1
          }
        }
      },
      "expected": "a:\n  b:\n    c: 1",
      "options": {
        "keyFolding": "safe",
        "flattenDepth": 1
      },
      "specSection": "13.4",
      "note": "flattenDepth=1 has no practical folding effect (requires at least 2 segments)"
    },
    {
      "name": "encodes standard nesting with keyFolding=off (baseline)",
      "input": {
        "a": {
          "b": {
            "c": 1
          }
        }
      },
      "expected": "a:\n  b:\n    c: 1",
      "options": {
        "keyFolding": "off"
      },
      "specSection": "13.4"
    },
    {
      "name": "encodes folded chain ending with empty object",
      "input": {
        "a": {
          "b": {
            "c": {}
          }
        }
      },
      "expected": "a.b.c:",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4"
    },
    {
      "name": "stops folding at array boundary (not single-key object)",
      "input": {
        "a": {
          "b": [1, 2]
        }
      },
      "expected": "a.b[2]: 1,2",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4"
    },
    {
      "name": "folds @-prefixed keys (AWS CloudWatch style)",
      "input": {
        "@timestamp": "2026-04-06T12:00:00Z",
        "@message": {
          "level": "ERROR",
          "msg": "timeout"
        },
        "requestId": "abc-123"
      },
      "expected": "@timestamp: \"2026-04-06T12:00:00Z\"\n@message:\n  level: ERROR\n  msg: timeout\nrequestId: abc-123",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4",
      "note": "@message has 2 children so no fold; @timestamp is scalar so no fold either — but both participate in fold eligibility"
    },
    {
      "name": "folds single-child @-prefixed key into dot-notation",
      "input": {
        "@log": {
          "detail": {
            "code": 500
          }
        }
      },
      "expected": "@log.detail.code: 500",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4",
      "note": "@log → detail → code is a single-key chain; @-prefixed folded key is a valid identifier"
    },
    {
      "name": "folds $-prefixed keys (MongoDB/JSON Schema style)",
      "input": {
        "$schema": "https://json-schema.org/draft/2020-12/schema",
        "$ref": {
          "definitions": {
            "User": {"type": "object"}
          }
        }
      },
      "expected": "$schema: \"https://json-schema.org/draft/2020-12/schema\"\n$ref.definitions.User.type: object",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4",
      "note": "$ref → definitions → User is a single-key chain; $-prefixed folded key is a valid identifier"
    },
    {
      "name": "folds #-prefixed keys (JSON-LD/Azure style)",
      "input": {
        "#context": {
          "vocab": "https://schema.org/"
        },
        "#comment": "internal metadata",
        "id": 42
      },
      "expected": "#context.vocab: \"https://schema.org/\"\n#comment: internal metadata\nid: 42",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4",
      "note": "#context → vocab is a single-key chain; #-prefixed folded key is a valid identifier"
    },
    {
      "name": "encodes folded chains preserving sibling field order",
      "input": {
        "first": {
          "second": {
            "third": 1
          }
        },
        "simple": 2,
        "short": {
          "path": 3
        }
      },
      "expected": "first.second.third: 1\nsimple: 2\nshort.path: 3",
      "options": {
        "keyFolding": "safe"
      },
      "specSection": "13.4"
    }
  ]
}