using System;
using System.Collections;
using System.Data;
using OutSystems.HubEdition.RuntimePlatform;
using OutSystems.RuntimePublic.Db;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
namespace OutSystems.NssJSON_Compare {
public class CssJSON_Compare: IssJSON_Compare {
/// <summary>
///
/// </summary>
/// <param name="ssOriginalJSON"></param>
/// <param name="ssNewJSON"></param>
/// <param name="ssKeyPrefix"></param>
/// <param name="ssKeySuffix"></param>
/// <param name="ssChangeSeparator"></param>
/// <param name="ssPropertySeparator"></param>
/// <param name="ssExcludeProperties"></param>
/// <param name="ssResult"></param>
public void MssJSONDifference(string ssOriginalJSON, string ssNewJSON, string ssKeyPrefix, string ssKeySuffix,
string ssChangeSeparator, string ssPropertySeparator, string ssExcludeProperties, out string ssResult) {
ssResult = CompareObjects(JObject.Parse(ssOriginalJSON), JObject.Parse(ssNewJSON), ssKeyPrefix, ssKeySuffix,
ssChangeSeparator, ssPropertySeparator, ssExcludeProperties).ToString();
return;
} // MssJSONDifference
private static StringBuilder CompareObjects(JObject source, JObject target,
string keyPrefix = "", string keySuffix = ": ", string changeSeparator = " > ",
string propertySeparator = ", ", string excludeProperties = "ChangedBy;DateChanged;Changed")
{
StringBuilder returnString = new StringBuilder();
string pSeparator = "";
excludeProperties = ";" + excludeProperties + ";";
foreach (KeyValuePair<string, JToken> sourcePair in source)
if (excludeProperties.IndexOf(";" + sourcePair.Key + ";") < 0)
if (sourcePair.Value.Type == JTokenType.Object)
if (target.GetValue(sourcePair.Key) == null)
returnString.Append(pSeparator + sourcePair.Key + " not found"); // + Environment.NewLine);
pSeparator = propertySeparator;
}
else if (target.GetValue(sourcePair.Key).Type != JTokenType.Object)
returnString.Append(pSeparator + sourcePair.Key + " is not an object");
else
StringBuilder r = CompareObjects(sourcePair.Value.ToObject<JObject>(), target.GetValue(sourcePair.Key).ToObject<JObject>(),
keyPrefix, keySuffix, changeSeparator, propertySeparator, excludeProperties);
if (r.ToString() != "")
returnString.Append(pSeparator + r);
else if (sourcePair.Value.Type == JTokenType.Array)
returnString.Append(pSeparator + sourcePair.Key + " not found");
StringBuilder r = CompareArrays(sourcePair.Value.ToObject<JArray>(),
target.GetValue(sourcePair.Key).ToObject<JArray>(), sourcePair.Key,
JToken expected = sourcePair.Value;
var actual = target.SelectToken(sourcePair.Key);
if (actual == null)
if (!JToken.DeepEquals(expected, actual))
returnString.Append(pSeparator + sourcePair.Key + keySuffix
+ sourcePair.Value + changeSeparator
+ target.Property(sourcePair.Key).Value
);
return returnString;
private static StringBuilder CompareArrays(JArray source, JArray target, string arrayName = "", string keyPrefix = "",
string keySuffix = ": ", string changeSeparator = " > ", string propertySeparator = ", ", string excludeProperties = "ChangedBy;DateChanged;Changed")
var returnString = new StringBuilder();
for (var index = 0; index < source.Count; index++)
var expected = source[index];
if (expected.Type == JTokenType.Object)
var actual = (index >= target.Count) ? new JObject() : target[index];
StringBuilder r = CompareObjects(expected.ToObject<JObject>(), actual.ToObject<JObject>(), keyPrefix, keySuffix, changeSeparator,
propertySeparator, excludeProperties);
var actual = (index >= target.Count) ? "" : target[index];
if (String.IsNullOrEmpty(arrayName))
returnString.Append(pSeparator + "Index " + index + keySuffix + expected
+ changeSeparator + actual);
returnString.Append(pSeparator + arrayName
+ "[" + index + "]" + keySuffix + expected
} // OutSystems.NssJSON_Compare