kapps_ogm.node.node_serializer#

Serialization utilities for Node instances to RDF and JSON-LD formats.

kapps_ogm.node.node_serializer.to_triples(self: Node) → set[Triple][source]#

Serialize the nodes current instance into RDF triples for persistence.

Uses the in-memory materialized instance and cached data. Updates in memory will be reflected in the serialized triples.

Parameters:

node – The Node instance to serialize.

Returns:

RDF triples representing this node, including nested objects.

Return type:

set[Triple]

Raises:

RuntimeError – If the node has no ClassSpec or IRI.

Notes

  • Intended for persisting the current instance state.

  • Always uses the cached instance; does not refresh from the database.

kapps_ogm.node.node_serializer.to_json_ld(self: Node, context: Dict[str, str] | None = None) → Dict[str, Any][source]#

Serialize the nodes instance into JSON-LD format.

Converts RDF triples to JSON-LD with proper handling of IRIs, blank nodes, and literals. Blank nodes are inlined recursively for cleaner output.

Parameters:
  • node – The Node instance to serialize.

  • context – Optional @context dictionary mapping prefixes to namespace URIs. If provided, URIs are compacted using these prefixes.

Returns:

JSON-LD document with @context and @graph keys.

Main subject appears first in @graph, followed by other named nodes. Blank nodes are inlined as nested objects.

Return type:

Dict[str, Any]

Notes

  • Calls to_triples() internally to obtain RDF representation

  • Literal datatypes are preserved as native Python types

  • Multiple property values are represented as JSON arrays

  • Context prefixes are registered globally in IRI.PREFIXES