/// <summary> /// Merges this instance with another <see cref="BDictionary"/>. /// </summary> /// <remarks> /// By default existing keys are either overwritten (<see cref="BString"/> and <see cref="BNumber"/>) or merged if possible (<see cref="BList"/> and <see cref="BDictionary"/>). /// This behavior can be changed with the <paramref name="existingKeyAction"/> parameter. /// </remarks> /// <param name="dictionary">The dictionary to merge into this instance.</param> /// <param name="existingKeyAction">Decides how to handle the values of existing keys.</param> public void MergeWith(BDictionary dictionary, ExistingKeyAction existingKeyAction = ExistingKeyAction.Merge) { foreach (var field in dictionary) { // Add non-existing key if (!ContainsKey(field.Key)) { Add(field); continue; } if (existingKeyAction == ExistingKeyAction.Skip) { continue; } switch (field.Value) { // Replace strings and numbers case BString _: case BNumber _: this[field.Key] = field.Value; continue; // Append list to existing list or replace other types case BList newList: { var existingList = Get <BList>(field.Key); if (existingList == null || existingKeyAction == ExistingKeyAction.Replace) { this[field.Key] = field.Value; continue; } existingList.AddRange(newList); continue; } // Merge dictionary with existing or replace other types case BDictionary newDictionary: { var existingDictionary = Get <BDictionary>(field.Key); if (existingDictionary == null || existingKeyAction == ExistingKeyAction.Replace) { this[field.Key] = field.Value; continue; } existingDictionary.MergeWith(newDictionary); break; } } } }
/// <summary> /// Merges this instance with another <see cref="BDictionary"/>. /// </summary> /// <remarks> /// By default existing keys are either overwritten (<see cref="BString"/> and <see cref="BNumber"/>) or merged if possible (<see cref="BList"/> and <see cref="BDictionary"/>). /// This behavior can be changed with the <paramref name="existingKeyAction"/> parameter. /// </remarks> /// <param name="dictionary">The dictionary to merge into this instance.</param> /// <param name="existingKeyAction">Decides how to handle the values of existing keys.</param> public void MergeWith(BDictionary dictionary, ExistingKeyAction existingKeyAction = ExistingKeyAction.Merge) { foreach (var field in dictionary) { // Add non-existing key if (!ContainsKey(field.Key)) { Add(field); continue; } if (existingKeyAction == ExistingKeyAction.Skip) { continue; } // Replace strings and numbers if (field.Value is BString || field.Value is BNumber) { this[field.Key] = field.Value; continue; } // Append list to existing list or replace other types var newList = field.Value as BList; if (newList != null) { var existingList = Get <BList>(field.Key); if (existingList == null || existingKeyAction == ExistingKeyAction.Replace) { this[field.Key] = field.Value; continue; } existingList.AddRange(newList); continue; } // Merge dictionary with existing or replace other types var newDictionary = field.Value as BDictionary; if (newDictionary != null) { var existingDictionary = Get <BDictionary>(field.Key); if (existingDictionary == null || existingKeyAction == ExistingKeyAction.Replace) { this[field.Key] = field.Value; continue; } existingDictionary.MergeWith(newDictionary); } } }