public void ExampleUsage2()
        {
            MyClass1 originalObj = new MyClass1()
            {
                myString = "abc", myString2 = "def"
            };

            MyClass1 copy1 = originalObj.DeepCopyViaJson();

            copy1.myString     = "abcd";
            copy1.complexField = new MyClass1()
            {
                myString = "123", myString2 = "456"
            };
            copy1.complexField.complexList = new List <MyClass1>()
            {
                new MyClass1()
                {
                    myString = "listEntry1"
                }
            };

            MyClass1 copy2 = originalObj.DeepCopyViaJson();

            copy2.myString2    = "defg";
            copy2.myString     = "123";
            copy2.complexField = new MyClass1()
            {
                myString = "zyx"
            };
            copy2.complexField.complexList = new List <MyClass1>()
            {
                new MyClass1()
                {
                    myString = "listEntry2"
                }
            };

            var merge = MergeJson.Merge(originalObj, copy1, copy2);

            Assert.True(merge.hasMergeConflict);

            // Parsed conflicts returns an easy to iterate through array:
            var parsedConflicts = merge.GetParsedMergeConflicts();
            var firstConflict   = parsedConflicts.First();

            Assert.Equal("myString", firstConflict.fieldName);
            Assert.Equal("abcd", "" + firstConflict.oldValue);
            Assert.Equal("123", "" + firstConflict.newValue);

            //Log.d("merge2.conflicts=" + JsonWriter.AsPrettyString(merge2.conflicts));
            //Log.d("parsedConflicts=" + JsonWriter.AsPrettyString(parsedConflicts));
        }
示例#2
0
        public void ExampleUsage1()
        {
            MyClass1 original = new MyClass1()
            {
                name = "1", child = new MyClass1()
                {
                    name = "2"
                }
            };

            MyClass1 copy = original.DeepCopyViaJson();

            Assert.Null(MergeJson.GetDiff(original, copy)); // No diff between original and copy
            AssertV2.AreEqualJson(original, copy);          // WIll use MergeJson.GetDiff internally
            Assert.Equal(original.child.name, copy.child.name);
            // Modify the copy, changing the copy will not change the original:
            copy.child.name = "Some new name..";
            // Check that the change was only done in the copy and not the original:
            Assert.NotEqual(original.child.name, copy.child.name);
            Assert.NotNull(MergeJson.GetDiff(original, copy));

            // Objects that impl. IClonable can also ShallowCopy (will call .Clone internally):
            MyClass1 shallowCopy = original.ShallowCopyViaClone();

            Assert.NotSame(original, shallowCopy);
            Assert.Same(original.child, shallowCopy.child);
        }
        public void ExampleUsage1()
        {
            MyClass1 originalObj = new MyClass1()
            {
                myString = "abc", myString2 = "def"
            };

            MyClass1 copy1 = originalObj.DeepCopyViaJson();

            copy1.myString     = "abcd";
            copy1.complexField = new MyClass1()
            {
                myString = "123", myString2 = "456"
            };
            copy1.complexField.complexList = new List <MyClass1>()
            {
                new MyClass1()
                {
                    myString = "listEntry1"
                }
            };

            MyClass1 copy2 = originalObj.DeepCopyViaJson();

            copy2.myString2 = "defg";

            var merge = MergeJson.Merge(originalObj, copy1, copy2);

            Assert.False(merge.hasMergeConflict);

            // Parse the merged result back into a MyClass1 object:
            MyClass1 mergeResult1 = merge.GetResult();

            // The changes from both copies were merged correctly:
            Assert.Equal(copy1.myString, mergeResult1.myString);
            Assert.Equal(copy2.myString2, mergeResult1.myString2);
        }
示例#4
0
        public void ExampleUsage1()
        {
            MyClass1 original = new MyClass1()
            {
                name = "1", child = new MyClass1()
                {
                    name = "2", age = 3
                }
            };

            MyClass1 copy = original.DeepCopyViaJson();

            Assert.Null(MergeJson.GetDiff(original, copy)); // No diff between original and copy
            AssertV2.AreEqualJson(original, copy);          // AreEqualJson will use MergeJson.GetDiff internally
            Assert.Equal(original.child.name, copy.child.name);
            // Modify the copy, changing the copy will not change the original:
            copy.child.name = "Some new name..";
            // Check that the change was only done in the copy and not the original:
            Assert.NotEqual(original.child.name, copy.child.name);
            JToken diffToOriginal = MergeJson.GetDiff(original, copy);

            Assert.NotNull(diffToOriginal);

            // Objects that impl. IClonable can also ShallowCopy (will call .Clone internally):
            MyClass1 shallowCopy = original.ShallowCopyViaClone();

            Assert.NotSame(original, shallowCopy);
            Assert.Same(original.child, shallowCopy.child);

            // Applying a change to an existing target object is done using MergeJson.Patch:
            var oldName = original.child.name;

            MergeJson.Patch(original, diffToOriginal);     // Apply the changes stored in the diff
            Assert.NotEqual(oldName, original.child.name); // The name field was updated
            Assert.Equal(copy.child.name, original.child.name);
            Assert.Equal(3, original.child.age);           // The age field was not changed by the patch
        }