ralphtq/elm-rdf/1.0.0

Rdf.Core.Exporter

Convert an Elm Quad into the JSON wire shape produced and consumed by the TypeScript companion package @ralphtq/elm-rdf-n3-quads. Use this when your Elm code constructs quads that need to be sent through a port to the TS side (e.g. for writeQuads, shaclValidateWithQuads).

About the wire types

The three type aliases here (JSONrdfQuad, JSONrdfObject, JSONrdfTermTypeAndValue) are structurally identical to the record type aliases of the same name in ralphtq/elm-rdf-n3-quads (modules Rdf.Wire.Types / Rdf.Wire.Decode / Rdf.Wire.Encode). Because Elm uses structural typing for record aliases, values produced here can be passed wherever a Rdf.Wire.Types.JSONrdfQuad is expected, without any conversion. They're defined here so this package has no hard dependency on the wire package — useful while both packages are pre-publish.

type alias JSONrdfQuad =
{ subject : JSONrdfTermTypeAndValue
, predicate : JSONrdfTermTypeAndValue
, object : JSONrdfObject
, graph : JSONrdfTermTypeAndValue
}

Wire shape for a complete RDF quad.

type alias JSONrdfObject =
{ termType : String
, value : String
, language : Maybe String
, datatype : Maybe JSONrdfTermTypeAndValue
, id : String
}

Wire shape for an RDF object term. Adds optional language and datatype fields for literals.

{ termType : String
, value : String
, id : String
}

Wire shape for a named-node, blank-node, or default-graph marker. For the default graph the TS side sends { termType = "NamedNode", value = "", id = "" }.

Bulk conversion

transformJSONquadsToN3Quads : List Quad -> List JSONrdfQuad

Convert a list of Elm Quads to wire JSONrdfQuads.

-- e.g. before sending to a port:
quads
    |> Rdf.Core.Exporter.transformJSONquadsToN3Quads
    |> sendQuadsToPort

Single-term converters

Exposed so consumers can mix and match — e.g. send a single triple rather than a list.

quadGraphToWire : QuadGraph -> JSONrdfTermTypeAndValue

Convert a QuadGraph to its wire shape. The default graph becomes { termType = "NamedNode", value = "", id = "" }.

predicateToWire : PredicateTerm -> JSONrdfTermTypeAndValue

Convert a PredicateTerm to its wire shape (always termType = "NamedNode").

nonLiteralToWire : RDFnonLiteralTerm -> JSONrdfTermTypeAndValue

Convert a non-literal RDF term (named node or blank node) to its wire shape. Used for the subject position.

rdfTermToWireObject : RDFterm -> JSONrdfObject

Convert an RDFterm to a wire JSONrdfObject.

  • NonLiteral (NamedNode iri) → NamedNode object
  • NonLiteral (BlankNode bn) → BlankNode object
  • Literal (LiteralString s (Just lang)) → Literal with language and rdf:langString datatype
  • Literal (LiteralString s Nothing) → Literal with rdf:langString datatype
  • Literal (LiteralDataType s (Just (DataType iri))) → typed literal
  • Literal (LiteralDataType s Nothing) → "Unhandled" fallback
  • Predicate (PredicateNamedNode iri) → NamedNode (predicate-as-object)
  • other → "Unexpected" fallback

The "Unhandled" and "Unexpected" fallbacks are intentional escape hatches to avoid silent data loss; the TS side ignores them in writers and validators.