pub struct JsonLdParser { /* private fields */ }
Expand description
A JSON-LD parser.
The parser is a work in progress. Only JSON-LD 1.0 is supported at the moment. JSON-LD 1.1 is not supported yet.
The parser supports two modes:
- regular JSON-LD parsing that needs to buffer the full file into memory.
- Streaming JSON-LD that can avoid buffering in a few cases.
To enable it call the
with_profile(JsonLdProfile::Streaming)
method.
Count the number of people:
use oxjsonld::JsonLdParser;
use oxrdf::vocab::rdf;
use oxrdf::NamedNodeRef;
let file = br#"{
"@context": {"schema": "http://schema.org/"},
"@graph": [
{
"@type": "schema:Person",
"@id": "http://example.com/foo",
"schema:name": "Foo"
},
{
"@type": "schema:Person",
"schema:name": "Bar"
}
]
}"#;
let schema_person = NamedNodeRef::new("http://schema.org/Person")?;
let mut count = 0;
for quad in JsonLdParser::new().for_reader(file.as_ref()) {
let quad = quad?;
if quad.predicate == rdf::TYPE && quad.object == schema_person.into() {
count += 1;
}
}
assert_eq!(2, count);
Implementations§
Source§impl JsonLdParser
impl JsonLdParser
Sourcepub fn new() -> Self
pub fn new() -> Self
Builds a new JsonLdParser
.
Sourcepub fn lenient(self) -> Self
pub fn lenient(self) -> Self
Assumes the file is valid to make parsing faster.
It will skip some validations.
Note that if the file is actually not valid, broken RDF might be emitted by the parser.
Sourcepub fn with_profile(self, profile: impl Into<JsonLdProfileSet>) -> Self
pub fn with_profile(self, profile: impl Into<JsonLdProfileSet>) -> Self
Assume the given profile(s) during parsing.
If you set the Streaming JSON-LD profile (JsonLdProfile::Streaming
),
the parser will skip some buffering to make parsing faster and memory consumption lower.
use oxjsonld::{JsonLdParser, JsonLdProfile};
use oxrdf::vocab::rdf;
use oxrdf::NamedNodeRef;
let file = br#"{
"@context": {"schema": "http://schema.org/"},
"@graph": [
{
"@type": "schema:Person",
"@id": "http://example.com/foo",
"schema:name": "Foo"
}
]
}"#;
let schema_person = NamedNodeRef::new("http://schema.org/Person")?;
let mut count = 0;
for quad in JsonLdParser::new()
.with_profile(JsonLdProfile::Streaming)
.for_slice(file)
{
let quad = quad?;
if quad.predicate == rdf::TYPE && quad.object == schema_person.into() {
count += 1;
}
}
assert_eq!(1, count);
Sourcepub fn with_base_iri(
self,
base_iri: impl Into<String>,
) -> Result<Self, IriParseError>
pub fn with_base_iri( self, base_iri: impl Into<String>, ) -> Result<Self, IriParseError>
Base IRI to use when expanding the document.
It corresponds to the base
option from the algorithm specification.
Sourcepub fn for_reader<R: Read>(self, reader: R) -> ReaderJsonLdParser<R> ⓘ
pub fn for_reader<R: Read>(self, reader: R) -> ReaderJsonLdParser<R> ⓘ
Parses a JSON-LD file from a Read
implementation.
Count the number of people:
use oxjsonld::JsonLdParser;
use oxrdf::vocab::rdf;
use oxrdf::NamedNodeRef;
let file = br#"{
"@context": {"schema": "http://schema.org/"},
"@graph": [
{
"@type": "schema:Person",
"@id": "http://example.com/foo",
"schema:name": "Foo"
},
{
"@type": "schema:Person",
"schema:name": "Bar"
}
]
}"#;
let schema_person = NamedNodeRef::new("http://schema.org/Person")?;
let mut count = 0;
for quad in JsonLdParser::new().for_reader(file.as_ref()) {
let quad = quad?;
if quad.predicate == rdf::TYPE && quad.object == schema_person.into() {
count += 1;
}
}
assert_eq!(2, count);
Sourcepub fn for_slice(self, slice: &[u8]) -> SliceJsonLdParser<'_> ⓘ
pub fn for_slice(self, slice: &[u8]) -> SliceJsonLdParser<'_> ⓘ
Parses a JSON-LD file from a byte slice.
Count the number of people:
use oxjsonld::JsonLdParser;
use oxrdf::vocab::rdf;
use oxrdf::NamedNodeRef;
let file = br#"{
"@context": {"schema": "http://schema.org/"},
"@graph": [
{
"@type": "schema:Person",
"@id": "http://example.com/foo",
"schema:name": "Foo"
},
{
"@type": "schema:Person",
"schema:name": "Bar"
}
]
}"#;
let schema_person = NamedNodeRef::new("http://schema.org/Person")?;
let mut count = 0;
for quad in JsonLdParser::new().for_slice(file) {
let quad = quad?;
if quad.predicate == rdf::TYPE && quad.object == schema_person.into() {
count += 1;
}
}
assert_eq!(2, count);
Trait Implementations§
Source§impl Clone for JsonLdParser
impl Clone for JsonLdParser
Source§fn clone(&self) -> JsonLdParser
fn clone(&self) -> JsonLdParser
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more