Saturday, March 27, 2021

JSON object comparision

 I wrote this small program for my own needs to compare two JSON objects and spit out either true or false to tell is there any difference between the objects. This was useful  in angular forms when form controls wasn't  used. The app have to notify the user that if the user navigate away from the current page making some changes in the form but without saving the form. Form controls are grouped under form group object and form group value changes can be observed to notify the changes if subscribed. In my scenario I didn't use form control but used NGMODEL which I thought was pretty straight forward. NGMODEL and Form Control  combination are deprecated so wrote this recursive brute force approach to detect changes if any. This will work for small JSON Objects but not for large JSON Objects. Language is typescript.


class ObjectComparision<T>{

  oldObject: T;
  newObject: T;

  getOldObject(): T {
    return this.oldObject;
  }

  setOldObject(tempObject: T) {
    //Object.assign(this.oldObject,tempObject) ;
    this.oldObject = Object.assign({}, tempObject);
    //this.dates(this.oldObject);

  }
  
  getNewObject(): T {
    return this.newObject
  }
  setNewObject(tempObject: T) {
    this.newObject = Object.assign({}, tempObject);

  }
  compareObject(obj: T, obj1: T): boolean {

    for (var key in obj) {
      if (this.checkPropertyInstanceIsAnObject(obj[key]) ) {

        if (this.checkObjectDifference(obj[key],obj1[key]))
         return true;
      }
      else if (this.checkPropertyInstanceIsAnArray(obj[key]) ) {
        if(this.checkArrayDifference(obj[key],obj1[key]))
          return true;
       
      }// end for if
      else {
        if (this.compare(obj1[key],
          obj[key]) == true) {

          return true;
        }
      }
    }
    return false;
  }

  
  checkObjectDifference(compare,compared) : boolean
  {
    if (this.checkIsPropertyundefined(compare) || this.checkIsPropertyundefined(compared)) {
          return true;
        }

        if (this.compareObject(compare, compared) == true) {
          return true;
        }
    return false;
  }

  checkArrayDifference(obj,obj1) : boolean
  {
    for (var element in obj) {

          if (this.checkPropertyInstanceIsAnObject(obj[element])) {
              
            if (this.checkObjectDifference(obj[element],obj1[element]))
               return true;
             
          }
          else {

            if (this.checkIsPropertyundefined(obj1[element]) 
                || this.checkIsPropertyundefined(obj[element])) {
              return true;
            }
            if (this.compare(this.obj1[element],
              obj[element]) == true) {

              return true
            }
          }
        }
    return false;
  }

  checkPropertyInstanceIsAnObject(objectT): boolean {
    return object instanceof Object

    }

    checkPropertyInstanceIsAnArray(objectT): boolean {
    return object instanceof Array

    }

    checkIsPropertyundefined(object : T) : boolean
    {
      return object === undefined
    }
  
  compare(val1: any, val2: any): boolean {
    if (val1.toString() != val2.toString())
      console.log(val1, val2)
    return (val1.toString() != val2.toString())
  }
}

example

 var obj1 = { 'id'1'name' : 'krishna'}
var obj2 = { 'id'1'name' : 'krishna_'}

var o = new ObjectComparision<any>();
 console.log(o.compareObject(objaa, objbb))
 console.log(o.compareObject(objbb,objaa))


No comments:

Post a Comment