Skip to main content

Crate pg_jsonl

Crate pg_jsonl 

Source
Expand description

§pg-jsonl

Crates.io Downloads Documentation CI License

An owned, streaming parser for property-graph JSON Lines (PG-JSONL): one JSON object per line describing a node or an edge.

Two dialects are accepted on every line, so files may mix them:

  • the plain dialect
    {"type":"node","id":"alice","labels":["Person"],"properties":{"name":"Alice","age":30}}
    {"type":"edge","source":"alice","target":"bob","label":"KNOWS","properties":{"since":2020}}
  • Neo4j APOC apoc.export.json
    {"type":"relationship","id":"7","label":"KNOWS","start":{"id":"0","labels":["Person"]},"end":{"id":"1"},"properties":{}}

Identifiers may be JSON strings or numbers; they are kept as text. Property values are rendered as text with the JSON type as a hint (string, int, float, boolean, or json for arrays and objects); null properties are skipped.

§Why this crate

The PgJsonlStreamParser API processes one physical line at a time and keeps nothing between lines but a counter, so memory stays bounded for large exports and callers decide where records go. The convenience parse_pg_jsonl function is available when collecting the complete result is appropriate.

The crate depends on no database driver, graph store, RDF model, or application framework. The optional property-graph-model feature adds field-for-field From conversions into that crate’s PgNode / PgEdge.

§Quick start

use pg_jsonl::parse_pg_jsonl;

let input = r#"{"type":"node","id":"alice","labels":["Person"],"properties":{"name":"Alice"}}
{"type":"edge","source":"alice","target":"bob","label":"KNOWS"}
"#;
let (nodes, edges) = parse_pg_jsonl(input)?;

assert_eq!(nodes[0].id, "alice");
assert_eq!(nodes[0].labels, ["Person"]);
assert_eq!(nodes[0].properties[0].name, "name");
assert_eq!(edges[0].label, "KNOWS");

§Streaming large exports

use pg_jsonl::{PgJsonlRecord, PgJsonlStreamParser};

let mut parser = PgJsonlStreamParser::new();
let mut edges = 0;
for line in r#"{"type":"node","id":"a"}
{"type":"edge","source":"a","target":"a","label":"SELF"}
"#.lines() {
    if let Some(PgJsonlRecord::Edge(_)) = parser.parse_line(line)? {
        edges += 1;
    }
}
assert_eq!(edges, 1);

§License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Structs§

Edge
A property-graph edge (relationship).
Node
A property-graph node.
PgJsonlStreamParser
Stateful line-by-line PG-JSONL parser. Memory is bounded by one line: nothing is retained between calls except the line counter.
Property
One property with its value rendered as text and the JSON type it came from as a hint.

Enums§

PgJsonlError
Errors from parsing PG-JSONL files.
PgJsonlRecord
One parsed PG-JSONL line.

Functions§

parse_pg_jsonl
Parse a complete PG-JSONL document into its nodes and edges.
parse_pg_jsonl_with
Parse a complete PG-JSONL document, invoking on_node / on_edge for each record as it is parsed. Memory stays bounded by one line.