-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleA_RemoteDocumentLoader.cs
55 lines (48 loc) · 1.75 KB
/
SampleA_RemoteDocumentLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Res = JsonLD.Demo.Resources.Resources;
using System;
using JsonLD.Core;
using Newtonsoft.Json.Linq;
namespace JsonLD.Demo
{
public class SampleA_RemoteDocumentLoader
{
private static readonly string _docJson = Res.ReadString("doc.json");
public class CustomDocumentLoader : DocumentLoader
{
private static readonly string _cachedExampleOrgContext = Res.ReadString("context.json");
public override RemoteDocument LoadDocument(string url)
{
if (url == "http://example.org/context.jsonld") // we have this cached locally
{
var doc = new JObject(new JProperty("@context", JObject.Parse(_cachedExampleOrgContext)));
return new RemoteDocument(url, doc);
}
else
{
return base.LoadDocument(url);
}
}
}
public static void Run()
{
var doc = JObject.Parse(_docJson);
var remoteContext = JObject.Parse("{'@context':'http://example.org/context.jsonld'}");
var opts = new JsonLdOptions { documentLoader = new CustomDocumentLoader() };
var compacted = JsonLdProcessor.Compact(doc, remoteContext, opts);
Console.WriteLine(compacted);
/*
Output:
{
"@id": "http://example.org/ld-experts",
"member": {
"@type": "Person",
"image": "http://manu.sporny.org/images/manu.png",
"name": "Manu Sporny",
"homepage": "http://manu.sporny.org/"
},
"name": "LD Experts"
}
*/
}
}
}