Skip to content

Commit 7dc9bec

Browse files
committed
Conformance cases pass for ToRDF, FromRDF, and mostly pass for Normalize.
1 parent 74446a3 commit 7dc9bec

18 files changed

+220
-132
lines changed

JsonLD.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{BADA6F
1414
EndProject
1515
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonLD.Test", "tests\JsonLD.Test\JsonLD.Test.csproj", "{D41C622A-4470-4BE1-AB20-D9862B628840}"
1616
EndProject
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonLD.Portable", "JsonLD.Portable\JsonLD.Portable.csproj", "{F119722F-5E6A-4479-A4CF-0CE00FF29737}"
18+
EndProject
1719
Global
1820
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1921
Debug|Any CPU = Debug|Any CPU
@@ -28,6 +30,10 @@ Global
2830
{D41C622A-4470-4BE1-AB20-D9862B628840}.Debug|Any CPU.Build.0 = Debug|Any CPU
2931
{D41C622A-4470-4BE1-AB20-D9862B628840}.Release|Any CPU.ActiveCfg = Release|Any CPU
3032
{D41C622A-4470-4BE1-AB20-D9862B628840}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{F119722F-5E6A-4479-A4CF-0CE00FF29737}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34+
{F119722F-5E6A-4479-A4CF-0CE00FF29737}.Debug|Any CPU.Build.0 = Debug|Any CPU
35+
{F119722F-5E6A-4479-A4CF-0CE00FF29737}.Release|Any CPU.ActiveCfg = Release|Any CPU
36+
{F119722F-5E6A-4479-A4CF-0CE00FF29737}.Release|Any CPU.Build.0 = Release|Any CPU
3137
EndGlobalSection
3238
GlobalSection(SolutionProperties) = preSolution
3339
HideSolutionNode = FALSE

src/JsonLD/Core/Context.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ namespace JsonLD.Core
1313
/// </summary>
1414
/// <author>tristan</author>
1515
//[System.Serializable]
16-
public class Context : JObject, ICloneable
16+
public class Context : JObject
17+
#if !PORTABLE
18+
, ICloneable
19+
#endif
1720
{
1821
private JsonLdOptions options;
1922

src/JsonLD/Core/DocumentLoader.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class DocumentLoader
1414
/// <exception cref="JsonLDNet.Core.JsonLdError"></exception>
1515
public virtual RemoteDocument LoadDocument(string url)
1616
{
17+
#if !PORTABLE
1718
RemoteDocument doc = new RemoteDocument(url, null);
1819
try
1920
{
@@ -58,6 +59,9 @@ public virtual RemoteDocument LoadDocument(string url)
5859
throw new JsonLdError(JsonLdError.Error.LoadingDocumentFailed, url);
5960
}
6061
return doc;
62+
#else
63+
throw new NotImplementedException();
64+
#endif
6165
}
6266

6367
/// <summary>An HTTP Accept header that prefers JSONLD.</summary>

src/JsonLD/Core/IJSONLDTripleCallback.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ public interface IJSONLDTripleCallback
3636
/// used).
3737
/// </param>
3838
/// <returns>the resulting RDF object in the desired format</returns>
39-
JToken Call(RDFDataset dataset);
39+
object Call(RDFDataset dataset);
4040
}
4141
}

src/JsonLD/Core/JsonLdApi.cs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,7 @@ public virtual JArray FromRDF(RDFDataset dataset)
19441944
// 3/3.1)
19451945
foreach (string name in dataset.GraphNames())
19461946
{
1947-
JArray graph = dataset.GetQuads(name);
1947+
IList<RDFDataset.Quad> graph = dataset.GetQuads(name);
19481948
// 3.2+3.4)
19491949
JObject nodeMap;
19501950
if (!graphMap.ContainsKey(name))
@@ -2139,40 +2139,41 @@ public virtual RDFDataset ToRDF()
21392139
return dataset;
21402140
}
21412141

