Skip to main content

Standard Library: Data Formats (JSON & TOML)

Kyna provides native, high-speed encoding and decoding for standard serialization formats including JSON and TOML without needing third-party libraries.


JSON Processing​

Parsing JSON Strings (jsonParse)​

jsonParse converts a valid JSON string into native Kyna maps, lists, and scalar types:

const rawJson = '{"status":"ok","code":200,"items":["alpha","beta"]}';
const data = jsonParse(rawJson);

print(data["status"]); # "ok"
print(data["code"]); # 200
print(data["items"][0]); # "alpha"

Serializing to JSON (jsonStringify)​

jsonStringify encodes native Kyna objects, maps, and lists into a UTF-8 JSON string:

const payload = {
"service": "kyna-auth",
"timestamp": 1788320000,
"active": true
};

const jsonOutput = jsonStringify(payload);
print(jsonOutput);
# Output: {"service":"kyna-auth","timestamp":1788320000,"active":true}

Handling Malformed JSON​

When jsonParse() encounters invalid syntax, it throws an Error with error code K5100:

try {
const invalid = jsonParse("{ invalid_json: true ");
} catch (err) {
print("Failed to parse JSON!");
print("Error code:", err.code); # K5100
print("Error message:", err.message);
}

TOML Support​

Kyna includes tomlParse() / toml.stringify and xmlParse() / xml.stringify for configuration and markup. Each XML element is { name, attributes, text, children }.

const tomlConfig = """
[server]
host = "0.0.0.0"
port = 8080

[database]
max_connections = 50
""";

const config = tomlParse(tomlConfig);
print("Server Host:", config["server"]["host"]);
print("Max DB Conns:", config["database"]["max_connections"]);