jsoncompareversion
Reactive icon

JSONCompareVersion

Stable version 1.0.0 (Compatible with OutSystems 11)
Uploaded
 on 18 October 2023
 by 
0.0
 (0 ratings)
jsoncompareversion

JSONCompareVersion

Documentation
1.0.0

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");

                            pSeparator = propertySeparator;

                        }

                        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);

                                pSeparator = propertySeparator;

                            }

                        }

                    }

                    else if (sourcePair.Value.Type == JTokenType.Array)

                    {

                        if (target.GetValue(sourcePair.Key) == null)

                        {

                            returnString.Append(pSeparator + sourcePair.Key + " not found");

                            pSeparator = propertySeparator;

                        }

                        else

                        {

                            StringBuilder r = CompareArrays(sourcePair.Value.ToObject<JArray>(),

                                target.GetValue(sourcePair.Key).ToObject<JArray>(), sourcePair.Key,

                                keyPrefix, keySuffix, changeSeparator, propertySeparator, excludeProperties);

                            if (r.ToString() != "")

                            {

                                returnString.Append(pSeparator + r);

                                pSeparator = propertySeparator;

                            }

                        }

                    }

                    else

                    {

                        JToken expected = sourcePair.Value;

                        var actual = target.SelectToken(sourcePair.Key);

                        if (actual == null)

                        {

                            returnString.Append(pSeparator + sourcePair.Key + " not found");

                            pSeparator = propertySeparator;

                        }

                        else

                        {

                            if (!JToken.DeepEquals(expected, actual))

                            {

                                returnString.Append(pSeparator + sourcePair.Key + keySuffix

                                                    + sourcePair.Value + changeSeparator

                                                    + target.Property(sourcePair.Key).Value

                                                    );

                                pSeparator = propertySeparator;

                            }

                        }

                    }

                }

            }

            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();

            string pSeparator = "";


            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);

                    if (r.ToString() != "")

                    {

                        returnString.Append(pSeparator + r);

                        pSeparator = propertySeparator;

                    }

                }

                else

                {


                    var actual = (index >= target.Count) ? "" : target[index];

                    if (!JToken.DeepEquals(expected, actual))

                    {

                        if (String.IsNullOrEmpty(arrayName))

                        {

                            returnString.Append(pSeparator + "Index " + index + keySuffix + expected

                                                + changeSeparator + actual);

                            pSeparator = propertySeparator;

                        }

                        else

                        {

                            returnString.Append(pSeparator + arrayName

                                                + "[" + index + "]" + keySuffix + expected

                                                + changeSeparator + actual);

                            pSeparator = propertySeparator;

                        }

                    }

                }

            }

            return returnString;

        }


    }


} // OutSystems.NssJSON_Compare