2142+
#if !PORTABLE
21422143
/// <summary>Performs RDF normalization on the given JSON-LD input.</summary>
21432144
/// <remarks>Performs RDF normalization on the given JSON-LD input.</remarks>
21442145
/// <param name="input">the expanded JSON-LD object to normalize.</param>
21452146
/// <param name="options">the normalization options.</param>
21462147
/// <param name="callback">(err, normalized) called once the operation completes.</param>
21472148
/// <exception cref="JSONLDProcessingError">JSONLDProcessingError</exception>
21482149
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
2149-
public virtual JToken Normalize(JObject dataset)
2150+
public virtual object Normalize(RDFDataset dataset)
21502151
{
21512152
// create quads and map bnodes to their associated quads
2152-
JArray quads = new JArray();
2153-
JObject bnodes = new JObject();
2154-
foreach (string graphName in dataset.GetKeys())
2153+
IList<RDFDataset.Quad> quads = new List<RDFDataset.Quad>();
2154+
IDictionary<string,IDictionary<string,object>> bnodes = new Dictionary<string,IDictionary<string,object>>();
2155+
foreach (string graphName in dataset.Keys)
21552156
{
21562157
var eachGraphName = graphName;
2157-
JArray triples = (JArray)dataset[eachGraphName];
2158+
IList<RDFDataset.Quad> triples = (IList<RDFDataset.Quad>)dataset[eachGraphName];
21582159
if ("@default".Equals(eachGraphName))
21592160
{
21602161
eachGraphName = null;
21612162
}
2162-
foreach (JObject quad in triples)
2163+
foreach (RDFDataset.Quad quad in triples)
21632164
{
21642165
if (eachGraphName != null)
21652166
{
21662167
if (eachGraphName.IndexOf("_:") == 0)
21672168
{
2168-
JObject tmp = new JObject();
2169+
IDictionary<string, object> tmp = new Dictionary<string, object>();
21692170
tmp["type"] = "blank node";
21702171
tmp["value"] = eachGraphName;
21712172
quad["name"] = tmp;
21722173
}
21732174
else
21742175
{
2175-
JObject tmp = new JObject();
2176+
IDictionary<string, object> tmp = new Dictionary<string, object>();
21762177
tmp["type"] = "IRI";
21772178
tmp["value"] = eachGraphName;
21782179
quad["name"] = tmp;
@@ -2182,33 +2183,23 @@ public virtual JToken Normalize(JObject dataset)
21822183
string[] attrs = new string[] { "subject", "object", "name" };
21832184
foreach (string attr in attrs)
21842185
{
2185-
if (quad.ContainsKey(attr) && ((JObject)quad
2186-
[attr])["type"].SafeCompare("blank node"))
2186+
if (quad.ContainsKey(attr) && (string)((IDictionary<string,object>)quad[attr])["type"] == "blank node")
21872187
{
2188-
string id = (string)((JObject)quad[attr])["value"];
2188+
string id = (string)((IDictionary<string,object>)quad[attr])["value"];
21892189
if (!bnodes.ContainsKey(id))
21902190
{
2191-
bnodes[id] = new _Dictionary_1910();
2191+
bnodes[id] = new Dictionary<string,object> { {"quads", new List<RDFDataset.Quad>()} };
21922192
}
2193-
((JArray)((JObject)bnodes[id])["quads"]).Add(quad);
2193+
((IList<RDFDataset.Quad>)bnodes[id]["quads"]).Add(quad);
21942194
}
21952195
}
21962196
}
21972197
}
21982198
// mapping complete, start canonical naming
21992199
NormalizeUtils normalizeUtils = new NormalizeUtils(quads, bnodes, new UniqueNamer
22002200
("_:c14n"), opts);
2201-
return normalizeUtils.HashBlankNodes(bnodes.GetKeys());
2202-
}
2203-
2204-
private sealed class _Dictionary_1910 : JObject
2205-
{
2206-
public _Dictionary_1910()
2207-
{
2208-
{
2209-
this["quads"] = new JArray();
2210-
}
2211-
}
2201+
return normalizeUtils.HashBlankNodes(bnodes.Keys);
22122202
}
2203+
#endif
22132204
}
22142205
}

