Skip to main content

purrdf_json/
lib.rs

1// SPDX-FileCopyrightText: 2026 Blackcat InformaticsĀ® Inc. <paudley@blackcatinformatics.ca>
2// SPDX-License-Identifier: MIT OR Apache-2.0 OR MulanPSL-2.0
3
4//! Ordered JSON as a queryable RDF 1.2 byte cover.
5//!
6//! [`analyze`] validates JSON and borrows its lexical spans; [`project`] writes
7//! the model directly to the shared IR. [`decode_document`] reads an explicitly
8//! selected document, reconstructs its bytes through the kernel cover law, then
9//! reparses them to verify every asserted occurrence, path, kind and identity.
10//! Object order, duplicate keys, whitespace, escapes and number spellings survive.
11//! A profile and vocabulary are always explicit.
12
13#![forbid(unsafe_code)]
14#![doc = include_str!("../README.md")]
15
16mod decode;
17mod error;
18mod model;
19mod parse;
20mod profile;
21mod project;
22
23pub use decode::decode_document;
24pub use error::JsonError;
25pub use model::{Document, Kind, SourceDocument, Value};
26pub use profile::{Bounds, Profile, STANDARD_NAMESPACE, Vocabulary};
27pub use project::{encode, project};
28
29use purrdf_core::{BaseIri, ContentDigest};
30
31/// Validate a source and describe its occurrences without copying source spans.
32pub fn analyze<'a>(
33    source: SourceDocument<'a>,
34    profile: &Profile,
35) -> Result<Document<'a>, JsonError> {
36    BaseIri::parse(source.id).map_err(JsonError::Iri)?;
37    if source.bytes.len() as u64 > profile.bounds().max_source_bytes {
38        return Err(JsonError::Limit {
39            resource: "source bytes",
40            limit: profile.bounds().max_source_bytes,
41        });
42    }
43    let text = core::str::from_utf8(source.bytes).map_err(|error| JsonError::InvalidUtf8 {
44        valid_up_to: error.valid_up_to(),
45    })?;
46    let (values, runs) = parse::parse(text, profile.bounds())?;
47    let digest = ContentDigest::of(source.bytes);
48    let id = profile.document_id(source.id, &digest);
49    Ok(Document {
50        source,
51        text,
52        values,
53        runs,
54        digest,
55        id,
56        profile_id: profile.identity(),
57    })
58}