Skip to content

Commit a04af73

Browse files
committed
Makes lexicographical sorting optional, preserving original object order instead
1 parent 6bb4271 commit a04af73

File tree

3 files changed

+79
-12
lines changed

3 files changed

+79
-12
lines changed

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

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ public virtual JToken Compact(Context activeCtx, string activeProperty, JToken e
122122
JObject result = new JObject();
123123
// 7)
124124
JArray keys = new JArray(element.GetKeys());
125-
keys.SortInPlace();
125+
126+
if (!opts.GetPreserveOrder())
127+
{
128+
keys.SortInPlace();
129+
}
130+
126131
foreach (string expandedProperty in keys)
127132
{
128133
JToken expandedValue = elem[expandedProperty];
@@ -491,7 +496,12 @@ public virtual JToken Expand(Context activeCtx, string activeProperty, JToken el
491496
JObject result = new JObject();
492497
// 7)
493498
JArray keys = new JArray(element.GetKeys());
494-
keys.SortInPlace();
499+
500+
if (!opts.GetPreserveOrder())
501+
{
502+
keys.SortInPlace();
503+
}
504+
495505
foreach (string key in keys)
496506
{
497507
JToken value = elem[key];
@@ -808,7 +818,12 @@ public virtual JToken Expand(Context activeCtx, string activeProperty, JToken el
808818
expandedValue = new JArray();
809819
// 7.6.2)
810820
JArray indexKeys = new JArray(value.GetKeys());
811-
indexKeys.SortInPlace();
821+
822+
if (!opts.GetPreserveOrder())
823+
{
824+
indexKeys.SortInPlace();
825+
}
826+
812827
foreach (string index in indexKeys)
813828
{
814829
JToken indexValue = ((JObject)value)[index];
@@ -1290,7 +1305,12 @@ private void GenerateNodeMap(JToken element, JObject nodeMap,
12901305
}
12911306
// 6.11)
12921307
JArray keys = new JArray(element.GetKeys());
1293-
keys.SortInPlace();
1308+
1309+
if (!opts.GetPreserveOrder())
1310+
{
1311+
keys.SortInPlace();
1312+
}
1313+
12941314
foreach (string property_1 in keys)
12951315
{
12961316
var eachProperty_1 = property_1;
@@ -1405,7 +1425,7 @@ public virtual JArray Frame(JToken input, JArray frame)
14051425
{
14061426
state.omitDefault = this.opts.GetOmitDefault().Value;
14071427
}
1408-
// use tree map so keys are sotred by default
1428+
// use tree map so keys are sorted by default
14091429
// XXX BUG BUG BUG XXX (sblom) Figure out where this needs to be sorted and use extension methods to return sorted enumerators or something!
14101430
JObject nodes = new JObject();
14111431
GenerateNodeMap(input, nodes);
@@ -1436,7 +1456,12 @@ private void Frame(JsonLdApi.FramingContext state, JObject nodes
14361456
bool explicitOn = GetFrameFlag(frame, "@explicit", state.@explicit);
14371457
// add matches to output
14381458
JArray ids = new JArray(matches.GetKeys());
1439-
ids.SortInPlace();
1459+
1460+
if (!opts.GetPreserveOrder())
1461+
{
1462+
ids.SortInPlace();
1463+
}
1464+
14401465
foreach (string id in ids)
14411466
{
14421467
if (property == null)
@@ -1499,7 +1524,12 @@ private void Frame(JsonLdApi.FramingContext state, JObject nodes
14991524
// iterate over subject properties
15001525
JObject element = (JObject)matches[id];
15011526
JArray props = new JArray(element.GetKeys());
1502-
props.SortInPlace();
1527+
1528+
if (!opts.GetPreserveOrder())
1529+
{
1530+
props.SortInPlace();
1531+
}
1532+
15031533
foreach (string prop in props)
15041534
{
15051535
// copy keywords to output
@@ -1576,7 +1606,12 @@ private void Frame(JsonLdApi.FramingContext state, JObject nodes
15761606
}
15771607
// handle defaults
15781608
props = new JArray(frame.GetKeys());
1579-
props.SortInPlace();
1609+
1610+
if (!opts.GetPreserveOrder())
1611+
{
1612+
props.SortInPlace();
1613+
}
1614+
15801615
foreach (string prop_1 in props)
15811616
{
15821617
// skip keywords
@@ -2116,7 +2151,12 @@ public virtual JArray FromRDF(RDFDataset dataset)
21162151
JArray result = new JArray();
21172152
// 6)
21182153
JArray ids = new JArray(defaultGraph.GetKeys());
2119-
ids.SortInPlace();
2154+
2155+
if (!opts.GetPreserveOrder())
2156+
{
2157+
ids.SortInPlace();
2158+
}
2159+
21202160
foreach (string subject_1 in ids)
21212161
{
21222162
JsonLdApi.NodeMapNode node = (NodeMapNode)defaultGraph[subject_1];
@@ -2127,7 +2167,12 @@ public virtual JArray FromRDF(RDFDataset dataset)
21272167
node["@graph"] = new JArray();
21282168
// 6.1.2)
21292169
JArray keys = new JArray(graphMap[subject_1].GetKeys());
2130-
keys.SortInPlace();
2170+
2171+
if (!opts.GetPreserveOrder())
2172+
{
2173+
keys.SortInPlace();
2174+
}
2175+
21312176
foreach (string s in keys)
21322177
{
21332178
JsonLdApi.NodeMapNode n = (NodeMapNode)graphMap[subject_1][s];

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public virtual JsonLD.Core.JsonLdOptions Clone()
4343

4444
private bool produceGeneralizedRdf = false;
4545

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

152+
public virtual bool GetPreserveOrder()
153+
{
154+
return preserveOrder;
155+
}
156+
157+
public virtual void SetPreserveOrder(bool preserveOrder)
158+
{
159+
this.preserveOrder = preserveOrder;
160+
}
161+
150162
public string format = null;
151163

152164
public bool useNamespaces = false;

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,12 @@ public static JToken Flatten(JToken input, JToken context, JsonLdOptions opts)
201201
entry["@graph"] = new JArray();
202202
}
203203
JArray keys = new JArray(graph.GetKeys());
204-
keys.SortInPlace();
204+
205+
if (!opts.GetPreserveOrder())
206+
{
207+
keys.SortInPlace();
208+
}
209+
205210
foreach (string id in keys)
206211
{
207212
JObject node = (JObject)graph[id];
@@ -215,7 +220,12 @@ public static JToken Flatten(JToken input, JToken context, JsonLdOptions opts)
215220
JArray flattened = new JArray();
216221
// 6)
217222
JArray keys_1 = new JArray(defaultGraph.GetKeys());
218-
keys_1.SortInPlace();
223+
224+
if (!opts.GetPreserveOrder())
225+
{
226+
keys_1.SortInPlace();
227+
}
228+
219229
foreach (string id_1 in keys_1)
220230
{
221231
JObject node = (JObject)defaultGraph[id_1

0 commit comments

Comments
 (0)