Skip to content

Commit 017f383

Browse files
author
Andrew Stewart Gibson
committed
demo samples for json-ld.net
0 parents  commit 017f383

20 files changed

+733
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode
2+
bin
3+
obj

ObjectDumper.cs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections;
3+
using System.ComponentModel;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace JsonLD.Demo
8+
{
9+
public static class ObjectDumperExtensions
10+
{
11+
public static string Dump(this object obj) => ObjectDumper.Dump(obj);
12+
13+
}
14+
15+
// thanks: https://stackoverflow.com/a/42264037
16+
public class ObjectDumper
17+
{
18+
public static string Dump(object obj)
19+
{
20+
return new ObjectDumper().DumpObject(obj);
21+
}
22+
23+
private readonly StringBuilder _dumpBuilder = new StringBuilder();
24+
25+
private string DumpObject(object obj)
26+
{
27+
DumpObject(obj, 0);
28+
return _dumpBuilder.ToString();
29+
}
30+
31+
private void DumpObject(object obj, int nestingLevel = 0)
32+
{
33+
var nestingSpaces = new String('\t', nestingLevel); //"".PadLeft(nestingLevel * 4);
34+
35+
if (obj == null)
36+
{
37+
_dumpBuilder.AppendFormat("null", nestingSpaces);
38+
}
39+
else if (obj is string || obj.GetType().IsPrimitive)
40+
{
41+
_dumpBuilder.AppendFormat("{1}", nestingSpaces, obj.ToString().PadRight(8));
42+
}
43+
else if (ImplementsDictionary(obj.GetType()))
44+
{
45+
using var e = ((dynamic)obj).GetEnumerator();
46+
var enumerator = (IEnumerator)e;
47+
while (enumerator.MoveNext())
48+
{
49+
dynamic p = enumerator.Current;
50+
51+
var key = p.Key;
52+
var value = p.Value;
53+
_dumpBuilder.AppendFormat("\n{0}{1}", nestingSpaces, key.PadRight(10), value != null ? value.GetType().ToString() : "<null>");
54+
DumpObject(value, nestingLevel + 1);
55+
}
56+
}
57+
else if (obj is IEnumerable)
58+
{
59+
foreach (dynamic p in obj as IEnumerable)
60+
{
61+
DumpObject(p, nestingLevel);
62+
DumpObject("\n", nestingLevel);
63+
DumpObject("---", nestingLevel);
64+
}
65+
}
66+
else
67+
{
68+
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj))
69+
{
70+
string name = descriptor.Name;
71+
object value = descriptor.GetValue(obj);
72+
73+
_dumpBuilder.AppendFormat("{0}{1}\n", nestingSpaces, name.PadRight(10), value != null ? value.GetType().ToString() : "<null>");
74+
DumpObject(value, nestingLevel + 1);
75+
}
76+
}
77+
}
78+
79+
private bool ImplementsDictionary(Type t) => t.GetInterfaces().Any(i => i.Name.Contains("IDictionary"));
80+
}
81+
}

Program.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace JsonLD.Demo
2+
{
3+
internal class Program
4+
{
5+
private static void Main()
6+
{
7+
Sample0_Installation.Run();
8+
Sample1_Compact.Run();
9+
Sample2_Expand.Run();
10+
Sample3_Flatten.Run();
11+
Sample4_Frame.Run();
12+
Sample5_Normalize.Run();
13+
Sample6_ToRDF.Run();
14+
Sample7_FromRDF.Run();
15+
Sample8_CustomRDFRender.Run();
16+
Sample9_CustomRDFParser.Run();
17+
SampleA_RemoteDocumentLoader.Run();
18+
}
19+
}
20+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This repository contains the demonstration code used to generate the README.md file samples for https://github.com/linked-data-dotnet/json-ld.net

Resources/Resources.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Reflection;
2+
using System.IO;
3+
4+
namespace JsonLD.Demo.Resources
5+
{
6+
7+
internal static class Resources
8+
{
9+
10+
private static readonly Assembly _thisAssembly = typeof(Resources).GetTypeInfo().Assembly;
11+
public static string ReadString(string name)
12+
{
13+
var resourceName = $"{nameof(JsonLD)}.{nameof(Demo)}.{nameof(Resources)}.{name}";
14+
using var resourceStream = _thisAssembly.GetManifestResourceStream(resourceName);
15+
using var streamReader = new StreamReader(resourceStream);
16+
return streamReader.ReadToEnd();
17+
}
18+
}
19+
}

Resources/context.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "http://schema.org/name",
3+
"member": "http://schema.org/member",
4+
"homepage": {
5+
"@id": "http://schema.org/url",
6+
"@type": "@id"
7+
},
8+
"image": {
9+
"@id": "http://schema.org/image",
10+
"@type": "@id"
11+
},
12+
"Person": "http://schema.org/Person",
13+
"@vocab": "http://example.org/",
14+
"@base": "http://example.org/"
15+
}

Resources/doc.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"@id": "http://example.org/ld-experts",
3+
"http://schema.org/name": "LD Experts",
4+
"http://schema.org/member": [
5+
{
6+
"@type": "http://schema.org/Person",
7+
"http://schema.org/name": "Manu Sporny",
8+
"http://schema.org/url": {
9+
"@id": "http://manu.sporny.org/"
10+
},
11+
"http://schema.org/image": {
12+
"@id": "http://manu.sporny.org/images/manu.png"
13+
}
14+
}
15+
]
16+
}

