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>
impl<R: AsyncRead + Unpin> TokioAsyncReaderJsonLdParser<R>
Sourcepub async fn next(&mut self) -> Option<Result<Quad, JsonLdParseError>>
pub async fn next(&mut self) -> Option<Result<Quad, JsonLdParseError>>
Reads the next quad or returns None if the file is finished.
Sourcepub fn prefixes(&self) -> JsonLdPrefixesIter<'_> ⓘ
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
//Sourcepub fn base_iri(&self) -> Option<&str>
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§
impl<R> Freeze for TokioAsyncReaderJsonLdParser<R>where
R: Freeze,
impl<R> RefUnwindSafe for TokioAsyncReaderJsonLdParser<R>where
R: RefUnwindSafe,
impl<R> Send for TokioAsyncReaderJsonLdParser<R>where
R: Send,
impl<R> Sync for TokioAsyncReaderJsonLdParser<R>where
R: Sync,
impl<R> Unpin for TokioAsyncReaderJsonLdParser<R>
impl<R> UnwindSafe for TokioAsyncReaderJsonLdParser<R>where
R: UnwindSafe,
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
Mutably borrows from an owned value. Read more