src/JsonLD/Core/JsonLdProcessor.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ public static JToken FromRDF(JToken input, IRDFParser parser)
385385
/// <?></?>
386386
/// <param name="callback">(err, dataset) called once the operation completes.</param>
387387
/// <exception cref="JsonLDNet.Core.JsonLdError"></exception>
388-
public static JToken ToRDF(JToken input, IJSONLDTripleCallback callback, JsonLdOptions
388+
public static object ToRDF(JToken input, IJSONLDTripleCallback callback, JsonLdOptions
389389
options)
390390
{
391391
JToken expandedInput = Expand(input, options);
@@ -438,23 +438,24 @@ public static JToken ToRDF(JToken input, IJSONLDTripleCallback callback, JsonLdO
438438
}
439439

440440
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
441-
public static JToken ToRDF(JToken input, JsonLdOptions options)
441+
public static object ToRDF(JToken input, JsonLdOptions options)
442442
{
443443
return ToRDF(input, null, options);
444444
}
445445

446446
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
447-
public static JToken ToRDF(JToken input, IJSONLDTripleCallback callback)
447+
public static object ToRDF(JToken input, IJSONLDTripleCallback callback)
448448
{
449449
return ToRDF(input, callback, new JsonLdOptions(string.Empty));
450450
}
451451

452452
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
453-
public static JToken ToRDF(JToken input)
453+
public static object ToRDF(JToken input)
454454
{
455455
return ToRDF(input, new JsonLdOptions(string.Empty));
456456
}
457457

458+
#if !PORTABLE
458459
/// <summary>Performs RDF dataset normalization on the given JSON-LD input.</summary>
459460
/// <remarks>
460461
/// Performs RDF dataset normalization on the given JSON-LD input. The output
@@ -465,7 +466,7 @@ public static JToken ToRDF(JToken input)
465466
/// <param name="callback">(err, normalized) called once the operation completes.</param>
466467
/// <exception cref="JSONLDProcessingError">JSONLDProcessingError</exception>
467468
/// <exception cref="JsonLDNet.Core.JsonLdError"></exception>
468-
public static JToken Normalize(JToken input, JsonLdOptions options)
469+
public static object Normalize(JToken input, JsonLdOptions options)
469470
{
470471
JsonLdOptions opts = options.Clone();
471472
opts.format = null;
@@ -474,9 +475,11 @@ public static JToken Normalize(JToken input, JsonLdOptions options)
474475
}
475476

476477
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
477-
public static JToken Normalize(JToken input)
478+
public static object Normalize(JToken input)
478479
{
479480
return Normalize(input, new JsonLdOptions(string.Empty));
480481
}
482+
483+
#endif
481484
}
482485
}

src/JsonLD/Core/NormalizeUtils.cs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66

