Skip to content

Commit 7202bc6

Browse files
authored
Merge pull request #41 from dnllowe/feature/issue-40
Control order of graph nodes and children nodes
2 parents 5114477 + 025f592 commit 7202bc6

15 files changed

+699
-44
lines changed

src/json-ld.net/Core/JsonLdApi.cs

+14-5
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,7 @@ public virtual JToken Compact(Context activeCtx, string activeProperty, JToken e
430430
/// <returns></returns>
431431
/// <exception cref="JsonLdError">JsonLdError</exception>
432432
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
433-
public virtual JToken Expand(Context activeCtx, string activeProperty, JToken element
434-
)
433+
public virtual JToken Expand(Context activeCtx, string activeProperty, JToken element)
435434
{
436435
// 1)
437436
if (element.IsNull())
@@ -1405,7 +1404,7 @@ public virtual JArray Frame(JToken input, JArray frame)
14051404
{
14061405
state.omitDefault = this.opts.GetOmitDefault().Value;
14071406
}
1408-
// use tree map so keys are sotred by default
1407+
// use tree map so keys are sorted by default
14091408
// XXX BUG BUG BUG XXX (sblom) Figure out where this needs to be sorted and use extension methods to return sorted enumerators or something!
14101409
JObject nodes = new JObject();
14111410
GenerateNodeMap(input, nodes);
@@ -2116,7 +2115,12 @@ public virtual JArray FromRDF(RDFDataset dataset)
21162115
JArray result = new JArray();
21172116
// 6)
21182117
JArray ids = new JArray(defaultGraph.GetKeys());
2119-
ids.SortInPlace();
2118+
2119+
if (opts.GetSortGraphsFromRdf())
2120+
{
2121+
ids.SortInPlace();
2122+
}
2123+
21202124
foreach (string subject_1 in ids)
21212125
{
21222126
JsonLdApi.NodeMapNode node = (NodeMapNode)defaultGraph[subject_1];
@@ -2127,7 +2131,12 @@ public virtual JArray FromRDF(RDFDataset dataset)
21272131
node["@graph"] = new JArray();
21282132
// 6.1.2)
21292133
JArray keys = new JArray(graphMap[subject_1].GetKeys());
2130-
keys.SortInPlace();
2134+
2135+
if (opts.GetSortGraphNodesFromRdf())
2136+
{
2137+
keys.SortInPlace();
2138+
}
2139+
21312140
foreach (string s in keys)
21322141
{
21332142
JsonLdApi.NodeMapNode n = (NodeMapNode)graphMap[subject_1][s];

src/json-ld.net/Core/JsonLdOptions.cs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using JsonLD.Core;
21
using Newtonsoft.Json.Linq;
32

43
namespace JsonLD.Core
@@ -43,6 +42,9 @@ public virtual JsonLD.Core.JsonLdOptions Clone()
4342

4443
private bool produceGeneralizedRdf = false;
4544

45+
private bool sortGraphsFromRdf = true;
46+
47+
private bool sortGraphNodesFromRdf = true;
4648
// base options
4749
// frame options
4850
// rdf conversion options
@@ -147,6 +149,25 @@ public virtual void SetProduceGeneralizedRdf(bool produceGeneralizedRdf)
147149
this.produceGeneralizedRdf = produceGeneralizedRdf;
148150
}
149151

152+
public virtual bool GetSortGraphsFromRdf()
153+
{
154+
return sortGraphsFromRdf;
155+
}
156+
157+
public virtual void SetSortGraphsFromRdf(bool sortGraphs)
158+
{
159+
this.sortGraphsFromRdf = sortGraphs;
160+
}
161+
162+
public virtual bool GetSortGraphNodesFromRdf()
163+
{
164+
return sortGraphNodesFromRdf;
165+
}
166+
167+
public virtual void SetSortGraphNodesFromRdf(bool sortGraphNodes)
168+
{
169+
this.sortGraphNodesFromRdf = sortGraphNodes;
170+
}
150171
public string format = null;
151172

152173
public bool useNamespaces = false;

src/json-ld.net/Core/JsonLdProcessor.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ namespace JsonLD.Core
1313
public class JsonLdProcessor
1414
{
1515
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
16-
public static JObject Compact(JToken input, JToken context, JsonLdOptions
17-
opts)
16+
public static JObject Compact(JToken input, JToken context, JsonLdOptions opts)
1817
{
1918
// 1)
2019
// TODO: look into java futures/promises

src/json-ld.net/Core/NormalizeUtils.cs

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public virtual object HashBlankNodes(IEnumerable<string> unnamed_)
108108
normalized.Add(RDFDatasetUtils.ToNQuad(quad, quad.ContainsKey("name"
109109
) && !(quad["name"] == null) ? (string)((IDictionary<string,object>)((IDictionary<string,object>)quad)["name"])["value"] : null));
110110
}
111+
111112
// sort normalized output
112113
normalized.SortInPlace();
113114
// handle output format

src/json-ld.net/json-ld.net.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Description>JSON-LD processor for .NET
44

55
Implements the W3C JSON-LD 1.0 standard.</Description>
6-
<VersionPrefix>1.0.6</VersionPrefix>
6+
<VersionPrefix>1.0.7</VersionPrefix>
77
<Authors>NuGet;linked-data-dotnet</Authors>
88
<TargetFrameworks>netstandard1.3;netstandard2.0;netcoreapp2.1</TargetFrameworks>
99
<AssemblyName>json-ld.net</AssemblyName>

test/json-ld.net.tests/ConformanceTests.cs

+12-35
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
54

65
using Newtonsoft.Json.Linq;
76
using Xunit;
8-
using Xunit.Extensions;
97
using System.IO;
10-
using Newtonsoft.Json;
118
using JsonLD.Core;
129
using JsonLD.Util;
1310

@@ -16,7 +13,7 @@ namespace JsonLD.Test
1613
public class ConformanceTests
1714
{
1815
[Theory, ClassData(typeof(ConformanceCases))]
19-
public void ConformanceTestPasses(string id, string testname, ConformanceCase conformanceCase)
16+
public void ConformanceTestPasses(string id, ConformanceCase conformanceCase)
2017
{
2118
JToken result = conformanceCase.run();
2219
if (conformanceCase.error != null)
@@ -77,20 +74,21 @@ public ConformanceCases()
7774

7875
public IEnumerator<object[]> GetEnumerator()
7976
{
77+
var jsonFetcher = new JsonFetcher();
78+
var rootDirectory = "W3C";
79+
8080
foreach (string manifest in manifests)
8181
{
82-
JToken manifestJson;
83-
84-
manifestJson = GetJson(manifest);
82+
JToken manifestJson = jsonFetcher.GetJson(manifest, rootDirectory);
8583

8684
foreach (JObject testcase in manifestJson["sequence"])
8785
{
8886
Func<JToken> run;
8987
ConformanceCase newCase = new ConformanceCase();
9088

91-
newCase.input = GetJson(testcase["input"]);
92-
newCase.context = GetJson(testcase["context"]);
93-
newCase.frame = GetJson(testcase["frame"]);
89+
newCase.input = jsonFetcher.GetJson(testcase["input"], rootDirectory);
90+
newCase.context = jsonFetcher.GetJson(testcase["context"], rootDirectory);
91+
newCase.frame = jsonFetcher.GetJson(testcase["frame"], rootDirectory);
9492

9593
var options = new JsonLdOptions("http://json-ld.org/test-suite/tests/" + (string)testcase["input"]);
9694

@@ -109,11 +107,11 @@ public IEnumerator<object[]> GetEnumerator()
109107
else if (testType.Any((s) => (string)s == "jld:FromRDFTest"))
110108
{
111109
newCase.input = File.ReadAllText(Path.Combine("W3C", (string)testcase["input"]));
112-
newCase.output = GetJson(testcase["expect"]);
110+
newCase.output = jsonFetcher.GetJson(testcase["expect"], rootDirectory);
113111
}
114112
else
115113
{
116-
newCase.output = GetJson(testcase["expect"]);
114+
newCase.output = jsonFetcher.GetJson(testcase["expect"], rootDirectory);
117115
}
118116
}
119117
else
@@ -138,7 +136,7 @@ public IEnumerator<object[]> GetEnumerator()
138136
}
139137
if (optionDescription.TryGetValue("expandContext", out value))
140138
{
141-
newCase.context = GetJson(testcase["option"]["expandContext"]);
139+
newCase.context = jsonFetcher.GetJson(testcase["option"]["expandContext"], rootDirectory);
142140
options.SetExpandContext((JObject)newCase.context);
143141
}
144142
if (optionDescription.TryGetValue("produceGeneralizedRdf", out value))
@@ -227,32 +225,11 @@ public IEnumerator<object[]> GetEnumerator()
227225

228226
newCase.run = run;
229227

230-
yield return new object[] { manifest + (string)testcase["@id"], (string)testcase["name"], newCase };
228+
yield return new object[] { manifest + (string)testcase["@id"], newCase };
231229
}
232230
}
233231
}
234232

235-
private JToken GetJson(JToken j)
236-
{
237-
try
238-
{
239-
if (j == null || j.Type == JTokenType.Null) return null;
240-
using (Stream manifestStream = File.OpenRead(Path.Combine("W3C", (string)j)))
241-
using (TextReader reader = new StreamReader(manifestStream))
242-
using (JsonReader jreader = new Newtonsoft.Json.JsonTextReader(reader)
243-
{
244-
DateParseHandling = DateParseHandling.None
245-
})
246-
{
247-
return JToken.ReadFrom(jreader);
248-
}
249-
}
250-
catch (Exception e)
251-
{ // TODO: this should not be here, figure out why this is needed or catch specific exception.
252-
return null;
253-
}
254-
}
255-
256233
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
257234
{
258235
throw new Exception("auggh");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"quads": [
3+
{
4+
"graph": "http://example.org/node/3",
5+
"subject": "http://example.org/object/3",
6+
"predicate": "http://example.org/value",
7+
"value": "n3-o3-value"
8+
},
9+
{
10+
"graph": "http://example.org/node/3",
11+
"subject": "http://example.org/object/1",
12+
"predicate": "http://example.org/value",
13+
"value": "n3-o1-value"
14+
},
15+
{
16+
"graph": "http://example.org/node/3",
17+
"subject": "http://example.org/object/2",
18+
"predicate": "http://example.org/value",
19+
"value": "n3-o2-value"
20+
},
21+
{
22+
"graph": "http://example.org/node/1",
23+
"subject": "http://example.org/object/3",
24+
"predicate": "http://example.org/value",
25+
"value": "n1-o3-value"
26+
},
27+
{
28+
"graph": "http://example.org/node/1",
29+
"subject": "http://example.org/object/1",
30+
"predicate": "http://example.org/value",
31+
"value": "n1-o1-value"
32+
},
33+
{
34+
"graph": "http://example.org/node/1",
35+
"subject": "http://example.org/object/2",
36+
"predicate": "http://example.org/value",
37+
"value": "n1-o2-value"
38+
},
39+
{
40+
"graph": "http://example.org/node/2",
41+
"subject": "http://example.org/object/3",
42+
"predicate": "http://example.org/value",
43+
"value": "n2-o3-value"
44+
},
45+
{
46+
"graph": "http://example.org/node/2",
47+
"subject": "http://example.org/object/1",
48+
"predicate": "http://example.org/value",
49+
"value": "n2-o1-value"
50+
},
51+
{
52+
"graph": "http://example.org/node/2",
53+
"subject": "http://example.org/object/2",
54+
"predicate": "http://example.org/value",
55+
"value": "n2-o2-value"
56+
}
57+
]
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"@type": "mf:Manifest",
3+
"name": "From RDF",
4+
"description": "JSON-LD sorting graphs and nodes when running FromRDF",
5+
"input": "fromRdf-in.json",
6+
"sequence": [
7+
{
8+
"@id": "#t0001",
9+
"sort-type": "jld:GraphsAndNodes",
10+
"test-type": "jld:FromRDF",
11+
"name": "sort graphs and nodes",
12+
"purpose": "graphs and nodes sorted when running FromRDF",
13+
"expect": "fromRdf-out-sort-graphs-and-nodes.jsonld"
14+
},
15+
{
16+
"@id": "#t0002",
17+
"sort-type": "jld:Graphs",
18+
"test-type": "jld:FromRDF",
19+
"name": "sort graphs only",
20+
"purpose": "graphs sorted when running FromRDF",
21+
"expect": "fromRdf-out-sort-graphs.jsonld"
22+
},
23+
{
24+
"@id": "#t0003",
25+
"sort-type": "jld:Nodes",
26+
"test-type": "jld:FromRDF",
27+
"name": "sort graph nodes only",
28+
"purpose": "graph nodes sorted when running FromRDF",
29+
"expect": "fromRdf-out-sort-graph-nodes.jsonld"
30+
},
31+
{
32+
"@id": "#t0004",
33+
"sort-type": "jld:None",
34+
"test-type": "jld:FromRDF",
35+
"name": "sort nothing",
36+
"purpose": "sort nothing running FromRDF",
37+
"expect": "fromRdf-out-no-sorting.jsonld"
38+
}
39+
]
40+
}

0 commit comments

Comments
 (0)