pub struct JsonLdParser { /* private fields */ }Expand description
A JSON-LD parser.
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 = r#"{
"@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_bytes()) {
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 = r#"{
"@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_processing_mode(self, processing_mode: JsonLdProcessingMode) -> Self
pub fn with_processing_mode(self, processing_mode: JsonLdProcessingMode) -> Self
Set the processing mode of the parser.
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 = r#"{
"@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_bytes()) {
let quad = quad?;
if quad.predicate == rdf::TYPE && quad.object == schema_person.into() {
count += 1;
}
}
assert_eq!(2, count);Sourcepub fn for_tokio_async_reader<R: AsyncRead + Unpin>(
self,
reader: R,
) -> TokioAsyncReaderJsonLdParser<R>
Available on crate feature async-tokio only.
pub fn for_tokio_async_reader<R: AsyncRead + Unpin>( self, reader: R, ) -> TokioAsyncReaderJsonLdParser<R>
async-tokio only.Parses a JSON-LD file from a AsyncRead implementation.
Count the number of people:
use oxjsonld::JsonLdParser;
use oxrdf::NamedNodeRef;
use oxrdf::vocab::rdf;
let file = r#"{
"@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;
let mut parser = JsonLdParser::new().for_tokio_async_reader(file.as_bytes());
while let Some(quad) = parser.next().await {
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: &(impl AsRef<[u8]> + ?Sized),
) -> SliceJsonLdParser<'_> ⓘ
pub fn for_slice( self, slice: &(impl AsRef<[u8]> + ?Sized), ) -> 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 = r#"{
"@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