Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/linqpad-samples/Compact.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Query Kind="Statements">
<Reference Relative="..\..\src\json-ld.net\bin\Debug\netstandard1.1\json-ld.net.dll">json-ld.net.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>JsonLD.Core</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>

#load "Utils/Resources"

var opts = new JsonLdOptions();
var compacted = JsonLdProcessor.Compact(Resources.Doc, Resources.Context, opts);

compacted.ToString().Dump("string");
compacted.Dump("JSON DOM");
31 changes: 31 additions & 0 deletions doc/linqpad-samples/CustomRDFParser.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Query Kind="Program">
<Reference Relative="..\..\src\json-ld.net\bin\Debug\netstandard1.1\json-ld.net.dll">json-ld.net.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>JsonLD.Core</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>

#load "Utils/Resources"

void Main()
{
var opts = new JsonLdOptions();

var rdf = (RDFDataset)JsonLdProcessor.ToRDF(Resources.Doc, opts);
var serialized = RDFDatasetUtils.ToNQuads(rdf); // serialize RDF to string

var parser = new CustomRDFParser();
var jsonld = JsonLdProcessor.FromRDF(serialized, parser);
jsonld.ToString().Dump("string");
jsonld.Dump("JSON DOM");
}

public class CustomRDFParser : IRDFParser
{
public RDFDataset Parse(JToken input)
{
// by public decree, references to example.org are normalized to https going forward...
var converted = ((string)input).Replace("http://example.org/", "https://example.org/");
return RDFDatasetUtils.ParseNQuads(converted);
}
}
23 changes: 23 additions & 0 deletions doc/linqpad-samples/CustomRDFRender.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Query Kind="Program">
<Reference Relative="..\..\src\json-ld.net\bin\Debug\netstandard1.1\json-ld.net.dll">json-ld.net.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>JsonLD.Core</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>

#load "Utils/Resources"

void Main()
{
var opts = new JsonLdOptions();

var callback = new JSONLDTripleCallback();
var serialized = JsonLdProcessor.ToRDF(Resources.Doc, callback);
serialized.Dump("RDF");
}

public class JSONLDTripleCallback : IJSONLDTripleCallback
{
public object Call(RDFDataset dataset) =>
RDFDatasetUtils.ToNQuads(dataset); // serialize the RDF dataset as NQuads
}
16 changes: 16 additions & 0 deletions doc/linqpad-samples/Expand.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Query Kind="Statements">
<Reference Relative="..\..\src\json-ld.net\bin\Debug\netstandard1.1\json-ld.net.dll">json-ld.net.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>JsonLD.Core</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>

#load "Utils/Resources"

var opts = new JsonLdOptions();
var compacted = JsonLdProcessor.Compact(Resources.Doc, Resources.Context, opts);

var expanded = JsonLdProcessor.Expand(compacted);

expanded.ToString().Dump("string");
expanded.Dump("JSON DOM");
11 changes: 11 additions & 0 deletions doc/linqpad-samples/FileOrder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Installation.linq
Compact.linq
Expand.linq
Flatten.linq
Frame.linq
Normalize.linq
ToRDF.linq
FromRDF.linq
CustomRDFRender.linq
CustomRDFParser.linq
RemoteDocumentLoader.linq
14 changes: 14 additions & 0 deletions doc/linqpad-samples/Flatten.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Query Kind="Statements">
<Reference Relative="..\..\src\json-ld.net\bin\Debug\netstandard1.1\json-ld.net.dll">json-ld.net.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>JsonLD.Core</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>

#load "Utils/Resources"

var opts = new JsonLdOptions();
var flattened = JsonLdProcessor.Flatten(Resources.Doc, Resources.Context, opts);

flattened.ToString().Dump("string");
flattened.Dump("JSON DOM");
14 changes: 14 additions & 0 deletions doc/linqpad-samples/Frame.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Query Kind="Statements">
<Reference Relative="..\..\src\json-ld.net\bin\Debug\netstandard1.1\json-ld.net.dll">json-ld.net.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>JsonLD.Core</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>

