Пример #1
1
        public string JsonPatchesWorks(string leftString, string rightString)
        {
            var left = JToken.Parse(leftString);
            var right = JToken.Parse(rightString);

            var patchDoc = new JsonDiffer().Diff(left, right);
            var patcher = new JsonPatcher();
            patcher.Patch(ref left, patchDoc);

            Assert.True(JToken.DeepEquals(left, right));
            //var patchedLeft = left.ToString(Formatting.None);
            //var expected = right.ToString(Formatting.None);
            //Assert.AreEqual(expected, patchedLeft);

            var patchJson = patchDoc.ToString(Formatting.None);
            return patchJson;
        }
Пример #2
0
        public void RemovePropertyTest()
        {
            #region jsonDefinitions
            string jsonValidationSchema = @"
               {
                ""$schema"": ""http://json-schema.org/draft-04/schema#"",
                ""title"": ""Person"",
                ""type"": ""object"",
					""properties"": {
						""firstName"": {
							""type"": ""string""
						},
						""lastName"": {
							""type"": ""string""
						},
						""age"": {
							""description"": ""Age in years"",
							""type"": ""integer""
						}					                 
                    },               
                ""additionalProperties"": false               
             }";

            string updatedJsonValidationSchema = @"
               {
                ""$schema"": ""http://json-schema.org/draft-04/schema#"",
                ""title"": ""Person"",
                ""type"": ""object"",
					""properties"": {
						""firstName"": {
							""type"": ""string"",
                            ""additionalProperties"": false               
                            
						},
						""lastName"": {
                            ""additionalProperties"": false               
                
						},
						""age"": {
							""description"": ""Age in years"",
							""type"": ""string"",
                            ""additionalProperties"": false
						},
                        ""email"": {
                            ""description"": ""Private email address"",
                            ""type"": ""string"",
                            ""additionalProperties"": false
                        }					                 
                    },               
                ""additionalProperties"": false               
             }";
            #endregion

            var existing = JToken.Parse(jsonValidationSchema);
            var newOne   = JToken.Parse(updatedJsonValidationSchema);

            var result           = new JsonDiffer().Diff(existing, newOne, false);
            var removeOperations = result.Operations.FindAll(x => x.GetType() == typeof(RemoveOperation));
            Assert.True(removeOperations.Count == 1);
        }
Пример #3
0
        private static string GenPatch(string leftFile, string rightFile, string relPath)
        {
            var left     = JToken.Parse(File.ReadAllText(leftFile));
            var right    = JToken.Parse(File.ReadAllText(rightFile));
            var patchDoc = new JsonDiffer().Diff(left, right, false);

            // Cheap way of doing it but meh
            var patchToken = JToken.Parse(patchDoc.ToString());

            foreach (var val in  (patchToken as JArray))
            {
                val["file"] = relPath.Replace("\\", "/");
            }

            return(patchToken.ToString());
        }
Пример #4
0
        public string JsonPatchesWorks(string leftString, string rightString)
        {
            var left  = JToken.Parse(leftString);
            var right = JToken.Parse(rightString);

            var patchDoc = new JsonDiffer().Diff(left, right, false);
            var patcher  = new JsonPatcher();

            patcher.Patch(ref left, patchDoc);


            Assert.True(JToken.DeepEquals(left, right));
            //Assert.AreEqual(expected, patchedLeft);

            var patchJson = patchDoc.ToString(Formatting.None);

            return(patchJson);
        }
Пример #5
0
 public void ComplexExampleWithDeepArrayChange()
 {
     
     var leftPath = @".\samples\scene{0}a.json";
     var rightPath = @".\samples\scene{0}b.json";
     var i = 1;
     while(File.Exists(string.Format(leftPath, i)))
     {
         var scene1Text = File.ReadAllText(string.Format(leftPath, i));
         var scene1 = JToken.Parse(scene1Text);
         var scene2Text = File.ReadAllText(string.Format(rightPath, i));
         var scene2 = JToken.Parse(scene2Text);
         var patchDoc = new JsonDiffer().Diff(scene1, scene2, true);
         //Assert.AreEqual("[{\"op\":\"remove\",\"path\":\"/items/0/entities/1\"}]",
         var patcher = new JsonPatcher();
         patcher.Patch(ref scene1, patchDoc);
         Assert.True(JToken.DeepEquals(scene1, scene2));
         i++;
     }
 }
