Example #1
0
        private static void MergeJsonObjects(Newtonsoft.Json.Linq.JObject self, Newtonsoft.Json.Linq.JObject other)
        {
            foreach (var p in other.Properties())
            {
                var sp = self.Property(p.Name);
                if (sp == null)
                {
                    self.Add(p);
                }
                else
                {
                    switch (p.Type)
                    {
                    // Primitives override
                    case Newtonsoft.Json.Linq.JTokenType.Boolean:
                    case Newtonsoft.Json.Linq.JTokenType.Bytes:
                    case Newtonsoft.Json.Linq.JTokenType.Comment:
                    case Newtonsoft.Json.Linq.JTokenType.Constructor:
                    case Newtonsoft.Json.Linq.JTokenType.Date:
                    case Newtonsoft.Json.Linq.JTokenType.Float:
                    case Newtonsoft.Json.Linq.JTokenType.Guid:
                    case Newtonsoft.Json.Linq.JTokenType.Integer:
                    case Newtonsoft.Json.Linq.JTokenType.String:
                    case Newtonsoft.Json.Linq.JTokenType.TimeSpan:
                    case Newtonsoft.Json.Linq.JTokenType.Uri:
                    case Newtonsoft.Json.Linq.JTokenType.None:
                    case Newtonsoft.Json.Linq.JTokenType.Null:
                    case Newtonsoft.Json.Linq.JTokenType.Undefined:
                        self.Replace(p);
                        break;

                    // Arrays merge
                    case Newtonsoft.Json.Linq.JTokenType.Array:
                        if (sp.Type == Newtonsoft.Json.Linq.JTokenType.Array)
                        {
                            sp.Value = new Newtonsoft.Json.Linq.JArray(((Newtonsoft.Json.Linq.JArray)sp.Value).Union((Newtonsoft.Json.Linq.JArray)p.Value));
                        }
                        else
                        {
                            var a = new Newtonsoft.Json.Linq.JArray(sp.Value);
                            sp.Value = new Newtonsoft.Json.Linq.JArray(a.Union((Newtonsoft.Json.Linq.JArray)p.Value));
                        }

                        break;

                    // Objects merge
                    case Newtonsoft.Json.Linq.JTokenType.Object:
                        if (sp.Type == Newtonsoft.Json.Linq.JTokenType.Object)
                        {
                            MergeJsonObjects((Newtonsoft.Json.Linq.JObject)sp.Value, (Newtonsoft.Json.Linq.JObject)p.Value);
                        }
                        else
                        {
                            sp.Value = p.Value;
                        }
                        break;

                    // Ignore other stuff
                    default:
                        break;
                    }
                }
            }
        }
Example #2
0
        private static void MergeJsonObjects(Newtonsoft.Json.Linq.JObject self, Newtonsoft.Json.Linq.JObject other)
        {
            foreach(var p in other.Properties())
            {
                var sp = self.Property(p.Name);
                if (sp == null)
                    self.Add(p);
                else
                {
                    switch (p.Type)
                    {
                        // Primitives override
                        case Newtonsoft.Json.Linq.JTokenType.Boolean:
                        case Newtonsoft.Json.Linq.JTokenType.Bytes:
                        case Newtonsoft.Json.Linq.JTokenType.Comment:
                        case Newtonsoft.Json.Linq.JTokenType.Constructor:
                        case Newtonsoft.Json.Linq.JTokenType.Date:
                        case Newtonsoft.Json.Linq.JTokenType.Float:
                        case Newtonsoft.Json.Linq.JTokenType.Guid:
                        case Newtonsoft.Json.Linq.JTokenType.Integer:
                        case Newtonsoft.Json.Linq.JTokenType.String:
                        case Newtonsoft.Json.Linq.JTokenType.TimeSpan:
                        case Newtonsoft.Json.Linq.JTokenType.Uri:
                        case Newtonsoft.Json.Linq.JTokenType.None:
                        case Newtonsoft.Json.Linq.JTokenType.Null:
                        case Newtonsoft.Json.Linq.JTokenType.Undefined:
                            self.Replace(p);
                            break;

                            // Arrays merge
                        case Newtonsoft.Json.Linq.JTokenType.Array:
                            if (sp.Type == Newtonsoft.Json.Linq.JTokenType.Array)
                                sp.Value = new Newtonsoft.Json.Linq.JArray(((Newtonsoft.Json.Linq.JArray)sp.Value).Union((Newtonsoft.Json.Linq.JArray)p.Value));
                            else
                            {
                                var a = new Newtonsoft.Json.Linq.JArray(sp.Value);
                                sp.Value = new Newtonsoft.Json.Linq.JArray(a.Union((Newtonsoft.Json.Linq.JArray)p.Value));
                            }

                            break;

                            // Objects merge
                        case Newtonsoft.Json.Linq.JTokenType.Object:
                            if (sp.Type == Newtonsoft.Json.Linq.JTokenType.Object)
                                MergeJsonObjects((Newtonsoft.Json.Linq.JObject)sp.Value, (Newtonsoft.Json.Linq.JObject)p.Value);
                            else
                                sp.Value = p.Value;
                            break;

                            // Ignore other stuff
                        default:
                            break;
                    }
                }
            }
        }