Resources/frame.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"@context": {
3+
"name": "http://schema.org/name",
4+
"member": {
5+
"@id": "http://schema.org/member",
6+
"@type": "@id"
7+
},
8+
"homepage": {
9+
"@id": "http://schema.org/url",
10+
"@type": "@id"
11+
},
12+
"image": {
13+
"@id": "http://schema.org/image",
14+
"@type": "@id"
15+
},
16+
"Person": "http://schema.org/Person"
17+
},
18+
"@type": "Person"
19+
}

Sample0_Installation.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using JsonLD.Core;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
5+
namespace JsonLD.Demo
6+
{
7+
public static class Sample0_Installation
8+
{
9+
10+
public static void Run()
11+
{
12+
var json = "{'@context':{'test':'http://www.example.org/'},'test:hello':'world'}";
13+
var document = JObject.Parse(json);
14+
var expanded = JsonLdProcessor.Expand(document);
15+
Console.WriteLine(expanded);
16+
}
17+
}
18+
}

Sample1_Compact.cs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Res = JsonLD.Demo.Resources.Resources;
2+
using System;
3+
using JsonLD.Core;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace JsonLD.Demo
7+
{
8+
public class Sample1_Compact
9+
{
10+
private static readonly string _contextJson = Res.ReadString("context.json");
11+
private static readonly string _docJson = Res.ReadString("doc.json");
12+
13+
public static JObject Run()
14+
{
15+
var doc = JObject.Parse(_docJson);
16+
var context = JObject.Parse(_contextJson);
17+
var opts = new JsonLdOptions();
18+
var compacted = JsonLdProcessor.Compact(doc, context, opts);
19+
Console.WriteLine(compacted);
20+
21+
/*
22+
23+
Output:
24+
{
25+
"@id": "ld-experts",
26+
"member": {
27+
"image": "http://manu.sporny.org/images/manu.png",
28+
"name": "Manu Sporny",
29+
"homepage": "http://manu.sporny.org/",
30+
},
31+
"name": "LD Experts",
32+
"@context": . . .
33+
}
34+
35+
*/
36+
37+
return compacted;
38+
}
39+
}
40+
}

Sample2_Expand.cs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using JsonLD.Core;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace JsonLD.Demo
6+
{
7+
public class Sample2_Expand
8+
{
9+
public static JArray Run()
10+
{
11+
var compacted = Sample1_Compact.Run();
12+
var expanded = JsonLdProcessor.Expand(compacted);
13+
Console.WriteLine(expanded);
14+
15+
/*
16+
17+
Output:
18+
[
19+
{
20+
"@id": "http://example.org/ld-experts",
21+
"http://schema.org/member": [
22+
{
23+
"http://schema.org/url": [
24+
{
25+
"@id": "http://manu.sporny.org/"
26+
}
27+
],
28+
"http://schema.org/image": [
29+
{
30+
"@id": "http://manu.sporny.org/images/manu.png"
31+
}
32+
],
33+
"http://schema.org/name": [
34+
{
35+
"@value": "Manu Sporny"
36+
}
37+
]
38+
}
39+
]
40+
}
41+
]
42+
43+
*/
44+
45+
return expanded;
46+
}
47+
}
48+
}

Sample3_Flatten.cs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Res = JsonLD.Demo.Resources.Resources;
2+
using System;
3+
using JsonLD.Core;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace JsonLD.Demo
7+
{
8+
public class Sample3_Flatten
9+
{
10+
private static readonly string _contextJson = Res.ReadString("context.json");
11+
private static readonly string _docJson = Res.ReadString("doc.json");
12+
13+
public static void Run()
14+
{
15+
var doc = JObject.Parse(_docJson);
16+
var context = JObject.Parse(_contextJson);
17+
var opts = new JsonLdOptions();
18+
var flattened = JsonLdProcessor.Flatten(doc, context, opts);
19+
Console.WriteLine(flattened);
20+
21+
/*
22+
23+
Output:
24+
{
25+
"@context": . . .,
26+
"@graph": [
27+
{
28+
"@id": "_:b0",
29+
"image": "http://manu.sporny.org/images/manu.png",
30+
"name": "Manu Sporny",
31+
"homepage": "http://manu.sporny.org/"
32+
},
33+
{
34+
"@id": "ld-experts",
35+
"member": {
36+
"@id": "_:b0"
37+
},
38+
"name": "LD Experts"
39+
}
40+
]
41+
}
42+
43+
*/
44+
45+
}
46+
}
47+
}

Sample4_Frame.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Res = JsonLD.Demo.Resources.Resources;
2+
using System;
3+
using JsonLD.Core;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace JsonLD.Demo
7+
{
8+
public class Sample4_Frame
9+
{
10+
private static readonly string _docJson = Res.ReadString("doc.json");
11+
private static readonly string _frameJson = Res.ReadString("frame.json");
12+
13+
public static void Run()
14+
{
15+
var doc = JObject.Parse(_docJson);
16+
var frame = JObject.Parse(_frameJson);
17+
var opts = new JsonLdOptions();
18+
var flattened = JsonLdProcessor.Frame(doc, frame, opts);
19+
Console.WriteLine(flattened);
20+
21+
/*
22+
23+
Output:
24+
{
25+
"@context": . . .,
26+
"@graph": [
27+
{
28+
"@id": "_:b0",
29+
"@type": "Person",
30+
"image": "http://manu.sporny.org/images/manu.png",
31+
"name": "Manu Sporny",
32+
"homepage": "http://manu.sporny.org/"
33+
}
34+
]
35+
}
36+
37+
*/
38+
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)