diff --git a/doc/linqpad-samples/Compact.linq b/doc/linqpad-samples/Compact.linq new file mode 100644 index 0000000..f90973e --- /dev/null +++ b/doc/linqpad-samples/Compact.linq @@ -0,0 +1,14 @@ + + json-ld.net.dll + Newtonsoft.Json + JsonLD.Core + Newtonsoft.Json.Linq + + +#load "Utils/Resources" + +var opts = new JsonLdOptions(); +var compacted = JsonLdProcessor.Compact(Resources.Doc, Resources.Context, opts); + +compacted.ToString().Dump("string"); +compacted.Dump("JSON DOM"); \ No newline at end of file diff --git a/doc/linqpad-samples/CustomRDFParser.linq b/doc/linqpad-samples/CustomRDFParser.linq new file mode 100644 index 0000000..bb48519 --- /dev/null +++ b/doc/linqpad-samples/CustomRDFParser.linq @@ -0,0 +1,31 @@ + + json-ld.net.dll + Newtonsoft.Json + JsonLD.Core + Newtonsoft.Json.Linq + + +#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); + } +} diff --git a/doc/linqpad-samples/CustomRDFRender.linq b/doc/linqpad-samples/CustomRDFRender.linq new file mode 100644 index 0000000..8bdf673 --- /dev/null +++ b/doc/linqpad-samples/CustomRDFRender.linq @@ -0,0 +1,23 @@ + + json-ld.net.dll + Newtonsoft.Json + JsonLD.Core + Newtonsoft.Json.Linq + + +#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 +} diff --git a/doc/linqpad-samples/Expand.linq b/doc/linqpad-samples/Expand.linq new file mode 100644 index 0000000..5569b4b --- /dev/null +++ b/doc/linqpad-samples/Expand.linq @@ -0,0 +1,16 @@ + + json-ld.net.dll + Newtonsoft.Json + JsonLD.Core + Newtonsoft.Json.Linq + + +#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"); \ No newline at end of file diff --git a/doc/linqpad-samples/FileOrder.txt b/doc/linqpad-samples/FileOrder.txt new file mode 100644 index 0000000..7cbe085 --- /dev/null +++ b/doc/linqpad-samples/FileOrder.txt @@ -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 \ No newline at end of file diff --git a/doc/linqpad-samples/Flatten.linq b/doc/linqpad-samples/Flatten.linq new file mode 100644 index 0000000..2016de6 --- /dev/null +++ b/doc/linqpad-samples/Flatten.linq @@ -0,0 +1,14 @@ + + json-ld.net.dll + Newtonsoft.Json + JsonLD.Core + Newtonsoft.Json.Linq + + +#load "Utils/Resources" + +var opts = new JsonLdOptions(); +var flattened = JsonLdProcessor.Flatten(Resources.Doc, Resources.Context, opts); + +flattened.ToString().Dump("string"); +flattened.Dump("JSON DOM"); \ No newline at end of file diff --git a/doc/linqpad-samples/Frame.linq b/doc/linqpad-samples/Frame.linq new file mode 100644 index 0000000..750f1bf --- /dev/null +++ b/doc/linqpad-samples/Frame.linq @@ -0,0 +1,14 @@ + + json-ld.net.dll + Newtonsoft.Json + JsonLD.Core + Newtonsoft.Json.Linq + + +#load "Utils/Resources" + +var opts = new JsonLdOptions(); +var framed = JsonLdProcessor.Frame(Resources.Doc, Resources.Frame, opts); + +framed.ToString().Dump("string"); +framed.Dump("JSON DOM"); \ No newline at end of file diff --git a/doc/linqpad-samples/FromRDF.linq b/doc/linqpad-samples/FromRDF.linq new file mode 100644 index 0000000..193cd77 --- /dev/null +++ b/doc/linqpad-samples/FromRDF.linq @@ -0,0 +1,17 @@ + + json-ld.net.dll + Newtonsoft.Json + JsonLD.Core + Newtonsoft.Json.Linq + + +#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"); diff --git a/doc/linqpad-samples/Installation.linq b/doc/linqpad-samples/Installation.linq new file mode 100644 index 0000000..b0edf4c --- /dev/null +++ b/doc/linqpad-samples/Installation.linq @@ -0,0 +1,13 @@ + + json-ld.net.dll + Newtonsoft.Json + Newtonsoft.Json.Linq + JsonLD.Core + + +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"); \ No newline at end of file diff --git a/doc/linqpad-samples/Normalize.linq b/doc/linqpad-samples/Normalize.linq new file mode 100644 index 0000000..ab252e6 --- /dev/null +++ b/doc/linqpad-samples/Normalize.linq @@ -0,0 +1,15 @@ + + json-ld.net.dll + Newtonsoft.Json + JsonLD.Core + Newtonsoft.Json.Linq + + +#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"); \ No newline at end of file diff --git a/doc/linqpad-samples/RemoteDocumentLoader.linq b/doc/linqpad-samples/RemoteDocumentLoader.linq new file mode 100644 index 0000000..4590bd5 --- /dev/null +++ b/doc/linqpad-samples/RemoteDocumentLoader.linq @@ -0,0 +1,37 @@ + + json-ld.net.dll + Newtonsoft.Json + JsonLD.Core + Newtonsoft.Json.Linq + + +#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); + } + } +} diff --git a/doc/linqpad-samples/ToRDF.linq b/doc/linqpad-samples/ToRDF.linq new file mode 100644 index 0000000..9ee8ca3 --- /dev/null +++ b/doc/linqpad-samples/ToRDF.linq @@ -0,0 +1,16 @@ + + json-ld.net.dll + Newtonsoft.Json + JsonLD.Core + Newtonsoft.Json.Linq + + +#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"); \ No newline at end of file diff --git a/doc/linqpad-samples/Utils/ObjectDumper.linq b/doc/linqpad-samples/Utils/ObjectDumper.linq new file mode 100644 index 0000000..a60473c --- /dev/null +++ b/doc/linqpad-samples/Utils/ObjectDumper.linq @@ -0,0 +1,80 @@ + + System.ComponentModel + + +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() : ""); + 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() : ""); + DumpObject(value, nestingLevel + 1); + } + } + } + + private bool ImplementsDictionary(Type t) => t.GetInterfaces().Any(i => i.Name.Contains("IDictionary")); +} diff --git a/doc/linqpad-samples/Utils/Resources.linq b/doc/linqpad-samples/Utils/Resources.linq new file mode 100644 index 0000000..9dddc06 --- /dev/null +++ b/doc/linqpad-samples/Utils/Resources.linq @@ -0,0 +1,19 @@ + + context.json + doc.json + frame.json + Newtonsoft.Json + Newtonsoft.Json.Linq + + +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"))); +} diff --git a/doc/linqpad-samples/context.json b/doc/linqpad-samples/context.json new file mode 100644 index 0000000..0b1d647 --- /dev/null +++ b/doc/linqpad-samples/context.json @@ -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/" +} \ No newline at end of file diff --git a/doc/linqpad-samples/doc.json b/doc/linqpad-samples/doc.json new file mode 100644 index 0000000..5d6bd3d --- /dev/null +++ b/doc/linqpad-samples/doc.json @@ -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" + } + } + ] +} \ No newline at end of file diff --git a/doc/linqpad-samples/frame.json b/doc/linqpad-samples/frame.json new file mode 100644 index 0000000..b274c87 --- /dev/null +++ b/doc/linqpad-samples/frame.json @@ -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" +} \ No newline at end of file diff --git a/src/json-ld.net/json-ld.net.csproj b/src/json-ld.net/json-ld.net.csproj index 728e89c..8db572b 100644 --- a/src/json-ld.net/json-ld.net.csproj +++ b/src/json-ld.net/json-ld.net.csproj @@ -8,7 +8,7 @@ Implements the W3C JSON-LD 1.0 standard. netstandard1.1;netstandard2.0;net40 json-ld.net json-ld.net - json-ld;jsonld;json;linked-data;rdf;semantic;web + json-ld;jsonld;json;linked-data;rdf;semantic;web;linqpad-samples http://json-ld.org/images/json-ld-logo-64.png https://github.com/linked-data-dotnet/json-ld.net/ https://raw.githubusercontent.com/linked-data-dotnet/json-ld.net/main/LICENSE @@ -26,6 +26,10 @@ Implements the W3C JSON-LD 1.0 standard. + + + +