JSONObject.cs
/// <summary>
/// Merge object right into left recursively
/// </summary>
/// <param name="left">The left (base) object</param>
/// <param name="right">The right (new) object</param>
Merge object right into left recursively
Merge object right into left recursively
static void MergeRecur(JSONObject left, JSONObject right)
        {
            if (left.type == Type.NULL)
                left.Absorb(right);
            else if (left.type == Type.OBJECT && right.type == Type.OBJECT)
            {
                for (int i = 0; i < right.list.Count; i++)
                {
                    string key = right.keys[i];
                    if (right[i].isContainer)
                    {
                        if (left.HasField(key))
                            MergeRecur(left[key], right[i]);
                        else
                            left.AddField(key, right[i]);
                    }
                    else
                    {
                        if (left.HasField(key))
                            left.SetField(key, right[i]);
                        else
                            left.AddField(key, right[i]);
                    }
                }
            }
            else if (left.type == Type.ARRAY && right.type == Type.ARRAY)
            {
                if (right.Count > left.Count)
                {
                    Debug.WriteLine
                ("Cannot merge arrays when right object has more elements");
                    return;
                }
                for (int i = 0; i < right.list.Count; i++)
                {
                    if (left[i].type == right[i].type)
                    {           
                        if (left[i].isContainer)
                            MergeRecur(left[i], right[i]);
                        else
                        {
                            left[i] = right[i];
                        }
                    }
                }
            }
        }
static void MergeRecur ( JSONObject left , JSONObject right ) { if ( left . type == Type . NULL ) left . Absorb ( right ) ; else if ( left . type == Type . OBJECT && right . type == Type . OBJECT ) { for ( int i = 0 ; i < right . list . Count ; i ++ ) { string key = right . keys [ i ] ; if ( right [ i ] . isContainer ) { if ( left . HasField ( key ) ) MergeRecur ( left [ key ] , right [ i ] ) ; else left . AddField ( key , right [ i ] ) ; } else { if ( left . HasField ( key ) ) left . SetField ( key , right [ i ] ) ; else left . AddField ( key , right [ i ] ) ; } } } else if ( left . type == Type . ARRAY && right . type == Type . ARRAY ) { if ( right . Count > left . Count ) { Debug . WriteLine ( " " ) ; return ; } for ( int i = 0 ; i < right . list . Count ; i ++ ) { if ( left [ i ] . type == right [ i ] . type ) { if ( left [ i ] . isContainer ) MergeRecur ( left [ i ] , right [ i ] ) ; else { left [ i ] = right [ i ] ; } } } } }
Merge object right into left recursively
Merge object right into left recursively
//Only overwrite with the same type
left
JSONObject
The left (base) object
The left ( base ) object
right
JSONObject
The right (new) object
The right ( new ) object