knora-jsonld-simplify
v1.0.22
Published
Simplify JSON-LD returned from Knora
Downloads
10
Readme
KnoraJsonldSimplify – a Simplifier for the "simple" JSON-LD returned by Knora
KnoraJsonldSimplify is a small TypeScript module that simplifies the output of the Knora V2 API, notably the
results from a resource or gravsearch query. Usage is very simple.
Let's assume that the variable jsonld
contains the result of a gravsearch query either as
JSON string or as an object:
let simplifier = new KnoraJsonldSimplify(jsonld);
const result = simplifier.simplify();
The result is an array of KnoraResource
-instances:
Installation source coude
- download it from github
yarn install
to install and compileyarn test
to perform the testing
Installation npm
Knora-jsonld-simplify is now available as npm module
npm install knora-jsonld-simplify
In a angular projects
npm add knora.jsonld-simplify
should work...
Helpers
export enum KnoraCalendar { GREGORIAN, JULIAN, ISLAMIC, JEWISH }
export enum KnoraCalendarEra { BC, CE }
KnoraResource class
Member variables
A KnoraResource object provides the following member variables that are accessible readonly:
iri
: [readonly] The Knora IRI of the resourcetype
: [readonly] The type of the resource as defined by the ontology, e.g., mls:Lemmalabel
: [readonly] The label of the resource
Member Methods
In order to access the properties of the resource, the following methods are provided:
- getNProps(): Returns the number of properties that the resource actually has.
getNProps(): number
- getNValues(): Return the number of values available of the given property.
getNValues(propertyname: string) : number
- isValue(): Is True if thee value of the given property name and given index is of type KnoraValue,
That is, it points to a property value (and is not a link to another resource)
isValue(propertyname: string, index: number)
- getValue(): Returns an KnoraValue instance.
getValue(propertyname: string, index: number)
: Returns a KnoraValue instance. - isResource(): Is True if thee value of the given property name and given index is of type KnoraResource,
that is, it is a link property that points to another resource. This resource may be empty (just providing
the IRI without property values or it can be a full resource with properties values)
isResource(propertyname: string, index: number)
- getResource(): Returns a Knora_Resource (the property must be a link property)
getResource(propname: string, index?: number) : KnoraResource
KnoraValue class
This class provides access to the property values. Usually KnoraValue it is subclassed to reflect the different data types that Knora provides. The following sublcasses are provided:
- KnoraTextValue (text values)
- KnoraIntValue (integer values)
- KnoraListValue (list node value)
- KnoraColorValue (Knora color value)
- KnoraDecimalValue (decimal value)
- KnoraUriValue (URI value)
- KnoraBooleanValue (boolean value)
- KnoraGeomValue (geometry value for region)
- KnoraGeonameValue (geoname value)
- KnoraIntervalValue (interval value)
- KnoraDateValue (Knora date value)
- KnoraStillImageFileValue (still image file value)
Member variables (also available in subclasses)
- iri: string [readonly]: The IRI of this value
- subtype: string [readonly]: Get string with subclass name (e.g. "KnoraTextValue")
- strval: string [readonly]: The string representation of the value (available for all value types!)
- comment: strong : The comment associated with a value
Member methods (also available in subclasses)
- stringify: Returns a descriptive string representation (e.g. for logging etc.)
stringify(indent: string = ''): string
KnoraTextValue
Represents a text value. This can be a richtext with standoff markup!
Member variables
- strval: string: Text string or XML
- mapping: string: The IRI of the XML mapping (see standoff of Knora)
KnoraIntValue
Represents an integer value
Member variables
- ival: number: Returns the integer value
KnoraListValue
Represents a node from a list
Member variables
- nodeIri: string: The IRI of the list node
- label: string: The label of the nodse if available, otherwise undefined
KnoraColorValue
Represents a color value
Member variables
- color: string: The color in HTML notation
KnoraDecimalValue
Represents a decimal value
Member variables
- decimal: number: The decimal value
KnoraUriValue
Represents an URI
Member variables
- uri: string: An URI
KnoraBooleanValue
Represents a boolean
Member variables
- bool: boolean: A boolean value
KnoraGeomValue
Represents a geometry value of a region. This is a JSON string.
Member variables
- geom: string: A geometry value as JSON string
KnoraGeonameValue
Represents a geographical location as given by a code by geonames.org
Member variables
- geonameCode: string: Geonames.org geo code
KnoraIntervalValue
Represents a time interval
Member variables
- start: number: Start time in seconds (including fractions of seconds)
- end: number: End time in seconds (including fractions of seconds)
KnoraDateValue
Implemensts the Knora calendar date representation
Member variables
- calendar: KnoraCalendar: The calendar, either
- KnoraCalendar.GREGORIAN
- KnoraCalendar.JULIAN
- KnoraCalendar.ISLAMIC (NYI!)
- KnoraCalendar.JEWISH (NYI!)
- startEra: KnoraCalendarEra: Era of start date, either
- KnoraCalendarEra.BC
- KnoraCalendarEra.CE
- startYear: number: Start year
- startMonth: number: Start month
- startDay: number: Start day
- endEra: KnoraCalendarEra: Era of end date, see startEra
- endYear: number: End year
- endMonth: number: End month
- endDay: number: End day
KnoraStillImageFileValue
Implements the still image value of a Knora StillImageResource
Member variables
- strval: string: A IIIF compatible URL to access the full image as JPEG
- filename: string: The filename/file-ID
- baseUrl: string: The base URL of the IIIF server providing the image
- dimX: number: X dimension of image (in pixels)
- dimY: number: Y dimension of image (in pixels)
Example
// create the simplyfier instance
let simplifier = new KnoraJsonldSimplify();
// simplify the JSON from knora. We get an array of resources
const result = simplifier.simplify(json);
// get the IRI of the first resource
let iri = result[0].iri
// get the label of the first resource
let label = result[0].label
// get the type of the first resource, eg. "kpt:restype1"
let type = result[0].type
// check if there is a property "kpt:titleprop"
if (result[0].isValue("kpt:titleprop")) {
// get the string representation of the first value
let strval = result[0].getValue("kpt:titleprop").strval
}
// Check if kpt:linkpropValue is a Resource (should be because its a link)
if (result[0].isResource("kpt:linkpropValue")) {
let res = result[0].getResource("kpt:linkpropValue")
let restype = res.type; // type of resource
let resiri = res.iri; // IRI of resource
let nprops = res.getNProps(); // see if the resource has been retrieved completely
}