public static void Run() { var doc = JObject.Parse(_docJson); var context = JObject.Parse(_contextJson); var opts = new JsonLdOptions(); var flattened = JsonLdProcessor.Flatten(doc, context, opts); Console.WriteLine(flattened); /* * * Output: * { * "@context": . . ., * "@graph": [ * { * "@id": "_:b0", * "image": "http://manu.sporny.org/images/manu.png", * "name": "Manu Sporny", * "homepage": "http://manu.sporny.org/" * }, * { * "@id": "ld-experts", * "member": { * "@id": "_:b0" * }, * "name": "LD Experts" * } * ] * } * */ }
public void Load(IGraph g, TextReader input) { JToken json; using (JsonReader jsonReader = new JsonTextReader(input)) { json = JToken.Load(jsonReader); JArray flattened = (JArray)JsonLdProcessor.Flatten(json, new JsonLdOptions()); foreach (JObject subjectJObject in flattened) { string subject = subjectJObject["@id"].ToString(); JToken type; if (subjectJObject.TryGetValue("@type", out type)) { if (type is JArray) { foreach (JToken t in (JArray)type) { Assert(g, subject, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", new Uri(t.ToString()), null); } } else { Assert(g, subject, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", new Uri(type.ToString()), null); } } foreach (JProperty property in subjectJObject.Properties()) { if (property.Name == "@id" || property.Name == "@type") { continue; } foreach (JObject objectJObject in property.Value) { JToken id; JToken value; if (objectJObject.TryGetValue("@id", out id)) { Assert(g, subject, property.Name, new Uri(id.ToString()), null); } else if (objectJObject.TryGetValue("@value", out value)) { string datatype = null; JToken datatypeJToken; if (objectJObject.TryGetValue("@type", out datatypeJToken)) { datatype = datatypeJToken.ToString(); } Assert(g, subject, property.Name, value.ToString(), datatype); } } } } } }
public static IGraph CreateGraph(JToken compacted) { JToken flattened = JsonLdProcessor.Flatten(compacted, new JsonLdOptions()); IRdfReader rdfReader = new JsonLdReader(); IGraph graph = new Graph(); rdfReader.Load(graph, new StringReader(flattened.ToString())); return(graph); }
public static IGraph CreateGraph(JToken compacted, bool readOnly) { JToken flattened = JsonLdProcessor.Flatten(compacted, new JsonLdOptions()); IRdfReader rdfReader = new JsonLdReader(); IGraph graph = new Graph(); rdfReader.Load(graph, new StringReader(flattened.ToString(Newtonsoft.Json.Formatting.None, new Newtonsoft.Json.JsonConverter[0]))); if (readOnly) { graph = new ReadOnlyGraph(graph); } return(graph); }
public IEnumerator <object[]> GetEnumerator() { foreach (string manifest in manifests) { JToken manifestJson; manifestJson = GetJson(manifest); foreach (JObject testcase in manifestJson["sequence"]) { Func <JToken> run; ConformanceCase newCase = new ConformanceCase(); newCase.input = GetJson(testcase["input"]); newCase.context = GetJson(testcase["context"]); newCase.frame = GetJson(testcase["frame"]); var options = new JsonLdOptions("http://json-ld.org/test-suite/tests/" + (string)testcase["input"]); var testType = (JArray)testcase["@type"]; if (testType.Any((s) => (string)s == "jld:NegativeEvaluationTest")) { newCase.error = testcase["expect"]; } else if (testType.Any((s) => (string)s == "jld:PositiveEvaluationTest")) { if (testType.Any((s) => new List <string> { "jld:ToRDFTest", "jld:NormalizeTest" }.Contains((string)s))) { newCase.output = File.ReadAllText(Path.Combine("W3C", (string)testcase["expect"])); } else if (testType.Any((s) => (string)s == "jld:FromRDFTest")) { newCase.input = File.ReadAllText(Path.Combine("W3C", (string)testcase["input"])); newCase.output = GetJson(testcase["expect"]); } else { newCase.output = GetJson(testcase["expect"]); } } else { throw new Exception("Expecting either positive or negative evaluation test."); } JToken optionToken; JToken value; if (testcase.TryGetValue("option", out optionToken)) { JObject optionDescription = (JObject)optionToken; if (optionDescription.TryGetValue("compactArrays", out value)) { options.SetCompactArrays((bool)value); } if (optionDescription.TryGetValue("base", out value)) { options.SetBase((string)value); } if (optionDescription.TryGetValue("expandContext", out value)) { newCase.context = GetJson(testcase["option"]["expandContext"]); options.SetExpandContext((JObject)newCase.context); } if (optionDescription.TryGetValue("produceGeneralizedRdf", out value)) { options.SetProduceGeneralizedRdf((bool)value); } if (optionDescription.TryGetValue("useNativeTypes", out value)) { options.SetUseNativeTypes((bool)value); } if (optionDescription.TryGetValue("useRdfType", out value)) { options.SetUseRdfType((bool)value); } } if (testType.Any((s) => (string)s == "jld:CompactTest")) { run = () => JsonLdProcessor.Compact(newCase.input, newCase.context, options); } else if (testType.Any((s) => (string)s == "jld:ExpandTest")) { run = () => JsonLdProcessor.Expand(newCase.input, options); } else if (testType.Any((s) => (string)s == "jld:FlattenTest")) { run = () => JsonLdProcessor.Flatten(newCase.input, newCase.context, options); } else if (testType.Any((s) => (string)s == "jld:FrameTest")) { run = () => JsonLdProcessor.Frame(newCase.input, newCase.frame, options); } else if (testType.Any((s) => (string)s == "jld:NormalizeTest")) { run = () => new JValue( RDFDatasetUtils.ToNQuads((RDFDataset)JsonLdProcessor.Normalize(newCase.input, options)).Replace("\n", "\r\n") ); } else if (testType.Any((s) => (string)s == "jld:ToRDFTest")) { options.format = "application/nquads"; run = () => new JValue( ((string)JsonLdProcessor.ToRDF(newCase.input, options)).Replace("\n", "\r\n") ); } else if (testType.Any((s) => (string)s == "jld:FromRDFTest")) { options.format = "application/nquads"; run = () => JsonLdProcessor.FromRDF(newCase.input, options); } else { run = () => { throw new Exception("Couldn't find a test type, apparently."); }; } if ((string)manifestJson["name"] == "Remote document") { Func <JToken> innerRun = run; run = () => { var remoteDoc = options.documentLoader.LoadDocument("https://json-ld.org/test-suite/tests/" + (string)testcase["input"]); newCase.input = remoteDoc.Document; options.SetBase(remoteDoc.DocumentUrl); options.SetExpandContext((JObject)remoteDoc.Context); return(innerRun()); }; } if (testType.Any((s) => (string)s == "jld:NegativeEvaluationTest")) { Func <JToken> innerRun = run; run = () => { try { return(innerRun()); } catch (JsonLdError err) { JObject result = new JObject(); result["error"] = err.Message; return(result); } }; } newCase.run = run; yield return(new object[] { manifest + (string)testcase["@id"], (string)testcase["name"], newCase }); } } }
public static BasicGraph GetGraphFromCompacted(JToken compacted) { var flattened = JsonLdProcessor.Flatten(compacted, new JsonLdOptions()); return(GetGraph(flattened)); }