2using System.Collections.Generic;
4using System.Threading.Tasks;
21 private readonly
object parsedJson;
23 private readonly
string text;
24 private readonly
string blankNodeIdPrefix;
25 private DateTimeOffset? date =
null;
26 private int blankNodeIndex = 0;
37 this.parsedJson = Doc;
40 this.blankNodeIdPrefix = BlankNodeIdPrefix;
68 public static Task<JsonLdDocument>
CreateAsync(
string Text, Uri BaseUri,
string BlankNodeIdPrefix)
113 public static Task<JsonLdDocument>
CreateAsync(
object Doc,
string Text, Uri BaseUri,
string BlankNodeIdPrefix)
129 await Result.Parse(Doc,
null, BaseUri, Result.CreateBlankNode());
136 return new BlankNode(this.blankNodeIdPrefix + Guid.NewGuid().ToString());
138 return new BlankNode(this.blankNodeIdPrefix + (++this.blankNodeIndex).
ToString());
141 private UriNode CreateUriNode(
string Reference, Uri BaseUri, JsonLdContext Context)
143 return new UriNode(this.CreateUri(Reference, BaseUri, Context), Reference);
146 private UriNode CreateUriNode(
string Reference, Uri BaseUri,
string ShortName, JsonLdContext Context)
148 return new UriNode(this.CreateUri(Reference, BaseUri, Context), ShortName);
151 private Uri CreateUri(
string Reference, Uri BaseUri, JsonLdContext Context)
153 ExpandPrefixes(ref Reference, Context);
154 BaseUri = Context?.Base ?? BaseUri;
158 if (Uri.TryCreate(Reference, UriKind.Absolute, out Uri URI))
165 if (
string.IsNullOrEmpty(Reference))
167 else if (Uri.TryCreate(BaseUri, Reference, out Uri URI))
170 throw this.
ParsingException(
"Invalid URI: " + Reference +
" (base: " + BaseUri.ToString() +
")");
176 return new Exception(Message);
179 private async Task<ISemanticElement> Parse(
object Doc, JsonLdContext Context, Uri BaseUri,
ISemanticElement Subject)
183 if (Doc is Dictionary<string, object> DocObj)
191 foreach (KeyValuePair<string, object> P
in DocObj)
196 if (!TryGetContextName(Name, Context, out
object ContextValue))
198 else if (ContextValue is
string Alias)
201 if (Name.StartsWith(
"@"))
206 Context = await this.GetContext(Value, BaseUri, Context);
211 ??
throw this.
ParsingException(
"Unsupported @id value: " + Value.ToString());
213 Subject = this.CreateUriNode(s, BaseUri, Context);
217 Context = await this.AddType(Subject, Value, BaseUri, Context);
221 await this.Parse(P.Value, Context, BaseUri, Subject);
229 ??
throw this.
ParsingException(
"Unsupported @base value: " + Value.ToString());
231 BaseUri = this.CreateUri(s, BaseUri, Context);
235 ReturnValue = await this.ParseValue(BaseUri, Value, Context);
247 if (ContextValue is
null)
249 if (Uri.TryCreate(Name, UriKind.Absolute, out Uri UriValue))
252 ParsedUri = UriValue;
258 s = ContextValue as string;
263 if (!TryGetContextName(Name, Context, out ContextValue))
267 if (ContextValue is Dictionary<string, object> ContextValueObj)
269 foreach (KeyValuePair<string, object> P2
in ContextValueObj)
274 s = P2.Value as
string
275 ??
throw this.
ParsingException(
"Unsupported @id value: " + P2.Value.ToString());
278 ExpandPrefixes(ref Name, Context);
282 s = P2.Value as
string
283 ??
throw this.
ParsingException(
"Unsupported @type value: " + P2.Value.ToString());
288 Value = this.CreateUri(Value?.
ToString(), BaseUri, Context);
292 Uri TypeUri = this.CreateUri(s, BaseUri, Context);
299 s = P2.Value as
string
300 ??
throw this.
ParsingException(
"Unsupported @vocab value: " + P2.Value.ToString());
302 ParsedUri = this.CreateUri(s, BaseUri, Context);
306 s = P2.Value as
string
307 ??
throw this.
ParsingException(
"Unsupported @base value: " + P2.Value.ToString());
309 BaseUri = this.CreateUri(s, BaseUri, Context);
316 JsonLdContext ScopedContext = await this.GetContext(P2.Value, BaseUri, Context);
317 if (!(Context is
null))
318 ScopedContext.Append(Context);
320 Context = ScopedContext;
328 if (ParsedUri is
null && !(Context.Vocabulary is
null))
329 ParsedUri = this.CreateUri(Name, Context.Vocabulary, Context);
332 if (!(ParsedUri is
null) || Uri.TryCreate(BaseUri, Name, out ParsedUri))
336 if (ParsedValue is
null)
338 if (Value is Array ValueArray)
340 foreach (
object ValueItem
in ValueArray)
342 ParsedValue = await this.ParseValue(BaseUri, ValueItem, Context);
348 ParsedValue = await this.ParseValue(BaseUri, Value, Context);
358 else if (Doc is Array DocArray)
360 foreach (
object DocItem
in DocArray)
361 await this.Parse(DocItem, Context, BaseUri, this.CreateBlankNode());
367 private async Task<JsonLdContext> GetContext(
object Value, Uri BaseUri, JsonLdContext Context)
369 if (Value is Dictionary<string, object> ContextObj)
370 return new JsonLdContext(ContextObj, BaseUri);
371 else if (Value is Array ContextArray)
373 JsonLdContext Result =
new JsonLdContext();
375 foreach (
object ContextItem
in ContextArray)
377 JsonLdContext Result2 = await this.GetContext(ContextItem, BaseUri, Context);
378 Result.Append(Result2, BaseUri);
388 if (Value is
string Url)
389 ContextUri = this.CreateUri(Url, BaseUri, Context);
390 else if (Value is Uri Uri2)
395 if (contextObjects.TryGetValue(ContextUri.AbsolutePath, out KeyValuePair<DateTimeOffset, Dictionary<string, object>> P2))
404 ContextDoc.parsedJson is Dictionary<string, object> ContextObj2 &&
405 ContextObj2.TryGetValue(
"@context", out Obj) &&
406 Obj is Dictionary<string, object> ContextObj3)
408 Context =
new JsonLdContext(ContextObj3, BaseUri);
409 contextObjects[ContextUri.AbsolutePath] =
new KeyValuePair<DateTimeOffset, Dictionary<string, object>>(
410 ContextDoc.Date ?? DateTimeOffset.Now, ContextObj3);
413 Context =
new JsonLdContext(P2.Value, BaseUri);
417 Context =
new JsonLdContext(P2.Value, BaseUri);
426 ContextDoc.parsedJson is Dictionary<string, object> ContextObj2 &&
427 ContextObj2.TryGetValue(
"@context", out Obj) &&
428 Obj is Dictionary<string, object> ContextObj3)
430 Context =
new JsonLdContext(ContextObj3, BaseUri);
431 contextObjects[ContextUri.AbsolutePath] =
new KeyValuePair<DateTimeOffset, Dictionary<string, object>>(
432 ContextDoc.Date ?? DateTimeOffset.Now, ContextObj3);
442 private async Task<JsonLdContext> AddType(
ISemanticElement Subject,
object Value, Uri BaseUri, JsonLdContext Context)
444 if (Value is
string s)
446 if (TryGetContextName(s, Context, out
object ContextValue))
448 Context = await this.AddType(Subject, ContextValue, BaseUri, Context);
453 else if (Value is Dictionary<string, object> TypeObj)
455 foreach (KeyValuePair<string, object> P
in TypeObj)
460 Context = await this.AddType(Subject, P.Value, BaseUri, Context);
464 JsonLdContext ScopedContext = await this.GetContext(P.Value, BaseUri, Context);
465 if (!(Context is
null))
466 ScopedContext.Append(Context);
468 Context = ScopedContext;
473 else if (Value is Array A)
475 foreach (
object Item
in A)
476 await this.AddType(Subject, Item, BaseUri, Context);
484 private static bool ExpandPrefixes(ref
string Name, JsonLdContext Context)
489 int i = Name.IndexOf(
':');
493 string Prefix = Name.Substring(0, i);
495 if (!Context.TryGetObjectValue(Prefix, out
object Value))
498 if (Value is
string PrefixUrl)
500 Name = PrefixUrl + Name.Substring(i + 1);
504 if (Value is Dictionary<string, object> Obj)
506 foreach (KeyValuePair<string, object> P
in Obj)
511 PrefixUrl = P.Value as string;
512 if (!(PrefixUrl is
null))
514 Name = PrefixUrl + Name.Substring(i + 1);
525 private static bool TryGetContextName(
string Name, JsonLdContext Context, out
object Value)
527 if (Name.StartsWith(
"@"))
539 if (ExpandPrefixes(ref Name, Context))
545 if (Context.TryGetObjectValue(Name, out Value))
548 if (!(Context.Vocabulary is
null) && Name.IndexOf(
':') < 0)
550 Value = Context.Vocabulary.AbsoluteUri + Name;
558 private async Task<ISemanticElement> ParseValue(Uri BaseUri,
object Value, JsonLdContext Context)
560 if (Value is Dictionary<string, object> Obj)
564 foreach (KeyValuePair<string, object> P
in Obj)
569 if (P.Value is
string s)
570 Object = this.CreateUriNode(s, BaseUri, Context);
576 s = P.Value as string;
578 BaseUri = this.CreateUri(s, BaseUri, Context);
587 Object = this.CreateBlankNode();
589 return await this.Parse(Obj, Context, BaseUri, Object) ?? Object;
591 else if (Value is Uri Uri)
592 return new UriNode(Uri, Uri.ToString());
605 public string Text => this.text;
610 public DateTimeOffset?
Date => this.date;
618 this.date = HttpResponse.Headers.Date;
619 return Task.CompletedTask;
Helps with parsing of commong data types.
static string EncodeRfc822(DateTime Timestamp)
Encodes a date and time, according to RFC 822 §5.
Static class managing encoding and decoding of internet content.
static Task< object > GetAsync(Uri Uri, params KeyValuePair< string, string >[] Headers)
Gets a resource, given its URI.
Helps with common JSON-related tasks.
static object Parse(string Json)
Parses a JSON string.
override void Add(ISemanticTriple Triple)
Adds a triple to the cube.
Encoder and Decoder of semantic information in JSON-LD Documents.
const string DefaultContentType
application/ld+json
Contains semantic information stored in a JSON-LD document.
DateTimeOffset? Date
Server timestamp of document.
static Task< JsonLdDocument > CreateAsync(string Text, Uri BaseUri)
Contains semantic information stored in a turtle document.
static Task< JsonLdDocument > CreateAsync(object Doc, string Text, Uri BaseUri)
Contains semantic information stored in a turtle document.
string Text
Text representation.
static Task< JsonLdDocument > CreateAsync(string Text, Uri BaseUri, string BlankNodeIdPrefix)
Contains semantic information stored in a turtle document.
static Task< JsonLdDocument > CreateAsync(string Text)
Contains semantic information stored in a turtle document.
static Task< JsonLdDocument > CreateAsync(string Text, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
Contains semantic information stored in a turtle document.
static Task< JsonLdDocument > CreateAsync(object Doc, string Text)
Contains semantic information stored in a turtle document.
static Task< JsonLdDocument > CreateAsync(object Doc, string Text, Uri BaseUri, string BlankNodeIdPrefix)
Contains semantic information stored in a turtle document.
static async Task< JsonLdDocument > CreateAsync(object Doc, string Text, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
Contains semantic information stored in a turtle document.
Task DecodeMetaInformation(HttpResponseMessage HttpResponse)
Decodes meta-information available in the HTTP Response.
object ParsedJson
Original content, as parsed JSON.
Set of semantic elements.
static ISemanticLiteral EncapsulateLiteral(object Value)
Encapsulates an object as a semantic literal.
static ISemanticElement Parse(string Value, string DataType, string Language)
Parses a string literal value.
Implements a semantic triple.
Contains semantic information stored in an RDF document.
static UriNode RdfType
rdf:type predicate
Implements an in-memory cache.
Interface for content classes, that process information available in HTTP headers in the response.
Interface for semantic nodes.
BlankNodeIdMode
How blank node IDs are generated
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.
Prefix
SI prefixes. http://physics.nist.gov/cuu/Units/prefixes.html