From 24ed4afad7b860a84c0f9b1c1d22973262fc0999 Mon Sep 17 00:00:00 2001 From: Karol Tarasiuk Date: Fri, 8 Apr 2016 15:48:44 +1000 Subject: [PATCH 1/4] [BUG] Fixed JsonObject methods and functionality + added unit tests for coverage --- LearnositySDK/Utils/JsonObject.cs | 45 ++- LearnositySDK/Utils/JsonObjectFactory.cs | 89 +++--- UnitTestProject1/CheckingEqualityUnitTests.cs | 128 ++++++++ UnitTestProject1/LearnositySDKUnitTests.cs | 64 +--- .../LearnositySDKUnitTests.csproj | 2 + UnitTestProject1/MergingObjectsUnitTests.cs | 283 ++++++++++++++++++ 6 files changed, 499 insertions(+), 112 deletions(-) create mode 100644 UnitTestProject1/CheckingEqualityUnitTests.cs create mode 100644 UnitTestProject1/MergingObjectsUnitTests.cs diff --git a/LearnositySDK/Utils/JsonObject.cs b/LearnositySDK/Utils/JsonObject.cs index fcee1dd..6bd50da 100755 --- a/LearnositySDK/Utils/JsonObject.cs +++ b/LearnositySDK/Utils/JsonObject.cs @@ -90,6 +90,27 @@ public void set(bool value) this.arrayIndex++; } + /// + /// Sets/adds the value + /// + /// + /// + public void set(JToken value) + { + this.set(this.arrayIndex, value); + this.arrayIndex++; + } + + /// + /// Sets/adds NULL + /// + /// + /// + public void setNull() + { + this.set(JToken.Parse("null")); + } + /// /// Sets/adds value with given type /// @@ -161,6 +182,16 @@ public void set(int key, JToken value) this.set(key.ToString(), value); } + /// + /// Sets/adds NULL + /// + /// + /// + public void setNull(int key) + { + this.set(key, JToken.Parse("null")); + } + /// /// Sets/adds the value /// @@ -198,7 +229,7 @@ public void set(string key, string value) /// public void set(string key, JsonObject value) { - if (this.isArray()) + if (value.isArray()) { this.set("JsonArray", key, value); } @@ -218,6 +249,16 @@ public void set(string key, JToken value) this.set("NULL", key, value); } + /// + /// Sets/adds NULL + /// + /// + /// + public void setNull(string key) + { + this.set(key, JToken.Parse("null")); + } + /// /// Sets/adds the value /// @@ -518,6 +559,8 @@ public string[] getKeys() l.Add(item.Key); } + l.Sort(); + return l.ToArray(); } diff --git a/LearnositySDK/Utils/JsonObjectFactory.cs b/LearnositySDK/Utils/JsonObjectFactory.cs index 40318bc..08069c7 100755 --- a/LearnositySDK/Utils/JsonObjectFactory.cs +++ b/LearnositySDK/Utils/JsonObjectFactory.cs @@ -147,6 +147,7 @@ public static JsonObject merge(JsonObject obj1, JsonObject obj2, bool overwrite { JsonObject newObj = null; + // for arrays we just insert elements from the second array to the first one and return a new array if (obj1.isArray() && obj2.isArray()) { newObj = JsonObjectFactory.mergeArrays(obj1, obj2); @@ -164,68 +165,49 @@ public static JsonObject merge(JsonObject obj1, JsonObject obj2, bool overwrite foreach (string key in keys) { - if (recursive) - { - string type1 = ""; - string type2 = ""; - Object temp1Object = obj1.get(key, ref type1); - Object temp2Object = obj2.get(key, ref type2); - - // recursive merge makes sense only when both items are associative arrays - // if not regular merge will be performed - if ( - (type1 == "JsonObject" || type1 == "JsonArray") && - (type2 == "JsonObject" || type2 == "JsonArray") ) - { - JsonObject temp1 = (JsonObject)temp1Object; - JsonObject temp2 = (JsonObject)temp2Object; - int temp1Count = temp1.count(); - int temp2Count = temp2.count(); - - if (temp1Count == 0 && temp2Count == 0) - { - type = "JsonObject"; - temp = new JsonObject(); - } - else if(temp1Count == 0) - { - type = type2; - temp = temp2; - } - else if(temp2Count == 0) - { - type = type1; - temp = temp1; - } - else if (type1 == "JsonArray" && type2 == "JsonArray") - { - type = "JsonArray"; - temp = JsonObjectFactory.merge(temp1, temp2, overwrite, recursive); - } - else - { - type = "JsonObject"; - temp = JsonObjectFactory.merge(temp1, temp2, overwrite, recursive); - } + string type1 = ""; + string type2 = ""; + Object temp1Object = obj1.get(key, ref type1); + Object temp2Object = obj2.get(key, ref type2); - newObj.set(type, key, temp); - continue; - } + // recursive merging for objects only + if (recursive && (type1 == "JsonObject" && type2 == "JsonObject")) + { + JsonObject temp1 = (JsonObject)temp1Object; + JsonObject temp2 = (JsonObject)temp2Object; + type = "JsonObject"; + temp = JsonObjectFactory.merge(temp1, temp2, overwrite, recursive); + } + // special treatment for arrays + else if (type1 == "JsonArray" && type2 == "JsonArray") + { + JsonObject temp1 = (JsonObject)temp1Object; + JsonObject temp2 = (JsonObject)temp2Object; + type = "JsonArray"; + temp = JsonObjectFactory.merge(temp1, temp2); } - - if (overwrite && Tools.in_array(key, keys2)) + // for the rest we simple insert the value depending on overwrite parameter + // overwrite is enabled and value is in the second object + else if (overwrite && Tools.in_array(key, keys2)) { temp = obj2.get(key, ref type); } + // value is in the first object else if (Tools.in_array(key, keys1)) { temp = obj1.get(key, ref type); } + // value is in the second object only else if (Tools.in_array(key, keys2)) { temp = obj2.get(key, ref type); } + if (type == "JsonObject" || type == "JsonArray") + { + temp = ((JsonObject)temp).Clone(); + } + newObj.set(type, key, temp); continue; } @@ -256,5 +238,16 @@ public static bool JSONEquality(JsonObject JSON1, JsonObject JSON2) { return JSONEquality(JSON1.toJson(), JSON2.toJson()); } + + /// + /// Checks whether two JsonObjects are equal + /// + /// first JsonObject to compare + /// second JsonObject to compare + /// + public static bool JSONEquality(JsonObject JSON1, string JSON2) + { + return JSONEquality(JSON1.toJson(), JSON2); + } } } diff --git a/UnitTestProject1/CheckingEqualityUnitTests.cs b/UnitTestProject1/CheckingEqualityUnitTests.cs new file mode 100644 index 0000000..e017531 --- /dev/null +++ b/UnitTestProject1/CheckingEqualityUnitTests.cs @@ -0,0 +1,128 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using LearnositySDK.Utils; +using Newtonsoft.Json.Linq; + +namespace UnitTestProject1 +{ + /// + /// Summary description for CheckingEqualityUnitTests + /// + [TestClass] + public class CheckingEqualityUnitTests + { + [TestMethod] + public void inputJSONEqualsOutput() + { + string JSON = "{\"boolean\":true,\"integer\":1,\"float\":1.2,\"string\":\"string\",\"object\":{\"property\":null},\"array\":[null,[2]]}"; + JsonObject jo = JsonObjectFactory.fromString(JSON); + string outputJSON = jo.toJson(); + + Assert.IsTrue( + JsonObjectFactory.JSONEquality(JSON, outputJSON), + "Input JSON (" + JSON + ") doesn't equal the output (" + outputJSON + ")" + ); + + JSON = "[0,1,2,3,4,5,6,7,8,9,10]"; + jo = JsonObjectFactory.fromString(JSON); + outputJSON = jo.toJson(); + + Assert.IsTrue( + JsonObjectFactory.JSONEquality(JSON, outputJSON), + "Input JSON (" + JSON + ") doesn't equal the output (" + outputJSON + ")" + ); + + JSON = "[0,\"1\",2,\"3\",4,\"5\",6,\"7\",8,\"9\",10]"; + jo = JsonObjectFactory.fromString(JSON); + outputJSON = jo.toJson(); + + Assert.IsTrue( + JsonObjectFactory.JSONEquality(JSON, outputJSON), + "Input JSON (" + JSON + ") doesn't equal the output (" + outputJSON + ")" + ); + } + + [TestMethod] + public void JSONEqualityMethodWorksProperly() + { + string JSON = "{\"boolean\":true,\"integer\":1,\"float\":1.2,\"string\":\"string\",\"object\":{\"property\":null},\"array\":[null,[2]]}"; + + // building a structure for comparison purposes + JsonObject jo = new JsonObject(); + JsonObject joInner = new JsonObject(); + JsonObject ja = new JsonObject(true); + JsonObject jaInner = new JsonObject(true); + jo.set("boolean", true); + jo.set("integer", 1); + jo.set("float", 1.2f); + jo.set("string", "string"); + jo.set("object", joInner); + jo.set("array", ja); + joInner.setNull("property"); + ja.setNull(); + ja.set(jaInner); + jaInner.set(2); + + Assert.IsTrue( + JsonObjectFactory.JSONEquality(JSON, JSON), + "JSONEquality method doesn't work correctly" + ); + + Assert.IsTrue( + JsonObjectFactory.JSONEquality(jo, jo), + "JSONEquality method doesn't work correctly" + ); + + Assert.IsTrue( + JsonObjectFactory.JSONEquality(jo, JSON), + "JSONEquality method doesn't work correctly" + ); + + JsonObject jo1 = new JsonObject(); + JsonObject jo2 = new JsonObject(); + JsonObject jo3 = new JsonObject(); + + jo1.set("a", "a"); + jo1.set("b", "b"); + jo1.set("c", "c"); + + jo2.set("d", "d"); + jo2.set("e", "e"); + jo2.set("f", "f"); + + jo3.set("a", "d"); + jo3.set("b", "e"); + jo3.set("c", "f"); + + Assert.IsFalse( + JsonObjectFactory.JSONEquality(jo1, jo2), + "JSONEquality method doesn't work correctly" + ); + + Assert.IsFalse( + JsonObjectFactory.JSONEquality(jo1, jo3), + "JSONEquality method doesn't work correctly" + ); + + Assert.IsFalse( + JsonObjectFactory.JSONEquality(jo2, jo3), + "JSONEquality method doesn't work correctly" + ); + + Assert.IsTrue( + JsonObjectFactory.JSONEquality(jo1, jo1), + "JSONEquality method doesn't work correctly" + ); + + Assert.IsTrue( + JsonObjectFactory.JSONEquality(jo2, jo2), + "JSONEquality method doesn't work correctly" + ); + + Assert.IsTrue( + JsonObjectFactory.JSONEquality(jo3, jo3), + "JSONEquality method doesn't work correctly" + ); + } + } +} diff --git a/UnitTestProject1/LearnositySDKUnitTests.cs b/UnitTestProject1/LearnositySDKUnitTests.cs index 67f86d9..de800eb 100644 --- a/UnitTestProject1/LearnositySDKUnitTests.cs +++ b/UnitTestProject1/LearnositySDKUnitTests.cs @@ -7,68 +7,6 @@ namespace UnitTestProject1 [TestClass] public class LearnositySDKUnitTests { - [TestMethod] - public void inputJSONEqualsOutput() - { - string JSON = "{\"boolean\":true,\"integer\":1,\"float\":1.2,\"string\":\"string\",\"object\":{\"property\":null},\"array\":[null,[2]]}"; - JsonObject jo = JsonObjectFactory.fromString(JSON); - string outputJSON = jo.toJson(); - - Assert.IsTrue( - JsonObjectFactory.JSONEquality(JSON, outputJSON), - "Input JSON (" + JSON + ") doesn't equal the output (" + outputJSON + ")" - ); - - JSON = "[0,1,2,3,4,5,6,7,8,9,10]"; - jo = JsonObjectFactory.fromString(JSON); - outputJSON = jo.toJson(); - - Assert.IsTrue( - JsonObjectFactory.JSONEquality(JSON, outputJSON), - "Input JSON (" + JSON + ") doesn't equal the output (" + outputJSON + ")" - ); - - JSON = "[0,\"1\",2,\"3\",4,\"5\",6,\"7\",8,\"9\",10]"; - jo = JsonObjectFactory.fromString(JSON); - outputJSON = jo.toJson(); - - Assert.IsTrue( - JsonObjectFactory.JSONEquality(JSON, outputJSON), - "Input JSON (" + JSON + ") doesn't equal the output (" + outputJSON + ")" - ); - } - - [TestMethod] - public void JSONEqualityMethodWorksProperly() - { - string JSON = "{\"boolean\":true,\"integer\":1,\"float\":1.2,\"string\":\"string\",\"object\":{\"property\":null},\"array\":[null,[2]]}"; - - // building a structure for comparison purposes - JsonObject jo = new JsonObject(); - JsonObject joInner = new JsonObject(); - JsonObject ja = new JsonObject(true); - JsonObject jaInner = new JsonObject(true); - jo.set("boolean", true); - jo.set("integer", 1); - jo.set("float", 1.2f); - jo.set("string", "string"); - jo.set("object", joInner); - jo.set("array", ja); - joInner.set("property", 2); - ja.set(2); - ja.set(jaInner); - jaInner.set(3); - jaInner.set(4); - - Assert.IsTrue( - JsonObjectFactory.JSONEquality(JSON, JSON), - "JSONEquality method doesn't work correctly with strings" - ); - - Assert.IsTrue( - JsonObjectFactory.JSONEquality(jo, jo), - "JSONEquality method doesn't work correctly with strings" - ); - } + } } diff --git a/UnitTestProject1/LearnositySDKUnitTests.csproj b/UnitTestProject1/LearnositySDKUnitTests.csproj index 0c874fc..b3b0f47 100644 --- a/UnitTestProject1/LearnositySDKUnitTests.csproj +++ b/UnitTestProject1/LearnositySDKUnitTests.csproj @@ -55,7 +55,9 @@ + + diff --git a/UnitTestProject1/MergingObjectsUnitTests.cs b/UnitTestProject1/MergingObjectsUnitTests.cs new file mode 100644 index 0000000..d999c4e --- /dev/null +++ b/UnitTestProject1/MergingObjectsUnitTests.cs @@ -0,0 +1,283 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using LearnositySDK.Utils; +using Newtonsoft.Json.Linq; + +namespace UnitTestProject1 +{ + /// + /// Summary description for MergingObjectsUnitTests + /// + [TestClass] + public class MergingObjectsUnitTests + { + [TestMethod] + public void MergingJSONArraysWorksProperly() + { + JsonObject ja1 = new JsonObject(true); + JsonObject ja2 = new JsonObject(true); + JsonObject jaResult1 = new JsonObject(true); + + ja1.set("a"); + ja1.set("b"); + ja1.set("c"); + + ja2.set("d"); + ja2.set("e"); + ja2.set("f"); + + jaResult1.set("a"); + jaResult1.set("b"); + jaResult1.set("c"); + jaResult1.set("d"); + jaResult1.set("e"); + jaResult1.set("f"); + + JsonObject jaResult2 = Tools.array_merge(ja1, ja2); + + Assert.IsTrue( + JsonObjectFactory.JSONEquality(jaResult1, jaResult2), + "Merging of two arrays doesn't work properly" + ); + + jaResult1 = new JsonObject(true); + jaResult1.set("d"); + jaResult1.set("e"); + jaResult1.set("f"); + + jaResult2 = Tools.array_merge(ja1, ja2, true); + + // overwrite should only work for objects + Assert.IsFalse( + JsonObjectFactory.JSONEquality(jaResult1, jaResult2), + "Merging of two arrays with overwriting enabled does work properly" + ); + } + + [TestMethod] + public void MergingJSONObjectsWorksProperly() + { + JsonObject jo1 = new JsonObject(); + JsonObject jo2 = new JsonObject(); + JsonObject jo3 = new JsonObject(); + JsonObject joResult1 = new JsonObject(); + + jo1.set("a", "a"); + jo1.set("b", "b"); + jo1.set("c", "c"); + + jo2.set("d", "d"); + jo2.set("e", "e"); + jo2.set("f", "f"); + + jo3.set("a", "d"); + jo3.set("b", "e"); + jo3.set("c", "f"); + + joResult1.set("a", "a"); + joResult1.set("b", "b"); + joResult1.set("c", "c"); + joResult1.set("d", "d"); + joResult1.set("e", "e"); + joResult1.set("f", "f"); + + JsonObject joResult2 = Tools.array_merge(jo1, jo2); + + Assert.IsTrue( + JsonObjectFactory.JSONEquality(joResult1, joResult2), + "Merging of two objects doesn't work properly" + ); + + joResult1 = new JsonObject(); + joResult1.set("a", "a"); + joResult1.set("b", "b"); + joResult1.set("c", "c"); + + joResult2 = Tools.array_merge(jo1, jo3); + + Assert.IsTrue( + JsonObjectFactory.JSONEquality(joResult1, joResult2), + "Merging of two objects doesn't work properly" + ); + + joResult1 = new JsonObject(); + joResult1.set("a", "d"); + joResult1.set("b", "e"); + joResult1.set("c", "f"); + + joResult2 = Tools.array_merge(jo1, jo3, true); + + // overwrite works for objects + Assert.IsTrue( + JsonObjectFactory.JSONEquality(joResult1, joResult2), + "Merging of two objects using the same property names with overwriting enabled doesn't work properly" + ); + } + + [TestMethod] + public void RecursiveMergingJSONObjectsWorksProperly() + { + JsonObject jo1 = new JsonObject(); + JsonObject jo1o = new JsonObject(); + JsonObject jo1a = new JsonObject(true); + JsonObject jo1ao1 = new JsonObject(); + JsonObject jo1ao2 = new JsonObject(); + JsonObject jo2 = new JsonObject(); + JsonObject jo2o = new JsonObject(); + JsonObject jo2a = new JsonObject(true); + JsonObject jo2ao1 = new JsonObject(); + JsonObject jo2ao2 = new JsonObject(); + JsonObject joResult1 = new JsonObject(); + JsonObject joResult2; + + jo1.set("a", "a1"); + jo1.set("b", "b1"); + jo1.set("obj", jo1o); + jo1.set("arr", jo1a); + jo1o.set("d", "d1"); + jo1o.set("e", "e1"); + jo1a.set(true); + jo1a.setNull(); + jo1a.set("string"); + jo1a.set(1); + jo1a.set(1.2f); + jo1a.set(jo1ao1); + jo1a.set(jo1ao2); + jo1ao1.set("a", "a1"); + jo1ao1.set("b", "b1"); + jo1ao2.set("a", "a1"); + jo1ao2.set("b", "b1"); + + jo2.set("a", "a2"); + jo2.set("c", "c2"); + jo2.set("obj", jo2o); + jo2.set("arr", jo2a); + jo2o.set("d", "d2"); + jo2o.set("f", "f2"); + jo2a.set(true); + jo2a.setNull(); + jo2a.set("string"); + jo2a.set(1); + jo2a.set(1.2f); + jo2a.set(jo2ao1); + jo2a.set(jo2ao2); + jo2ao1.set("a", "a2"); + jo2ao1.set("b", "b2"); + jo2ao2.set("a", "a2"); + jo2ao2.set("b", "b2"); + + joResult1 = JsonObjectFactory.fromString(@"{ + ""a"": ""a1"", + ""b"": ""b1"", + ""c"": ""c2"", + ""obj"": { + ""d"": ""d1"", + ""e"": ""e1"", + ""f"": ""f2"" + }, + ""arr"": [ + true, + null, + ""string"", + 1, + 1.2, + { + ""a"": ""a1"", + ""b"": ""b1"" + }, + { + ""a"": ""a1"", + ""b"": ""b1"" + }, + true, + null, + ""string"", + 1, + 1.2, + { + ""a"": ""a2"", + ""b"": ""b2"" + }, + { + ""a"": ""a2"", + ""b"": ""b2"" + } + ] +}"); + + joResult2 = Tools.array_merge_recursive(jo1, jo2, false); + Assert.IsTrue( + JsonObjectFactory.JSONEquality(joResult1, joResult2), + "Merging of two objects recursively using the same property names doesn't work properly" + ); + + joResult1 = JsonObjectFactory.fromString(@"{ + ""a"": ""a2"", + ""b"": ""b1"", + ""c"": ""c2"", + ""obj"": { + ""d"": ""d2"", + ""e"": ""e1"", + ""f"": ""f2"" + }, + ""arr"": [ + true, + null, + ""string"", + 1, + 1.2, + { + ""a"": ""a1"", + ""b"": ""b1"" + }, + { + ""a"": ""a1"", + ""b"": ""b1"" + }, + true, + null, + ""string"", + 1, + 1.2, + { + ""a"": ""a2"", + ""b"": ""b2"" + }, + { + ""a"": ""a2"", + ""b"": ""b2"" + } + ] +}"); + + joResult2 = Tools.array_merge_recursive(jo1, jo2, true); + Assert.IsTrue( + JsonObjectFactory.JSONEquality(joResult1, joResult2), + "Merging of two objects recursively using the same property names with overwriting enabled doesn't work properly" + ); + } + + [TestMethod] + public void RecursiveMergingJSONArraysWorksProperly() + { + JsonObject jo1 = new JsonObject(); + JsonObject jo2 = new JsonObject(); + JsonObject joResult1 = new JsonObject(); + JsonObject joResult2; + + + + joResult2 = Tools.array_merge_recursive(jo1, jo2, false); + Assert.IsTrue( + JsonObjectFactory.JSONEquality(joResult1, joResult2), + "Merging of two objects recursively using the same property names doesn't work properly" + ); + + joResult2 = Tools.array_merge_recursive(jo1, jo2, true); + Assert.IsTrue( + JsonObjectFactory.JSONEquality(joResult1, joResult2), + "Merging of two objects recursively using the same property names with overwriting enabled doesn't work properly" + ); + } + } +} From 4db1d269ac433c4def63794843134c9b0123bed2 Mon Sep 17 00:00:00 2001 From: Karol Tarasiuk Date: Thu, 7 Apr 2016 17:08:42 +1000 Subject: [PATCH 2/4] [BUG] Added proper handling of date strings - i.e. disabled special treatment of it, they behave like normal strings now. --- LearnositySDK/Utils/JsonObject.cs | 18 ++++++++--------- LearnositySDK/Utils/JsonObjectFactory.cs | 25 ++++++++++++------------ 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/LearnositySDK/Utils/JsonObject.cs b/LearnositySDK/Utils/JsonObject.cs index 6bd50da..bdd2fd0 100755 --- a/LearnositySDK/Utils/JsonObject.cs +++ b/LearnositySDK/Utils/JsonObject.cs @@ -636,6 +636,10 @@ public string toJson() { sb.Append(this.db[key].ToString().ToLower()); } + else if (this.dt.ContainsKey(key)) + { + sb.Append("null"); + } else if (this.dj.ContainsKey(key)) { sb.Append(this.dj[key].toJson()); @@ -644,10 +648,6 @@ public string toJson() { sb.Append(this.da[key].toJson()); } - else if (this.dt.ContainsKey(key)) - { - sb.Append("null"); - } } sb.Append("]"); @@ -706,19 +706,19 @@ public string toJson() index++; } - foreach (KeyValuePair item in this.dj) + foreach (KeyValuePair item in this.dt) { if (index > 0) { sb.Append(","); } - sb.Append(Json.encode(item.Key) + ":" + item.Value.toJson()); + sb.Append(Json.encode(item.Key) + ":null"); index++; } - foreach (KeyValuePair item in this.da) + foreach (KeyValuePair item in this.dj) { if (index > 0) { @@ -730,14 +730,14 @@ public string toJson() index++; } - foreach (KeyValuePair item in this.dt) + foreach (KeyValuePair item in this.da) { if (index > 0) { sb.Append(","); } - sb.Append(Json.encode(item.Key) + ":null"); + sb.Append(Json.encode(item.Key) + ":" + item.Value.toJson()); index++; } diff --git a/LearnositySDK/Utils/JsonObjectFactory.cs b/LearnositySDK/Utils/JsonObjectFactory.cs index 08069c7..8aca1c6 100755 --- a/LearnositySDK/Utils/JsonObjectFactory.cs +++ b/LearnositySDK/Utils/JsonObjectFactory.cs @@ -4,6 +4,7 @@ using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using System.IO; namespace LearnositySDK.Utils { @@ -23,19 +24,19 @@ public static JsonObject fromString(string JSON) return null; } - if (type == "object") + using (var sr = new StringReader(JSON)) + using (var jr = new JsonTextReader(sr) { DateParseHandling = DateParseHandling.None }) { - JObject jObject = JObject.Parse(JSON); - return JsonObjectFactory.fromJObject(jObject); - } - else if (type == "array") - { - JArray jArray = JArray.Parse(JSON); - return JsonObjectFactory.fromJArray(jArray); - } - else - { - throw new Exception("Type not recognized"); + JToken parsed = JToken.ReadFrom(jr); + switch (parsed.Type) + { + case JTokenType.Object: + return JsonObjectFactory.fromJObject((JObject)parsed); + case JTokenType.Array: + return JsonObjectFactory.fromJArray((JArray)parsed); + default: + throw new Exception("Currently we don't accept single values, only objects and arrays"); + } } } From 6fd8ae28ce5671ee5a3a8eeb295bbf65a92dd8c7 Mon Sep 17 00:00:00 2001 From: Karol Tarasiuk Date: Fri, 8 Apr 2016 12:58:55 +1000 Subject: [PATCH 3/4] [TESTS] Added tests for Tools.empty static method --- LearnositySDK/Utils/Tools.cs | 2 +- .../LearnositySDKUnitTests.csproj | 1 + UnitTestProject1/ToolIsEmptyUnitTests.cs | 56 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 UnitTestProject1/ToolIsEmptyUnitTests.cs diff --git a/LearnositySDK/Utils/Tools.cs b/LearnositySDK/Utils/Tools.cs index fb26dd1..a5a04e5 100755 --- a/LearnositySDK/Utils/Tools.cs +++ b/LearnositySDK/Utils/Tools.cs @@ -7,7 +7,7 @@ namespace LearnositySDK.Utils { - class Tools + public class Tools { /// /// Returns integer from hexadecimal representation diff --git a/UnitTestProject1/LearnositySDKUnitTests.csproj b/UnitTestProject1/LearnositySDKUnitTests.csproj index b3b0f47..67c0e00 100644 --- a/UnitTestProject1/LearnositySDKUnitTests.csproj +++ b/UnitTestProject1/LearnositySDKUnitTests.csproj @@ -59,6 +59,7 @@ + diff --git a/UnitTestProject1/ToolIsEmptyUnitTests.cs b/UnitTestProject1/ToolIsEmptyUnitTests.cs new file mode 100644 index 0000000..1ccdc6e --- /dev/null +++ b/UnitTestProject1/ToolIsEmptyUnitTests.cs @@ -0,0 +1,56 @@ +using System; +using System.Text; +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using LearnositySDK.Utils; + +namespace UnitTestProject1 +{ + /// + /// Summary description for ToolIsEmptyUnitTests + /// + [TestClass] + public class ToolIsEmptyUnitTests + { + [TestMethod] + public void WhetherItWorksOnEmptyObjects() + { + JsonObject obj = new JsonObject(); + Assert.IsTrue( + Tools.empty(obj), + "Tools.empty doesn't work on empty objects" + ); + } + + [TestMethod] + public void WhetherItWorksOnEmptyArrays() + { + JsonObject arr = new JsonObject(true); + Assert.IsTrue( + Tools.empty(arr), + "Tools.empty doesn't work on empty arrays" + ); + } + + [TestMethod] + public void WhetherItWorksOnEmptyStrings() + { + String str = ""; + Assert.IsTrue( + Tools.empty(str), + "Tools.empty doesn't work on empty strings" + ); + } + + [TestMethod] + public void WhetherItWorksOnNullObjects() + { + Object obj = null; + Assert.IsTrue( + Tools.empty(obj), + "Tools.empty doesn't work on null objects" + ); + } + } +} From 2a7eb725e83f3ff258c11df055f616f46f43f4bc Mon Sep 17 00:00:00 2001 From: Karol Tarasiuk Date: Fri, 8 Apr 2016 12:57:48 +1000 Subject: [PATCH 4/4] [FEATURE] Callback can break recursive data api requests, when returns false Conflicts: LearnositySDK/Request/DataApi.cs --- LearnositySDK/Request/DataApi.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/LearnositySDK/Request/DataApi.cs b/LearnositySDK/Request/DataApi.cs index 9850b2a..004ec88 100755 --- a/LearnositySDK/Request/DataApi.cs +++ b/LearnositySDK/Request/DataApi.cs @@ -70,7 +70,7 @@ private Remote handleRequest(string url, Init init) /// Private key /// Request packet /// Action for the request - /// Callback to process JSON data + /// Callback to process JSON data. Returning false in callback breaks from the loop of recursive requests. /// Instance of the Remote class public JsonObject requestRecursive(string url, string securityPacketJson, string secret, string requestPacketJson = null, string action = null, ProcessData callback = null) { @@ -93,7 +93,7 @@ public JsonObject requestRecursive(string url, string securityPacketJson, string /// Private key /// Request packet /// Action for the request - /// Callback to process JSON data + /// Callback to process JSON data. Returning false in callback breaks from the loop of recursive requests. /// Instance of the Remote class public JsonObject requestRecursive(string url, JsonObject securityPacket, string secret, JsonObject requestPacket = null, string action = null, ProcessData callback = null) { @@ -108,7 +108,7 @@ public JsonObject requestRecursive(string url, JsonObject securityPacket, string /// Private key /// Request packet /// Action for the request - /// Callback to process JSON data + /// Callback to process JSON data. Returning false in callback breaks from the loop of recursive requests. /// Instance of the Remote class private JsonObject handleRequestRecursive(string url, JsonObject securityPacket, string secret, JsonObject requestPacket = null, string action = null, ProcessData callback = null) { @@ -135,7 +135,11 @@ private JsonObject handleRequestRecursive(string url, JsonObject securityPacket, { if (callback != null) { - callback(data.toJson()); + // return if callback returns false + if (!callback(data.toJson())) + { + return response; + } } else {