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::NamedNodeRef;
use oxrdf::vocab::rdf;
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, the parser might emit broken RDF.
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::NamedNodeRef;
use oxrdf::vocab::rdf;
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::NamedNodeRef;
use oxrdf::vocab::rdf;
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::NamedNodeRef;
use oxrdf::vocab::rdf;
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 moreSource§impl Default for JsonLdParser
impl Default for JsonLdParser
Source§fn default() -> JsonLdParser
fn default() -> JsonLdParser
Auto Trait Implementations§
impl Freeze for JsonLdParser
impl RefUnwindSafe for JsonLdParser
impl Send for JsonLdParser
impl Sync for JsonLdParser
impl Unpin for JsonLdParser
impl UnwindSafe for JsonLdParser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more