scjson

scjson logo

scjson Python Package

This directory contains the Python implementation of scjson, a format for representing SCXML state machines in JSON. The package provides a command line interface and utility functions to convert between .scxml and .scjson files and to validate documents against the project’s schema.

The package includes pydantic and dataclasses types for the associated objects / enums in both standard and strict forms.

For details on how SCXML elements are inferred during conversion see INFERENCE.md. In python, inference for conversion is handled by the dataclasses models. See below.

Installation

pip install scjson

You can also install from a checkout of this repository:

cd py && pip install -e .

Source Code - Multi-Language Support

[https://github.com/SoftOboros/scjson/]

Python Engine — User Guide

For end‑to‑end usage of the Python execution engine (tracing, comparing against a reference, generating vectors, sweeping corpora), see:

Online: https://github.com/SoftOboros/scjson/blob/main/docs/ENGINE-PY.md

SCION Reference Dependency

Several comparison tests (py/tests/test_exec_compare_advanced.py) and the exec_compare tooling invoke the Node-based SCION runner bundled under tools/scion-runner. Node.js must be able to resolve the SCION packages (scxml, jsdom, and regenerator-runtime) via its module loader. Install them once before running comparisons:

cd tools/scion-runner
npm ci  # or npm install

When running the Python tests or CLI comparisons, ensure node can load these modules (for example by keeping the installation above in place or by adding their location to NODE_PATH). Without the SCION packages, comparisons fall back to the Python engine.

Command Line Usage

After installation the scjson command is available:

# Convert a single file
scjson json path/to/machine.scxml

# Convert back to SCXML
scjson xml path/to/machine.scjson - o path/to/output.scxml

# Validate recursively
scjson validate path/to/dir -r

# Genrate typescript Types
scjson  typescript -o dir/of/output

# Genrate scjson.schema.json
scjson  schema -o dir/of/output

FastAPI example Usage

This is a minimal FastAPI endpoint as an example usage of the SCXMLDocumentHandler class.

import json
from fastapi import FastAPI, Request, HTTPException, Response
from scjson.SCXMLDocumentHandler import SCXMLDocumentHandler

app = FastAPI()
handler = SCXMLDocumentHandler(schema_path=None)

# In-memory store for demo
store = {}

@app.get("/xml/{slug}")
async def get_xml(slug: str):
    """Return the SCXML document as XML."""
    data = store.get(slug)
    if not data:
        raise HTTPException(status_code=404, detail="Document not found")
    xml_str = handler.json_to_xml(json.dumps(data))
    return Response(content=xml_str, media_type="application/xml")

@app.post("/xml/{slug}")
async def post_xml(slug: str, request: Request):
    """Accept an SCXML document and convert it to scjson."""
    xml_bytes = await request.body()
    xml_str = xml_bytes.decode("utf-8")
    json_str = handler.xml_to_json(xml_str)
    data = json.loads(json_str)
    data.setdefault("name", slug)
    store[slug] = data
    return data

Importing Objects.

This imports the definitions of individual types. See below for lib variats. Class varaints available for pydantic and dataclasses implementing both the standard and strict xsd variants.

from scjson.pydantic import Scxml, State, Transition, Onentry # etc.

SCJSON Caveats

The SCXML conversion helpers normalize data so it can be stored as JSON. During asdict() serialization the generated dataclasses may contain Decimal values and enumeration instances (e.g. AssignTypeDatatype).

These conversions allow the JSON representation to be serialized by json.dumps and then converted back via the _to_dataclass helper.

Known Issues

None at this time.

Operational conformance testing is performed via uber_test.py

/py# python uber_test.py -l python 2>&1 | tee test.log

Note: uber_test.py applies all scxml files in Zhornyak’s ScxmlEditor-Tutorial which provides a robest set of scxml test vectors useful for standard compliance verification. This is the only file in the test suite which fails to verify round-trip.

Uber Test Harness

Run across all languages or a single language with alias support:

# All languages detected on PATH
python py/uber_test.py

# Single language (aliases allowed): py, python, js, ts, javascript, rs, rust, swift, java, csharp
python py/uber_test.py -l js
python py/uber_test.py -l swfit   # typo tolerated → swift

# Limit the corpus and treat consensus as warnings only
python py/uber_test.py -l swift -s "Examples/Qt/StopWatch/*.scxml" --consensus-warn

Model Variants

The Python package exposes four sets of generated models that mirror the SCJSON schema. They all share the same field names and enumerations, but offer different runtime characteristics.

Enums

Each enumeration represents a restricted string set used by SCXML. The values shown below mirror those defined in the SCJSON schema.

Common Types

Several generated classes share generic helper fields:

scjson.dataclasses

Plain Python dataclasses without runtime validation.

scjson.dataclasses_strict

The same dataclasses as above but configured for stricter type checking.

scjson.pydantic

Pydantic BaseModel classes generated from the SCJSON schema. They provide data validation and convenient .model_dump() helpers.

scjson.pydantic_strict

Pydantic models with strict validation settings.

Other Resources

github: [https://github.com/SoftOboros/scjson]

git clone https://github.com/SoftOboros/scjson.git

git clone git@github.com:SoftOboros/scjson.git

gh repo clone SoftOboros/scjson

npm: [https://www.npmjs.com/package/scjson]

npm install scjson

cargo: [https://crates.io/crates/scjson]

cargo install scjson

dockerhub: [https://hub.docker.com/r/iraa/scjson] (Full development environment for all supported languages)

docker pull iraa/scjson:latest

License

All source code in this directory is released under the BSD 1-Clause license. See LICENSE and LEGAL.md for details.