#load "Utils/Resources"

var opts = new JsonLdOptions();
var framed = JsonLdProcessor.Frame(Resources.Doc, Resources.Frame, opts);

framed.ToString().Dump("string");
framed.Dump("JSON DOM");
17 changes: 17 additions & 0 deletions doc/linqpad-samples/FromRDF.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Query Kind="Statements">
<Reference Relative="..\..\src\json-ld.net\bin\Debug\netstandard1.1\json-ld.net.dll">json-ld.net.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>JsonLD.Core</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>

#load "Utils/Resources"

var opts = new JsonLdOptions();
var rdf = (RDFDataset)JsonLdProcessor.ToRDF(Resources.Doc, opts);
var serialized = RDFDatasetUtils.ToNQuads(rdf); // serialize RDF to string

var jsonld = JsonLdProcessor.FromRDF(serialized, opts);

jsonld.ToString().Dump("string");
jsonld.Dump("JSON DOM");
13 changes: 13 additions & 0 deletions doc/linqpad-samples/Installation.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Query Kind="Statements">
<Reference Relative="..\..\src\json-ld.net\bin\Debug\netstandard1.1\json-ld.net.dll">json-ld.net.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>Newtonsoft.Json.Linq</Namespace>
<Namespace>JsonLD.Core</Namespace>
</Query>

var json = "{'@context':{'test':'http://www.example.org/'},'test:hello':'world'}";
var document = JObject.Parse(json);
var expanded = JsonLdProcessor.Expand(document);

expanded.ToString().Dump("string");
expanded.Dump("JSON DOM");
15 changes: 15 additions & 0 deletions doc/linqpad-samples/Normalize.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Query Kind="Statements">
<Reference Relative="..\..\src\json-ld.net\bin\Debug\netstandard1.1\json-ld.net.dll">json-ld.net.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>JsonLD.Core</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>

#load "Utils/Resources"
#load "Utils/ObjectDumper"

var opts = new JsonLdOptions();
var normalized = (RDFDataset)JsonLdProcessor.Normalize(Resources.Doc, opts);

normalized.JsonLDDump().Dump("string");
normalized.Dump("JSON DOM");
37 changes: 37 additions & 0 deletions doc/linqpad-samples/RemoteDocumentLoader.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Query Kind="Program">
<Reference Relative="..\..\src\json-ld.net\bin\Debug\netstandard1.1\json-ld.net.dll">json-ld.net.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>JsonLD.Core</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>

#load "Utils/Resources"

void Main()
{
var doc = Resources.Doc;
var remoteContext = JObject.Parse("{'@context':'http://example.org/context.jsonld'}");
var opts = new JsonLdOptions { documentLoader = new CustomDocumentLoader() };
var compacted = JsonLdProcessor.Compact(doc, remoteContext, opts);

compacted.ToString().Dump("string");
compacted.Dump("JSON DOM");
}

public class CustomDocumentLoader : DocumentLoader
{
private static readonly string _cachedExampleOrgContext = Resources.Context.ToString();

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);
}
}
}
16 changes: 16 additions & 0 deletions doc/linqpad-samples/ToRDF.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Query Kind="Statements">
<Reference Relative="..\..\src\json-ld.net\bin\Debug\netstandard1.1\json-ld.net.dll">json-ld.net.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>JsonLD.Core</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>

#load "Utils/Resources"

var opts = new JsonLdOptions();
var rdf = (RDFDataset)JsonLdProcessor.ToRDF(Resources.Doc, opts);

var serialized = RDFDatasetUtils.ToNQuads(rdf); // serialize RDF to string

serialized.Dump("string");
serialized.Dump("JSON DOM");
80 changes: 80 additions & 0 deletions doc/linqpad-samples/Utils/ObjectDumper.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<Query Kind="Program">
<Namespace>System.ComponentModel</Namespace>
</Query>

void Main()
{

}

public static class ObjectDumperExtensions
{
public static string JsonLDDump(this object obj) => ObjectDumper.Dump(obj);
}

