Skip to main content

High-performance serializer/parser for TOON-LD (Token-Oriented Object Notation for Linked Data)

Project description

toon-ld

High-performance Python bindings for TOON-LD (Token-Oriented Object Notation for Linked Data).

Overview

TOON-LD extends TOON (Token-Oriented Object Notation) to handle Linked Data (RDF). It combines the token-saving "Tabular Arrays" of TOON with the @context expansion of JSON-LD.

This package provides Python bindings for the Rust implementation, offering excellent performance for serialization and parsing operations.

Installation

pip install toon-ld

Usage

Converting Between Formats

import toon_ld

# Convert JSON-LD to TOON-LD
json_str = '{"name": "Alice", "age": 30}'
toon_str = toon_ld.convert_jsonld_to_toonld(json_str)
print(toon_str)
# Output:
# age: 30
# name: Alice

# Convert TOON-LD back to JSON-LD
json_back = toon_ld.convert_toonld_to_jsonld(toon_str)
print(json_back)

Working with Python Objects

import toon_ld

# Serialize Python dict to TOON-LD
data = {
    "users": [
        {"id": 1, "name": "Alice"},
        {"id": 2, "name": "Bob"}
    ]
}
toon_str = toon_ld.serialize_to_toon(data)
print(toon_str)
# Output:
# users[2]{id,name}:
#   1, Alice
#   2, Bob

# Parse TOON-LD to Python dict
parsed = toon_ld.parse_toon(toon_str)
print(parsed["users"][0]["name"])  # Alice

Validation

import toon_ld

# Validate TOON-LD
if toon_ld.validate_toon("name: Alice"):
    print("Valid TOON-LD!")

# Validate JSON
if toon_ld.validate_json('{"name": "Alice"}'):
    print("Valid JSON!")

API Reference

convert_jsonld_to_toonld(json_str: str) -> str

Convert a JSON-LD string to TOON-LD format.

Args:

  • json_str: A JSON or JSON-LD formatted string

Returns: TOON-LD formatted string

Raises: ValueError if the input is not valid JSON

convert_toonld_to_jsonld(toon_str: str) -> str

Convert a TOON-LD string to JSON-LD format.

Args:

  • toon_str: A TOON-LD formatted string

Returns: JSON-LD formatted string (pretty-printed)

Raises: ValueError if the input is not valid TOON-LD

parse_toon(toon_str: str) -> dict

Parse a TOON-LD string to a Python dictionary.

Args:

  • toon_str: A TOON-LD formatted string

Returns: Python dictionary representing the parsed data

Raises: ValueError if the input is not valid TOON-LD

serialize_to_toon(data: Any) -> str

Serialize a Python dictionary to TOON-LD format.

Args:

  • data: A Python dictionary or JSON-serializable object

Returns: TOON-LD formatted string

Raises: ValueError if the input cannot be serialized

validate_toon(toon_str: str) -> bool

Validate a TOON-LD string.

Args:

  • toon_str: A TOON-LD formatted string

Returns: True if valid, False otherwise

validate_json(json_str: str) -> bool

Validate a JSON string.

Args:

  • json_str: A JSON formatted string

Returns: True if valid, False otherwise

TOON-LD Format

Tabular Arrays

Arrays of objects with identical keys are serialized compactly:

users[2]{id,name}:
  1, Alice
  2, Bob

Primitive Arrays

Simple value arrays use inline format:

tags[3]: rust, python, wasm

Quoting Rules

Strings are unquoted unless they contain special characters (,, :, |) or start/end with whitespace:

message: Hello World
note: "Hello, World"

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

toon_ld-0.2.1.tar.gz (35.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

toon_ld-0.2.1-cp38-abi3-macosx_11_0_arm64.whl (835.3 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file toon_ld-0.2.1.tar.gz.

File metadata

  • Download URL: toon_ld-0.2.1.tar.gz
  • Upload date:
  • Size: 35.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.10.2

File hashes

Hashes for toon_ld-0.2.1.tar.gz
Algorithm Hash digest
SHA256 229917e40248a5e823555b60a062622fc1c9263d96a470a7b0e3866e15d72cfd
MD5 bb46929ddceed5657f818cdb3c430f87
BLAKE2b-256 312c34b2ce0906ffaa9cec3650bb0445b148d61366e638f91c05bced5e6fc17b

See more details on using hashes here.

File details

Details for the file toon_ld-0.2.1-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for toon_ld-0.2.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eef148bb762cc5909346514c17d246b86a4be05c8184901688ad0c9c3e18d6e6
MD5 f3f1855c48ebbc81a0512ae4ace47e01
BLAKE2b-256 53017887030b0776ee3e646fbd9e9e60e76e093b7e0587973f7c2505026a6b8f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page