Skip to main content

TokioAsyncReaderJsonLdParser

Struct TokioAsyncReaderJsonLdParser 

Source
pub struct TokioAsyncReaderJsonLdParser<R: AsyncRead + Unpin> { /* private fields */ }
Available on crate feature async-tokio only.
Expand description

Parses a JSON-LD file from a AsyncRead implementation.

Can be built using JsonLdParser::for_tokio_async_reader.

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);

Implementations§

Source§

impl<R: AsyncRead + Unpin> TokioAsyncReaderJsonLdParser<R>

Source

pub async fn next(&mut self) -> Option<Result<Quad, JsonLdParseError>>

Reads the next quad or returns None if the file is finished.

Source

pub fn prefixes(&self) -> JsonLdPrefixesIter<'_>

The list of IRI prefixes considered at the current step of the parsing.

This method returns (prefix name, prefix value) tuples. It is empty at the beginning of the parsing and gets updated when prefixes are encountered. It should be full at the end of the parsing (but if a prefix is overridden, only the latest version will be returned).

use oxjsonld::JsonLdParser;

let file = r#"{
    "@context": {"schema": "http://schema.org/", "@base": "http://example.com/"},
    "@type": "schema:Person",
    "@id": "foo",
    "schema:name": "Foo"
}"#;

let mut parser = JsonLdParser::new().for_tokio_async_reader(file.as_bytes());
assert_eq!(parser.prefixes().collect::<Vec<_>>(), []); // No prefix at the beginning

parser.next().await.unwrap()?; // We read the first quad
assert_eq!(
    parser.prefixes().collect::<Vec<_>>(),
    [("schema", "http://schema.org/")]
); // There are now prefixes
//
Source

pub fn base_iri(&self) -> Option<&str>

The base IRI considered at the current step of the parsing.

use oxjsonld::JsonLdParser;

let file = r#"{
    "@context": {"schema": "http://schema.org/", "@base": "http://example.com/"},
    "@type": "schema:Person",
    "@id": "foo",
    "schema:name": "Foo"
}"#;

let mut parser = JsonLdParser::new().for_tokio_async_reader(file.as_bytes());
assert!(parser.base_iri().is_none()); // No base at the beginning because none has been given to the parser.

parser.next().await.unwrap()?; // We read the first quad
assert_eq!(parser.base_iri(), Some("http://example.com/")); // There is now a base IRI.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V