jsonld-streaming-parser
Version:
A fast and lightweight streaming JSON-LD parser
321 lines (320 loc) • 14.7 kB
TypeScript
import { JsonLdContextNormalized } from "jsonld-context-parser";
import * as RDF from "@rdfjs/types";
import { AnnotationsBufferEntry, ParsingContext } from "./ParsingContext";
/**
* Utility functions and methods.
*/
export declare class Util {
static readonly XSD: string;
static readonly XSD_BOOLEAN: string;
static readonly XSD_INTEGER: string;
static readonly XSD_DOUBLE: string;
static readonly RDF: string;
readonly dataFactory: RDF.DataFactory<RDF.BaseQuad>;
readonly rdfFirst: RDF.NamedNode;
readonly rdfRest: RDF.NamedNode;
readonly rdfNil: RDF.NamedNode;
readonly rdfType: RDF.NamedNode;
readonly rdfJson: RDF.NamedNode;
private readonly parsingContext;
constructor(options: {
parsingContext: ParsingContext;
dataFactory?: RDF.DataFactory<RDF.BaseQuad>;
});
/**
* Helper function to get the value of a context entry,
* or fallback to a certain value.
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param {string} contextKey A pre-defined JSON-LD key in context entries.
* @param {string} key A context entry key.
* @param {string} fallback A fallback value for when the given contextKey
* could not be found in the value with the given key.
* @return {string} The value of the given contextKey in the entry behind key in the given context,
* or the given fallback value.
*/
static getContextValue<FB>(context: JsonLdContextNormalized, contextKey: string, key: string, fallback: FB): string | any | FB;
/**
* Get the container type of the given key in the context.
*
* Should any context-scoping bugs should occur related to this in the future,
* it may be required to increase the offset from the depth at which the context is retrieved by one (to 2).
* This is because containers act 2 levels deep.
*
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param {string} key A context entry key.
* @return {string} The container type.
*/
static getContextValueContainer(context: JsonLdContextNormalized, key: string): {
[typeName: string]: boolean;
};
/**
* Get the value type of the given key in the context.
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param {string} key A context entry key.
* @return {string} The node type.
*/
static getContextValueType(context: JsonLdContextNormalized, key: string): string | null;
/**
* Get the language of the given key in the context.
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param {string} key A context entry key.
* @return {string} The node type.
*/
static getContextValueLanguage(context: JsonLdContextNormalized, key: string): string | null;
/**
* Get the direction of the given key in the context.
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param {string} key A context entry key.
* @return {string} The node type.
*/
static getContextValueDirection(context: JsonLdContextNormalized, key: string): string;
/**
* Check if the given key in the context is a reversed property.
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param {string} key A context entry key.
* @return {boolean} If the context value has a @reverse key.
*/
static isContextValueReverse(context: JsonLdContextNormalized, key: string): boolean;
/**
* Get the @index of the given key in the context.
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param {string} key A context entry key.
* @return {string} The index.
*/
static getContextValueIndex(context: JsonLdContextNormalized, key: string): any | null;
/**
* Check if the given key refers to a reversed property.
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param {string} key The property key.
* @param {string} parentKey The parent key.
* @return {boolean} If the property must be reversed.
*/
static isPropertyReverse(context: JsonLdContextNormalized, key: string, parentKey: string): boolean;
/**
* Check if the given key exists inside an embedded node as direct child.
* @param {string} parentKey The parent key.
* @return {boolean} If the property is embedded.
*/
static isPropertyInEmbeddedNode(parentKey: string): boolean;
/**
* Check if the given key exists inside an annotation object as direct child.
* @param {string} parentKey The parent key.
* @return {boolean} If the property is an annotation.
*/
static isPropertyInAnnotationObject(parentKey: string): boolean;
/**
* Check if the given IRI is valid.
* @param {string} iri A potential IRI.
* @return {boolean} If the given IRI is valid.
*/
static isValidIri(iri: string | null): boolean;
/**
* Check if the given first array (needle) is a prefix of the given second array (haystack).
* @param needle An array to check if it is a prefix.
* @param haystack An array to look in.
*/
static isPrefixArray(needle: string[], haystack: string[]): boolean;
/**
* Make sure that @id-@index pairs are equal over all array values.
* Reject otherwise.
* @param {any[]} value An array value.
* @return {Promise<void>} A promise rejecting if conflicts are present.
*/
validateValueIndexes(value: any[]): Promise<void>;
/**
* Convert a given JSON value to an RDF term.
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param {string} key The current JSON key.
* @param value A JSON value.
* @param {number} depth The depth the value is at.
* @param {string[]} keys The path of keys.
* @return {Promise<RDF.Term[]>} An RDF term array.
*/
valueToTerm(context: JsonLdContextNormalized, key: string, value: any, depth: number, keys: string[]): Promise<RDF.Term[]>;
/**
* If the context defines a property-scoped context for the given key,
* that context will be returned.
* Otherwise, the given context will be returned as-is.
*
* This should be used for valueToTerm cases that are not objects.
* @param context A context.
* @param key A JSON key.
*/
getContextSelfOrPropertyScoped(context: JsonLdContextNormalized, key: string): Promise<JsonLdContextNormalized>;
/**
* If the given term is null, return an empty array, otherwise return an array with the single given term.
* @param term A term.
*/
nullableTermToArray(term: RDF.Term | null): RDF.Term[];
/**
* Convert a given JSON key to an RDF predicate term,
* based on @vocab.
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param key A JSON key.
* @return {RDF.NamedNode} An RDF named node.
*/
predicateToTerm(context: JsonLdContextNormalized, key: string): RDF.Term | null;
/**
* Convert a given JSON key to an RDF resource term or blank node,
* based on @base.
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param key A JSON key.
* @return {RDF.NamedNode} An RDF named node or null.
*/
resourceToTerm(context: JsonLdContextNormalized, key: string): RDF.NamedNode | RDF.BlankNode | null;
/**
* Convert a given JSON key to an RDF resource term.
* It will do this based on the @vocab,
* and fallback to @base.
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param key A JSON key.
* @return {RDF.NamedNode} An RDF named node or null.
*/
createVocabOrBaseTerm(context: JsonLdContextNormalized, key: string): RDF.Term | null;
/**
* Ensure that the given value becomes a string.
* @param {string | number} value A string or number.
* @param {NamedNode} datatype The intended datatype.
* @return {string} The returned string.
*/
intToString(value: string | number, datatype: RDF.NamedNode | null): string;
/**
* Convert a given JSON string value to an RDF term.
* @param {number} depth The current stack depth.
* @param {JsonLdContextNormalized} context A JSON-LD context.
* @param {string} key The current JSON key.
* @param {string} value A JSON value.
* @param {NamedNode} defaultDatatype The default datatype for the given value.
* @return {RDF.Term} An RDF term or null.
*/
stringValueToTerm(depth: number, context: JsonLdContextNormalized, key: string, value: string | number, defaultDatatype: RDF.NamedNode | null): RDF.Term | null;
/**
* Create a literal for the given value with the given language and direction.
* Auxiliary quads may be emitted.
* @param {number} depth The current stack depth.
* @param {string} value A string value.
* @param {string} language A language tag.
* @param {string} direction A direction.
* @return {Term} An RDF term.
*/
createLanguageDirectionLiteral(depth: number, value: string, language: string | null, direction: string): RDF.Term;
/**
* Stringify the given JSON object to a canonical JSON string.
* @param value Any valid JSON value.
* @return {string} A canonical JSON string.
*/
valueToJsonString(value: any): string;
/**
* If the key is not a keyword, try to check if it is an alias for a keyword,
* and if so, un-alias it.
* @param {string} key A key, can be falsy.
* @param {string[]} keys The path of keys.
* @param {number} depth The depth to
* @param {boolean} disableCache If the cache should be disabled
* @param {JsonLdContextNormalized} context A context to unalias with,
* will fallback to retrieving the context for the given keys.
* @return {Promise<string>} A promise resolving to the key itself, or another key.
*/
unaliasKeyword(key: any, keys: string[], depth: number, disableCache?: boolean, context?: JsonLdContextNormalized): Promise<any>;
/**
* Unalias the keyword of the parent.
* This adds a safety check if no parent exist.
* @param {any[]} keys A stack of keys.
* @param {number} depth The current depth.
* @return {Promise<any>} A promise resolving to the parent key, or another key.
*/
unaliasKeywordParent(keys: any[], depth: number): Promise<any>;
/**
* Un-alias all keywords in the given hash.
* @param {{[p: string]: any}} hash A hash object.
* @param {string[]} keys The path of keys.
* @param {number} depth The depth.
* @param {JsonLdContextNormalized} context A context to unalias with,
* will fallback to retrieving the context for the given keys.
* @return {Promise<{[p: string]: any}>} A promise resolving to the new hash.
*/
unaliasKeywords(hash: {
[id: string]: any;
}, keys: string[], depth: number, context?: JsonLdContextNormalized): Promise<{
[id: string]: any;
}>;
/**
* Check if we are processing a literal (including JSON literals) at the given depth.
* This will also check higher levels,
* because if a parent is a literal,
* then the deeper levels are definitely a literal as well.
* @param {any[]} keys The keys.
* @param {number} depth The depth.
* @return {boolean} If we are processing a literal.
*/
isLiteral(keys: any[], depth: number): Promise<boolean>;
/**
* Check how many parents should be skipped for checking the @graph for the given node.
*
* @param {number} depth The depth of the node.
* @param {any[]} keys An array of keys.
* @return {number} The graph depth offset.
*/
getDepthOffsetGraph(depth: number, keys: any[]): Promise<number>;
/**
* Check if the given subject is of a valid type.
* This should be called when applying @reverse'd properties.
* @param {Term} subject A subject.
*/
validateReverseSubject(subject: RDF.Term): void;
/**
* Get the default graph.
* @return {Term} An RDF term.
*/
getDefaultGraph(): RDF.NamedNode | RDF.BlankNode | RDF.DefaultGraph;
/**
* Get the current graph, while taking into account a graph that can be defined via @container: @graph.
* If not within a graph container, the default graph will be returned.
* @param keys The current keys.
* @param depth The current depth.
*/
getGraphContainerValue(keys: any[], depth: number): Promise<RDF.NamedNode | RDF.BlankNode | RDF.DefaultGraph>;
/**
* Get the properties depth for retrieving properties.
*
* Typically, the properties depth will be identical to the given depth.
*
* The following exceptions apply:
* * When the parent is @reverse, the depth is decremented by one.
* * When @nest parents are found, the depth is decremented by the number of @nest parents.
* If in combination with the exceptions above an intermediary array is discovered,
* the depth is also decremented by this number of arrays.
*
* @param keys The current key chain.
* @param depth The current depth.
*/
getPropertiesDepth(keys: any[], depth: number): Promise<number>;
/**
* Get the key for the current container entry.
* @param key A key, can be falsy.
* @param keys The key chain.
* @param depth The current depth to get the key from.
* @return Promise resolving to the key.
* Null will be returned for @none entries, with aliasing taken into account.
*/
getContainerKey(key: any, keys: string[], depth: number): Promise<any>;
/**
* Check if no reverse properties are present in embedded nodes.
* @param key The current key.
* @param reverse If a reverse property is active.
* @param isEmbedded If we're in an embedded node.
*/
validateReverseInEmbeddedNode(key: string, reverse: boolean, isEmbedded: boolean): void;
/**
* Emit a quad, with checks.
* @param depth The current depth.
* @param subject S
* @param predicate P
* @param object O
* @param graph G
* @param reverse If a reverse property is active.
* @param isEmbedded If we're in an embedded node.
*/
emitQuadChecked(depth: number, subject: RDF.Term, predicate: RDF.Term, object: RDF.Term, graph: RDF.Term, reverse: boolean, isEmbedded: boolean): void;
protected emitAnnotation(depth: number, quad: RDF.BaseQuad, annotation: AnnotationsBufferEntry): void;
}