Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions LearnositySDK/Request/DataApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private Remote handleRequest(string url, Init init)
/// <param name="secret">Private key</param>
/// <param name="requestPacketJson">Request packet</param>
/// <param name="action">Action for the request</param>
/// <param name="callback">Callback to process JSON data</param>
/// <param name="callback">Callback to process JSON data. Returning false in callback breaks from the loop of recursive requests.</param>
/// <returns>Instance of the Remote class</returns>
public JsonObject requestRecursive(string url, string securityPacketJson, string secret, string requestPacketJson = null, string action = null, ProcessData callback = null)
{
Expand All @@ -93,7 +93,7 @@ public JsonObject requestRecursive(string url, string securityPacketJson, string
/// <param name="secret">Private key</param>
/// <param name="requestPacket">Request packet</param>
/// <param name="action">Action for the request</param>
/// <param name="callback">Callback to process JSON data</param>
/// <param name="callback">Callback to process JSON data. Returning false in callback breaks from the loop of recursive requests.</param>
/// <returns>Instance of the Remote class</returns>
public JsonObject requestRecursive(string url, JsonObject securityPacket, string secret, JsonObject requestPacket = null, string action = null, ProcessData callback = null)
{
Expand All @@ -108,7 +108,7 @@ public JsonObject requestRecursive(string url, JsonObject securityPacket, string
/// <param name="secret">Private key</param>
/// <param name="requestPacket">Request packet</param>
/// <param name="action">Action for the request</param>
/// <param name="callback">Callback to process JSON data</param>
/// <param name="callback">Callback to process JSON data. Returning false in callback breaks from the loop of recursive requests.</param>
/// <returns>Instance of the Remote class</returns>
private JsonObject handleRequestRecursive(string url, JsonObject securityPacket, string secret, JsonObject requestPacket = null, string action = null, ProcessData callback = null)
{
Expand All @@ -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
{
Expand Down
63 changes: 53 additions & 10 deletions LearnositySDK/Utils/JsonObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ public void set(bool value)
this.arrayIndex++;
}

/// <summary>
/// Sets/adds the value
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void set(JToken value)
{
this.set(this.arrayIndex, value);
this.arrayIndex++;
}

/// <summary>
/// Sets/adds NULL
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void setNull()
{
this.set(JToken.Parse("null"));
}

/// <summary>
/// Sets/adds value with given type
/// </summary>
Expand Down Expand Up @@ -161,6 +182,16 @@ public void set(int key, JToken value)
this.set(key.ToString(), value);
}

/// <summary>
/// Sets/adds NULL
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void setNull(int key)
{
this.set(key, JToken.Parse("null"));
}

/// <summary>
/// Sets/adds the value
/// </summary>
Expand Down Expand Up @@ -198,7 +229,7 @@ public void set(string key, string value)
/// <param name="value"></param>
public void set(string key, JsonObject value)
{
if (this.isArray())
if (value.isArray())
{
this.set("JsonArray", key, value);
}
Expand All @@ -218,6 +249,16 @@ public void set(string key, JToken value)
this.set("NULL", key, value);
}

/// <summary>
/// Sets/adds NULL
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void setNull(string key)
{
this.set(key, JToken.Parse("null"));
}

/// <summary>
/// Sets/adds the value
/// </summary>
Expand Down Expand Up @@ -518,6 +559,8 @@ public string[] getKeys()
l.Add(item.Key);
}

l.Sort();

return l.ToArray();
}

Expand Down Expand Up @@ -593,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());
Expand All @@ -601,10 +648,6 @@ public string toJson()
{
sb.Append(this.da[key].toJson());
}
else if (this.dt.ContainsKey(key))
{
sb.Append("null");
}
}

sb.Append("]");
Expand Down Expand Up @@ -663,19 +706,19 @@ public string toJson()
index++;
}

foreach (KeyValuePair<string, JsonObject> item in this.dj)
foreach (KeyValuePair<string, JToken> 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<string, JsonObject> item in this.da)
foreach (KeyValuePair<string, JsonObject> item in this.dj)
{
if (index > 0)
{
Expand All @@ -687,14 +730,14 @@ public string toJson()
index++;
}

foreach (KeyValuePair<string, JToken> item in this.dt)
foreach (KeyValuePair<string, JsonObject> 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++;
}
Expand Down
114 changes: 54 additions & 60 deletions LearnositySDK/Utils/JsonObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;

namespace LearnositySDK.Utils
{
Expand All @@ -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");
}
}
}

Expand Down Expand Up @@ -147,6 +148,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);
Expand All @@ -164,68 +166,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);
}

if (overwrite && Tools.in_array(key, keys2))
// 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);
}
// 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;
}
Expand Down Expand Up @@ -256,5 +239,16 @@ public static bool JSONEquality(JsonObject JSON1, JsonObject JSON2)
{
return JSONEquality(JSON1.toJson(), JSON2.toJson());
}

/// <summary>
/// Checks whether two JsonObjects are equal
/// </summary>
/// <param name="JSON1">first JsonObject to compare</param>
/// <param name="JSON2">second JsonObject to compare</param>
/// <returns></returns>
public static bool JSONEquality(JsonObject JSON1, string JSON2)
{
return JSONEquality(JSON1.toJson(), JSON2);
}
}
}
2 changes: 1 addition & 1 deletion LearnositySDK/Utils/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace LearnositySDK.Utils
{
class Tools
public class Tools
{
/// <summary>
/// Returns integer from hexadecimal representation
Expand Down
Loading