// thanks: https://stackoverflow.com/a/42264037
public class ObjectDumper
{
public static string Dump(object obj)
{
return new ObjectDumper().DumpObject(obj);
}

private readonly StringBuilder _dumpBuilder = new StringBuilder();

private string DumpObject(object obj)
{
DumpObject(obj, 0);
return _dumpBuilder.ToString();
}

private void DumpObject(object obj, int nestingLevel = 0)
{
var nestingSpaces = new String('\t', nestingLevel); //"".PadLeft(nestingLevel * 4);

if (obj == null)
{
_dumpBuilder.AppendFormat("null", nestingSpaces);
}
else if (obj is string || obj.GetType().IsPrimitive)
{
_dumpBuilder.AppendFormat("{1}", nestingSpaces, obj.ToString().PadRight(8));
}
else if (ImplementsDictionary(obj.GetType()))
{
using var e = ((dynamic)obj).GetEnumerator();
var enumerator = (IEnumerator)e;
while (enumerator.MoveNext())
{
dynamic p = enumerator.Current;

var key = p.Key;
var value = p.Value;
_dumpBuilder.AppendFormat("\n{0}{1}", nestingSpaces, key.PadRight(10), value != null ? value.GetType().ToString() : "<null>");
DumpObject(value, nestingLevel + 1);
}
}
else if (obj is IEnumerable)
{
foreach (dynamic p in obj as IEnumerable)
{
DumpObject(p, nestingLevel);
DumpObject("\n", nestingLevel);
DumpObject("---", nestingLevel);
}
}
else
{
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj))
{
string name = descriptor.Name;
object value = descriptor.GetValue(obj);

_dumpBuilder.AppendFormat("{0}{1}\n", nestingSpaces, name.PadRight(10), value != null ? value.GetType().ToString() : "<null>");
DumpObject(value, nestingLevel + 1);
}
}
}

private bool ImplementsDictionary(Type t) => t.GetInterfaces().Any(i => i.Name.Contains("IDictionary"));
}
19 changes: 19 additions & 0 deletions doc/linqpad-samples/Utils/Resources.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Query Kind="Program">
<Reference Relative="..\context.json">context.json</Reference>
<Reference Relative="..\doc.json">doc.json</Reference>
<Reference Relative="..\frame.json">frame.json</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>

void Main()
{

}

public static class Resources
{
public static readonly JObject Doc = JObject.Parse(File.ReadAllText(Util.GetFullPath("doc.json")));
public static readonly JObject Context = JObject.Parse(File.ReadAllText(Util.GetFullPath("context.json")));
public static readonly JObject Frame = JObject.Parse(File.ReadAllText(Util.GetFullPath("frame.json")));
}
15 changes: 15 additions & 0 deletions doc/linqpad-samples/context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "http://schema.org/name",
"member": "http://schema.org/member",
"homepage": {
"@id": "http://schema.org/url",
"@type": "@id"
},
"image": {
"@id": "http://schema.org/image",
"@type": "@id"
},
"Person": "http://schema.org/Person",
"@vocab": "http://example.org/",
"@base": "http://example.org/"
}
16 changes: 16 additions & 0 deletions doc/linqpad-samples/doc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"@id": "http://example.org/ld-experts",
"http://schema.org/name": "LD Experts",
"http://schema.org/member": [
{
"@type": "http://schema.org/Person",
"http://schema.org/name": "Manu Sporny",
"http://schema.org/url": {
"@id": "http://manu.sporny.org/"
},
"http://schema.org/image": {
"@id": "http://manu.sporny.org/images/manu.png"
}
}
]
}
19 changes: 19 additions & 0 deletions doc/linqpad-samples/frame.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"@context": {
"name": "http://schema.org/name",
"member": {
"@id": "http://schema.org/member",
"@type": "@id"
},
"homepage": {
"@id": "http://schema.org/url",
"@type": "@id"
},
"image": {
"@id": "http://schema.org/image",
"@type": "@id"
},
"Person": "http://schema.org/Person"
},
"@type": "Person"
}
Loading