Deep event propagation in JsonValue
One of the features added in the latest release of the WCF for jQuery support in Codeplex was the ability for a user to register to listen to modifications in a JsonValue instance. However, if you have a JSON DOM and you want to be notified for changes in any member of the graph, you’d need to register for changes in all elements in the graph. This sample provides a wrapper which can be used for such. It’s a simple wrapper which essentially adds a new field to the JsonValue object, its parent in the graph. So when any change is made deep in the graph, the wrapper can propagate the change to its parent until it reaches the root.
JsonValueWrapper class
- /// <summary>
- /// Wrapper for a <see cref="JsonValue"/> instance, which can raise events for changes in the whole JSON
- /// graph (not only in the immediate children).
- /// </summary>
- public class JsonValueWrapper : IDisposable
- {
- private JsonValueWrapper parent;
- private object parentKey;
- private JsonValue wrappedValue;
- private List<JsonValueWrapper> children;
- private bool disposed = false;
- /// <summary>
- /// Creates a new instance of the <see cref="JsonValueWrapper"/> class.
- /// </summary>
- /// <param name="parent">The parent of the <paramref name="value"/> instance in the JSON graph, or <code>null</code> if
- /// the <paramref name="value"/> is the root of the graph.</param>
- /// <param name="parentKey">The key for the <paramref name="value"/> for which it can be accessed by the parent, or <code>null</code>
- /// if the <paramref name="value"/> is the root of the graph.</param>
- /// <param name="value">The <see cref="JsonValue"/> instance being wrapped.</param>
- public JsonValueWrapper(JsonValueWrapper parent, object parentKey, JsonValue value)
- {
- if (value == null)
- {
- throw new ArgumentNullException("value", "The wrapped JsonValue cannot be null.");
- }
- if (parentKey == null)
- {
- if (parent != null)
- {
- throw new ArgumentException("If parent is not null, parentKey must also be not null.");
- }
- }
- else
- {
- if (parent == null)
- {
- throw new ArgumentException("If parent is null, parentKey must also be null.");
- }
- switch (Type.GetTypeCode(parentKey.GetType()))
- {
- case TypeCode.Int32:
- if (parent.Value.JsonType != JsonType.Array)
- {
- throw new ArgumentException("Int32 parentKey values can only be used with a JsonArray parent.");
- }
- if (!object.ReferenceEquals(value, parent.Value[(int)parentKey]))
- {
- throw new ArgumentException("The wrapped value does not correspond to the item indexed by the parentKey in the parent.");
- }
- break;
- case TypeCode.String:
- if (parent.Value.JsonType != JsonType.Object)
- {
- throw new ArgumentException("String parentKey values can only be used with a JsonObject parent.");
- }
- if (!object.ReferenceEquals(value, parent.Value[(string)parentKey]))
- {
- throw new ArgumentException("The wrapped value does not correspond to the item indexed by the parentKey in the parent.");
- }
- break;
- default:
- throw new ArgumentException("The parentKey value must be either an Int32 or a String.");
- }
- }
- this.parent = parent;
- this.parentKey = parentKey;
- this.wrappedValue = value;
- this.children = new List<JsonValueWrapper>();
- this.Initialize();
- this.wrappedValue.Changed += new EventHandler<JsonValueChangeEventArgs>(WrappedValueChanged);
- }
- /// <summary>
- /// Raised when the wrapped <see cref="JsonValue"/> instance, or any of its children (recursively) has changed.
- /// </summary>
- public event EventHandler<DeepJsonValueEventArgs> DeepChanged;
- /// <summary>
- /// Gets the wrapper for the parent of the wrapped <see cref="JsonValue"/> instance in the JSON graph. If the wrapped
- /// instance is the root of the graph, it will return <code>null</code>.
- /// </summary>
- public JsonValueWrapper Parent
- {
- get { return this.parent; }
- }
- /// <summary>
- /// Gets the key for the wrapped <see cref="JsonValue"/> instance where it can be accessed by its parent in the JSON graph. If the wrapped
- /// instance is the root of the graph, it will return <code>null</code>.
- /// </summary>
- public object ParentKey
- {
- get { return this.parentKey; }
- }
- /// <summary>
- /// Gets the <see cref="JsonValue"/> instance wrapped by this object.
- /// </summary>
- public JsonValue Value
- {
- get { return this.wrappedValue; }
- }
- /// <summary>
- /// Wraps a <see cref="JsonValue"/> instance in a <see cref="JsonValueWrapper"/> instance.
- /// </summary>
- /// <param name="parent">The parent of the <paramref name="value"/> instance in the JSON graph, or <code>null</code> if
- /// the <paramref name="value"/> is the root of the graph.</param>
- /// <param name="parentKey">The key for the <paramref name="value"/> for which it can be accessed by the parent, or <code>null</code>
- /// if the <paramref name="value"/> is the root of the graph.</param>
- /// <param name="value">The instance to be wrapped.</param>
- /// <returns>An instance of the <see cref="JsonValueWrapper"/> class which wraps the given <see cref="JsonValue"/> instance.</returns>
- public static JsonValueWrapper WrapJsonValue(JsonValueWrapper parent, object parentKey, JsonValue value)
- {
- if (value == null)
- {
- return null;
- }
- return new JsonValueWrapper(parent, parentKey, value);
- }
- /// <summary>
- /// Disposes this instance, removing the event handlers for the wrapped value (and all of its descendants).
- /// </summary>
- public void Dispose()
- {
- this.Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (!this.disposed)
- {
- if (disposing)
- {
- this.wrappedValue.Changed -= new EventHandler<JsonValueChangeEventArgs>(this.WrappedValueChanged);
- foreach (var child in this.children)
- {
- child.Dispose();
- }
- }
- this.disposed = true;
- }
- }
- ~JsonValueWrapper()
- {
- this.Dispose(false);
- }
- private void Initialize()
- {
- if (this.wrappedValue != null)
- {
- if (this.wrappedValue.JsonType == JsonType.Array)
- {
- for (int i = 0; i < this.wrappedValue.Count; i++)
- {
- JsonValueWrapper wrappedChild = JsonValueWrapper.WrapJsonValue(this, i, this.wrappedValue[i]);
- if (wrappedChild != null)
- {
- this.children.Add(wrappedChild);
- }
- }
- }
- else if (this.wrappedValue.JsonType == JsonType.Object)
- {
- foreach (var child in this.wrappedValue)
- {
- JsonValueWrapper wrappedChild = JsonValueWrapper.WrapJsonValue(this, child.Key, child.Value);
- if (wrappedChild != null)
- {
- this.children.Add(wrappedChild);
- }
- }
- }
- }
- }
- private void WrappedValueChanged(object sender, JsonValueChangeEventArgs e)
- {
- this.FireDeepChanged(e, sender as JsonValue, null);
- }
- private void FireDeepChanged(JsonValueChangeEventArgs originalEvent, JsonValue originalSender, params object[] keys)
- {
- if (this.DeepChanged != null)
- {
- this.DeepChanged(this, new DeepJsonValueEventArgs(originalEvent, originalSender, keys));
- }
- if (this.Parent != null)
- {
- List<object> eventKeys = new List<object>();
- if (this.parentKey != null)
- {
- eventKeys.Add(this.parentKey);
- }
- if (keys != null)
- {
- eventKeys.AddRange(keys);
- }
- this.Parent.FireDeepChanged(originalEvent, originalSender, eventKeys.ToArray());
- }
- }
- }
DeepJsonValueEventArgs class
- /// <summary>
- /// Provide data for the <see cref="JsonValueWrapper.DeepChanged"/> event.
- /// </summary>
- public class DeepJsonValueEventArgs : EventArgs
- {
- private JsonValueChangeEventArgs change;
- private JsonValue originalSender;
- private List<object> keys;
- /// <summary>
- /// Creates a new instance of the <see cref="DeepJsonValueEventArgs"/> class.
- /// </summary>
- /// <param name="originalChange">The data for the original change event which triggered the <see cref="JsonValueWrapper.DeepChanged"/> event.</param>
- /// <param name="originalSender">The sender for the original change event which triggered the <see cref="JsonValueWrapper.DeepChanged"/> event.</param>
- /// <param name="keys">The sequence of the keys to the change where the original event occurred.</param>
- public DeepJsonValueEventArgs(JsonValueChangeEventArgs originalChange, JsonValue originalSender, IEnumerable<object> keys)
- {
- this.change = originalChange;
- this.originalSender = originalSender;
- if (keys == null)
- {
- this.keys = new List<object>();
- }
- else
- {
- this.keys = new List<object>(keys);
- }
- }
- /// <summary>
- /// Gets the data for the original change event.
- /// </summary>
- public JsonValueChangeEventArgs OriginalChange
- {
- get
- {
- return this.change;
- }
- }
- /// <summary>
- /// Gets the sender for the original change event.
- /// </summary>
- public JsonValue OriginalSender
- {
- get
- {
- return this.originalSender;
- }
- }
- /// <summary>
- /// Gets the sequence of the keys from the event source to the item where the event occurred.
- /// </summary>
- public IEnumerable<object> Keys
- {
- get
- {
- return this.keys.AsReadOnly();
- }
- }
- }
Coming up
A WPF control which can be used to visualize JsonValue graphs.