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))
405 ContextDoc.parsedJson is Dictionary<string, object> ContextObj2 &&
406 ContextObj2.TryGetValue(
"@context", out
object Obj) &&
407 Obj is Dictionary<string, object> ContextObj3)
409 Context =
new JsonLdContext(ContextObj3, BaseUri);
410 contextObjects[ContextUri.AbsolutePath] =
new KeyValuePair<DateTimeOffset, Dictionary<string, object>>(
411 ContextDoc.Date ?? DateTimeOffset.Now, ContextObj3);
414 Context =
new JsonLdContext(P2.Value, BaseUri);
418 Context =
new JsonLdContext(P2.Value, BaseUri);
428 ContextDoc.parsedJson is Dictionary<string, object> ContextObj2 &&
429 ContextObj2.TryGetValue(
"@context", out
object Obj) &&
430 Obj is Dictionary<string, object> ContextObj3)
432 Context =
new JsonLdContext(ContextObj3, BaseUri);
433 contextObjects[ContextUri.AbsolutePath] =
new KeyValuePair<DateTimeOffset, Dictionary<string, object>>(
434 ContextDoc.Date ?? DateTimeOffset.Now, ContextObj3);
444 private async Task<JsonLdContext> AddType(
ISemanticElement Subject,
object Value, Uri BaseUri, JsonLdContext Context)
446 if (Value is
string s)
448 if (TryGetContextName(s, Context, out
object ContextValue))
450 Context = await this.AddType(Subject, ContextValue, BaseUri, Context);
455 else if (Value is Dictionary<string, object> TypeObj)
457 foreach (KeyValuePair<string, object> P
in TypeObj)
462 Context = await this.AddType(Subject, P.Value, BaseUri, Context);
466 JsonLdContext ScopedContext = await this.GetContext(P.Value, BaseUri, Context);
467 if (!(Context is
null))
468 ScopedContext.Append(Context);
470 Context = ScopedContext;
475 else if (Value is Array A)
477 foreach (
object Item
in A)
478 await this.AddType(Subject, Item, BaseUri, Context);
486 private static bool ExpandPrefixes(ref
string Name, JsonLdContext Context)
491 int i = Name.IndexOf(
':');
495 string Prefix = Name.Substring(0, i);
497 if (!Context.TryGetObjectValue(Prefix, out
object Value))
500 if (Value is
string PrefixUrl)
502 Name = PrefixUrl + Name.Substring(i + 1);
506 if (Value is Dictionary<string, object> Obj)
508 foreach (KeyValuePair<string, object> P
in Obj)
513 PrefixUrl = P.Value as string;
514 if (!(PrefixUrl is
null))
516 Name = PrefixUrl + Name.Substring(i + 1);
527 private static bool TryGetContextName(
string Name, JsonLdContext Context, out
object Value)
529 if (Name.StartsWith(
"@"))
541 if (ExpandPrefixes(ref Name, Context))
547 if (Context.TryGetObjectValue(Name, out Value))
550 if (!(Context.Vocabulary is
null) && Name.IndexOf(
':') < 0)
552 Value = Context.Vocabulary.AbsoluteUri + Name;
560 private async Task<ISemanticElement> ParseValue(Uri BaseUri,
object Value, JsonLdContext Context)
562 if (Value is Dictionary<string, object> Obj)
566 foreach (KeyValuePair<string, object> P
in Obj)
571 if (P.Value is
string s)
572 Object = this.CreateUriNode(s, BaseUri, Context);
578 s = P.Value as string;
580 BaseUri = this.CreateUri(s, BaseUri, Context);
589 Object = this.CreateBlankNode();
591 return await this.Parse(Obj, Context, BaseUri, Object) ?? Object;
593 else if (Value is Uri Uri)
594 return new UriNode(Uri, Uri.ToString());
607 public string Text => this.text;
612 public DateTimeOffset?
Date => this.date;
620 this.date = HttpResponse.Headers.Date;
621 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.
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Static class managing encoding and decoding of internet content.
static Task< ContentResponse > 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