77
namespace JsonLD.Core
88
{
9+
#if !PORTABLE
910
internal class NormalizeUtils
1011
{
1112
private readonly UniqueNamer namer;
1213

13-
private readonly JObject bnodes;
14+
private readonly IDictionary<string,IDictionary<string,object>> bnodes;
1415

15-
private readonly JArray quads;
16+
private readonly IList<RDFDataset.Quad> quads;
1617

1718
private readonly JsonLdOptions options;
1819

19-
public NormalizeUtils(JArray quads, JObject bnodes, UniqueNamer
20+
public NormalizeUtils(IList<RDFDataset.Quad> quads, IDictionary<string, IDictionary<string, object>> bnodes, UniqueNamer
2021
namer, JsonLdOptions options)
2122
{
2223
this.options = options;
@@ -27,7 +28,7 @@ public NormalizeUtils(JArray quads, JObject bnodes, UniqueNamer
2728

2829
// generates unique and duplicate hashes for bnodes
2930
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
30-
public virtual JToken HashBlankNodes(IEnumerable<string> unnamed_)
31+
public virtual object HashBlankNodes(IEnumerable<string> unnamed_)
3132
{
3233
IList<string> unnamed = new List<string>(unnamed_);
3334
IList<string> nextUnnamed = new List<string>();
@@ -91,22 +92,21 @@ public virtual JToken HashBlankNodes(IEnumerable<string> unnamed_)
9192
// update bnode names in each quad and serialize
9293
for (int cai = 0; cai < quads.Count; ++cai)
9394
{
94-
JObject quad = (JObject)quads[cai];
95+
RDFDataset.Quad quad = quads[cai];
9596
foreach (string attr in new string[] { "subject", "object", "name" })
9697
{
9798
if (quad.ContainsKey(attr))
9899
{
99-
JObject qa = (JObject)quad[attr];
100-
if (qa != null && qa["type"].SafeCompare("blank node") && ((string)qa["value"]).IndexOf
100+
IDictionary<string,object> qa = (IDictionary<string,object>)quad[attr];
101+
if (qa != null && (string)qa["type"] == "blank node" && ((string)qa["value"]).IndexOf
101102
("_:c14n") != 0)
102103
{
103-
qa["value"] = namer.GetName((string)qa[("value")]);
104+
qa["value"] = namer.GetName((string)qa["value"]);
104105
}
105106
}
106107
}
107-
normalized.Add(RDFDatasetUtils.ToNQuad((RDFDataset.Quad)quad, quad.ContainsKey("name"
108-
) && !quad["name"].IsNull() ? (string)((JObject)quad["name"])[
109-
"value"] : null));
108+
normalized.Add(RDFDatasetUtils.ToNQuad(quad, quad.ContainsKey("name"
109+
) && !(quad["name"] == null) ? (string)((IDictionary<string,object>)((IDictionary<string,object>)quad)["name"])["value"] : null));
110110
}
111111
// sort normalized output
112112
normalized.SortInPlace();
@@ -239,22 +239,22 @@ private class HashResult
239239
/// <param name="namer">the canonical bnode namer.</param>
240240
/// <param name="pathNamer">the namer used to assign names to adjacent bnodes.</param>
241241
/// <param name="callback">(err, result) called once the operation completes.</param>
242-
private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, UniqueNamer namer, UniqueNamer pathNamer)
242+
private static NormalizeUtils.HashResult HashPaths(string id, IDictionary<string, IDictionary<string, object>> bnodes, UniqueNamer namer, UniqueNamer pathNamer)
243243
{
244244
try
245245
{
246246
// create SHA-1 digest
247247
MessageDigest md = MessageDigest.GetInstance("SHA-1");
248248
JObject groups = new JObject();
249249
IList<string> groupHashes;
250-
JArray quads = (JArray)((JObject)bnodes[id])["quads"];
250+
IList<RDFDataset.Quad> quads = (IList<RDFDataset.Quad>)bnodes[id]["quads"];
251251
for (int hpi = 0; ; hpi++)
252252
{
253253
if (hpi == quads.Count)
254254
{
255255
// done , hash groups
256256
groupHashes = new List<string>(groups.GetKeys());
257-
groupHashes.SortInPlace();
257+
((List<string>)groupHashes).Sort(StringComparer.CurrentCultureIgnoreCase);
258258
for (int hgi = 0; ; hgi++)
259259
{
260260
if (hgi == groupHashes.Count)
@@ -381,8 +381,8 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
381381
}
382382
}
383383
// get adjacent bnode
384-
JObject quad = (JObject)quads[hpi];
385-
string bnode_2 = GetAdjacentBlankNodeName((JObject)quad["subject"
384+
IDictionary<string,object> quad = (IDictionary<string,object>)quads[hpi];
385+
string bnode_2 = GetAdjacentBlankNodeName((IDictionary<string, object>)quad["subject"
386386
], id);
387387
string direction = null;
388388
if (bnode_2 != null)
@@ -392,7 +392,7 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
392392
}
393393
else
394394
{
395-
bnode_2 = GetAdjacentBlankNodeName((JObject)quad["object"], id
395+
bnode_2 = GetAdjacentBlankNodeName((IDictionary<string, object>)quad["object"], id
396396
);
397397
if (bnode_2 != null)
398398
{
@@ -424,7 +424,7 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
424424
// String toHash = direction + (String) ((Map<String,
425425
// Object>) quad.get("predicate")).get("value") + name;
426426
md1.Update(JsonLD.JavaCompat.GetBytesForString(direction, "UTF-8"));
427-
md1.Update(JsonLD.JavaCompat.GetBytesForString(((string)((JObject)quad["predicate"])["value"]), "UTF-8"));
427+
md1.Update(JsonLD.JavaCompat.GetBytesForString(((string)((IDictionary<string,object>)quad["predicate"])["value"]), "UTF-8"));
428428
md1.Update(JsonLD.JavaCompat.GetBytesForString(name, "UTF-8"));
429429
string groupHash = EncodeHex(md1.Digest());
430430
if (groups.ContainsKey(groupHash))
@@ -455,28 +455,27 @@ private static NormalizeUtils.HashResult HashPaths(string id, JObject bnodes, Un
455455
/// <param name="bnodes">the mapping of bnodes to quads.</param>
456456
/// <param name="namer">the canonical bnode namer.</param>
457457
/// <returns>the new hash.</returns>
458-
private static string HashQuads(string id, JObject bnodes, UniqueNamer
458+
private static string HashQuads(string id, IDictionary<string, IDictionary<string, object>> bnodes, UniqueNamer
459459
namer)
460460
{
461461
// return cached hash
462-
if (((JObject)bnodes[id]).ContainsKey("hash"))
462+
if (bnodes[id].ContainsKey("hash"))
463463
{
464-
return (string)((JObject)bnodes[id])["hash"];
464+
return (string)bnodes[id]["hash"];
465465
}
466466
// serialize all of bnode's quads
467-
JArray quads = (JArray)((JObject)bnodes[id])["quads"];
467+
IList<RDFDataset.Quad> quads = (IList<RDFDataset.Quad>)bnodes[id]["quads"];
468468
IList<string> nquads = new List<string>();
469469
for (int i = 0; i < quads.Count; ++i)
470470
{
471-
nquads.Add(RDFDatasetUtils.ToNQuad((RDFDataset.Quad)quads[i], quads[i]["name"] !=
472-
null ? (string)((JObject)quads[i]["name"])["value"] : null,
473-
id));
471+
object name;
472+
nquads.Add(RDFDatasetUtils.ToNQuad((RDFDataset.Quad)quads[i], quads[i].TryGetValue("name", out name) ? (string)((IDictionary<string,object>)name)["value"] : null, id));
474473
}
475474
// sort serialized quads
476475
nquads.SortInPlace();
477476
// return hashed quads
478477
string hash = Sha1hash(nquads);
479-
((JObject)bnodes[id])["hash"] = hash;
478+
((IDictionary<string,object>)bnodes[id])["hash"] = hash;
480479
return hash;
481480
}
482481

@@ -511,7 +510,7 @@ private static string EncodeHex(byte[] data)
511510
string rval = string.Empty;
512511
foreach (byte b in data)
513512
{
514-
rval += string.Format("%02x", b);
513+
rval += b.ToString("x2");
515514
}
516515
return rval;
517516
}
@@ -528,10 +527,9 @@ private static string EncodeHex(byte[] data)
528527
/// <param name="node">the RDF quad node.</param>
529528
/// <param name="id">the ID of the blank node to look next to.</param>
530529
/// <returns>the adjacent blank node name or null if none was found.</returns>
531-
private static string GetAdjacentBlankNodeName(JObject node,
532-
string id)
530+
private static string GetAdjacentBlankNodeName(IDictionary<string,object> node, string id)
533531
{
534-
return node["type"].SafeCompare("blank node") && (!node.ContainsKey("value") || !node["value"].SafeCompare(id)) ? (string)node["value"] : null;
532+
return (string)node["type"] == "blank node" && (!node.ContainsKey("value") || (string)node["value"] == id) ? (string)node["value"] : null;
535533
}
536534

537535
private class Permutator
@@ -614,4 +612,5 @@ public virtual JArray Next()
614612
}
615613
}
616614
}
615+
#endif
617616
}

0 commit comments

Comments
 (0)