Пример #6
0
        public void ComplexExampleWithDeepArrayChange()
        {
            var leftPath  = @".\samples\scene{0}a.json";
            var rightPath = @".\samples\scene{0}b.json";
            var i         = 1;

            while (File.Exists(string.Format(leftPath, i)))
            {
                var scene1Text = File.ReadAllText(string.Format(leftPath, i));
                var scene1     = JToken.Parse(scene1Text);
                var scene2Text = File.ReadAllText(string.Format(rightPath, i));
                var scene2     = JToken.Parse(scene2Text);
                var patchDoc   = new JsonDiffer().Diff(scene1, scene2, true);
                //Assert.AreEqual("[{\"op\":\"remove\",\"path\":\"/items/0/entities/1\"}]",
                var patcher = new JsonPatcher();
                patcher.Patch(ref scene1, patchDoc);
                Assert.True(JToken.DeepEquals(scene1, scene2));
                i++;
            }
        }
Пример #7
0
        private IEnumerable <FieldChange> Find(BLModel.Alternate.Long.Airing currentAsset, BLModel.Alternate.Long.Airing originalAiring)
        {
            var changeBuilder = new ChangeBuilder();

            var currentJson  = JsonConvert.SerializeObject(currentAsset);
            var originalJson = JsonConvert.SerializeObject(originalAiring);

            var currentAssetAsJson  = JObject.Parse(currentJson);
            var originalAssetAsJson = JObject.Parse(originalJson);

            var differ = new JsonDiffer();

            var results = differ.FindDifferences(currentAssetAsJson, originalAssetAsJson);

            foreach (var change in results)
            {
                changeBuilder.SetCommonValues(change, currentAsset, originalAiring);
            }

            return(results);
        }
Пример #8
0
        public static IEnumerable <IReadOnlyCollection <DiffRecord> > Diff(IList <JToken> from, IList <JToken> to)
        {
            var length = from.Count > to.Count ? from.Count : to.Count;

            for (var i = 0; i < length; i++)
            {
                var fromToken     = i < from.Count ? from[i] : _emptyToken;
                var toToken       = i < to.Count ? to[i] : _emptyToken;
                var patch         = new JsonDiffer();
                var forwardPatch  = patch.Diff(fromToken, toToken, true);
                var backwardPatch = patch.Diff(toToken, fromToken, true);

                if (TryParseJson(forwardPatch.ToString(), out var forwardTokens) && TryParseJson(backwardPatch.ToString(), out var backwardTokens))
                {
                    var operations = new DiffRecord[forwardTokens.Count];
                    for (var j = 0; j < operations.Length; j++)
                    {
                        var forwardToken     = forwardTokens[j];
                        var forwardOperation = JsonConvert.DeserializeObject <PatchOperation>(forwardToken.ToString());

                        var backwardToken     = backwardTokens[j];
                        var backwardOperation = JsonConvert.DeserializeObject <PatchOperation>(backwardToken.ToString());
                        operations[j] = new DiffRecord
                        {
                            Op        = forwardOperation.Op,
                            Path      = forwardOperation.Path,
                            SideValue = forwardOperation.Value,
                            BaseValue = backwardOperation.Value
                        };
                    }
                    yield return(operations);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
        }
Пример #9
0
        public void GeneratePatches()
        {
            var e1 = new Event
            {
                Id       = 312332,
                Name     = "Real Madrid - FC Barcelona",
                OpenDate = DateTime.UtcNow,
                Sport    = new Sport {
                    Id = 4, Name = "Soccer"
                },
                Scoreboard = new Scoreboard
                {
                    Messages = new List <Message>
                    {
                        new Message
                        {
                            Id   = 1,
                            Text = "Goal for Real Madrid"
                        }
                    },
                    Stats = new GameStatistics
                    {
                        DoubleCounters = new List <LiveDoubleCounter>
                        {
                            new LiveDoubleCounter
                            {
                                Id    = 1,
                                Name  = "Double Counter 1",
                                Team1 = new Team
                                {
                                    Counters = new List <Counter>
                                    {
                                        new Counter
                                        {
                                            Id       = 1,
                                            PeriodId = 254,
                                            Value    = 3
                                        },
                                        new Counter
                                        {
                                            Id       = 2,
                                            PeriodId = 255,
                                            Value    = 5
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            var e2 = e1.DeepClone();

            //e2.OpenDate = DateTime.UtcNow.AddMinutes(10);

            e2.Scoreboard.Messages[0].Text = "New update message";
            e2.Scoreboard.Messages.Add(new Message
            {
                Id   = 124,
                Text = "Penalty for Barcelona"
            });

            e2.Scoreboard.Stats.DoubleCounters[0].Team1.Counters[0].Value = 11;
            e2.Scoreboard.Stats.DoubleCounters[0].Name = "Updated name";

            var jo1 = JObject.FromObject(e1).Root;
            var jo2 = JObject.FromObject(e2).Root;

            var patchDoc = new JsonDiffer().Diff(jo1, jo2, true);

            System.Diagnostics.Trace.WriteLine(patchDoc);
        }
Пример #10
0
 public JsonDifferProvider()
 {
     _differ = new JsonDiffer();
 }