Skip to content

plaid.utils.cgns_json

plaid.utils.cgns_json

JSON serialization helpers for CGNS trees.

The helpers in this module serialize a single pyCGNS-style tree node of the form [name, value, children, label] to a language-neutral JSON payload. Node values are encoded with :mod:plaid.utils.json_codec, which stores NumPy arrays as base64 little-endian C-contiguous bytes with explicit dtype and shape metadata so the payload can be decoded from Python, MATLAB, R, JavaScript, or any language with base64 and typed-array support.

plaid.utils.cgns_json.cgns_tree_to_json_payload

cgns_tree_to_json_payload(tree)

Convert a CGNS tree to a JSON-compatible payload.

Parameters:

  • tree (list[Any]) –

    pyCGNS-style node [name, value, children, label].

Returns:

  • dict[str, Any]

    A JSON-compatible dictionary containing format metadata and the encoded

  • dict[str, Any]

    tree.

Source code in plaid/utils/cgns_json.py
def cgns_tree_to_json_payload(tree: list[Any]) -> dict[str, Any]:
    """Convert a CGNS tree to a JSON-compatible payload.

    Args:
        tree: pyCGNS-style node ``[name, value, children, label]``.

    Returns:
        A JSON-compatible dictionary containing format metadata and the encoded
        tree.
    """
    return {
        "format": FORMAT_NAME,
        "version": FORMAT_VERSION,
        "tree": _encode_node(tree),
    }

plaid.utils.cgns_json.cgns_tree_from_json_payload

cgns_tree_from_json_payload(payload)

Rebuild a CGNS tree from a JSON-compatible payload.

Parameters:

  • payload (dict[str, Any]) –

    Payload produced by :func:cgns_tree_to_json_payload.

Returns:

  • list[Any]

    A pyCGNS-style node [name, value, children, label].

Raises:

  • ValueError

    If the payload format or version is unsupported.

Source code in plaid/utils/cgns_json.py
def cgns_tree_from_json_payload(payload: dict[str, Any]) -> list[Any]:
    """Rebuild a CGNS tree from a JSON-compatible payload.

    Args:
        payload: Payload produced by :func:`cgns_tree_to_json_payload`.

    Returns:
        A pyCGNS-style node ``[name, value, children, label]``.

    Raises:
        ValueError: If the payload format or version is unsupported.
    """
    if payload.get("format") != FORMAT_NAME:
        raise ValueError(f"Unsupported CGNS JSON format: {payload.get('format')!r}")
    if payload.get("version") != FORMAT_VERSION:
        raise ValueError(f"Unsupported CGNS JSON version: {payload.get('version')!r}")
    return _decode_node(payload["tree"])

plaid.utils.cgns_json.cgns_tree_to_json

cgns_tree_to_json(tree, **json_kwargs)

Convert a CGNS tree to a JSON string.

Parameters:

  • tree (list[Any]) –

    pyCGNS-style node [name, value, children, label].

  • **json_kwargs (Any, default: {} ) –

    Extra keyword arguments forwarded to :func:json.dumps.

Returns:

  • str

    A JSON string containing the encoded CGNS tree.

Source code in plaid/utils/cgns_json.py
def cgns_tree_to_json(tree: list[Any], **json_kwargs: Any) -> str:
    """Convert a CGNS tree to a JSON string.

    Args:
        tree: pyCGNS-style node ``[name, value, children, label]``.
        **json_kwargs: Extra keyword arguments forwarded to :func:`json.dumps`.

    Returns:
        A JSON string containing the encoded CGNS tree.
    """
    return json.dumps(cgns_tree_to_json_payload(tree), **json_kwargs)

plaid.utils.cgns_json.cgns_tree_from_json

cgns_tree_from_json(text)

Rebuild a CGNS tree from a JSON string.

Parameters:

  • text (str) –

    JSON string produced by :func:cgns_tree_to_json.

Returns:

  • list[Any]

    A pyCGNS-style node [name, value, children, label].

Source code in plaid/utils/cgns_json.py
def cgns_tree_from_json(text: str) -> list[Any]:
    """Rebuild a CGNS tree from a JSON string.

    Args:
        text: JSON string produced by :func:`cgns_tree_to_json`.

    Returns:
        A pyCGNS-style node ``[name, value, children, label]``.
    """
    payload = json.loads(text)
    return cgns_tree_from_json_payload(payload)