跳至主要內容

快速開始

本指南涵蓋了最高效的使用路徑:

  1. 序列化與反序列化物件
  2. 產生 T-JSON 輸出
  3. 處理表格資料與 Arrow
  4. 在格式之間轉碼

1. 物件來回轉換

import ttoon

data = {"name": "Alice", "age": 30, "id": "A-001"}

text = ttoon.dumps(data)
print(text)
# name: "Alice"
# age: 30
# id: "A-001"

restored = ttoon.loads(text)

2. 產生 T-JSON

T-JSON 使用類似 JSON 的 {} / [] 括號,同時在值層面保留具型別的語法。

import datetime as dt
import ttoon

text = ttoon.to_tjson({
"created_at": dt.datetime(2026, 3, 8, 10, 30, 0),
"score": 12.5,
})
print(text)
# {"created_at": 2026-03-08T10:30:00, "score": 12.5}

3. 表格資料與 Arrow

當資料是由統一物件組成的列表時,T-TOON 會自動輸出表格格式:

[2]{name,score}:
"Alice", 95
"Bob", 87
import polars as pl
import ttoon

df = pl.DataFrame({"name": ["Alice", "Bob"], "score": [95, 87]})

text = ttoon.dumps(df)
table = ttoon.read_arrow(text) # 回傳 pyarrow.Table
  • dumps(df) 內部會將其轉換為 Arrow,然後透過 Rust 核心進行序列化 — 沒有 list[dict] 中間產物
  • read_arrow() 直接回傳一個 pyarrow.Table

4. 直接轉碼

在 T-JSON 和 T-TOON 之間轉換,無需具現化為特定語言的原生物件 — 文字僅通過 Rust IR。

import ttoon

ttoon_text = ttoon.tjson_to_ttoon('{"name": "Alice", "scores": [95, 87]}')
tjson_text = ttoon.ttoon_to_tjson('name: "Alice"\nage: 30')

下一步