JsonValue viewer
One more nice tool for the JsonValue API: a WPF control which can be used to view JsonValue graphs. It’s a simple control which inherits from TreeView and shows the graph in a tree-like fashion. This following JSON document, for example, is shown in the picture below.
[1,2,true,"hello world",null,{"one":1,"two":2,"bool":true,"str":"Hello world","array":[null,{},true,false]}]
JsonValueViewer
The viewer uses the deep-event propagation I posted before to be notified of changes in the graph – when any place in the graph is changed, the viewer redraws the tree (ideally we’d only change the parts of the tree which have been changed, but I wanted to keep the code simple). Also, I wrote it in WPF, but I think it can be easily ported into Silverlight, as I didn't use any desktop-only feature. I've also played in changing the "icons" (A), (O) and (P) representing the JSON type (Array, Object, Primitive) to images, but that made the code longer (needed to either create the images on the fly or add images to the project resource).
- namespace JsonValueControls
- {
- /// <summary>
- /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
- ///
- /// Step 1a) Using this custom control in a XAML file that exists in the current project.
- /// Add this XmlNamespace attribute to the root element of the markup file where it is
- /// to be used:
- ///
- /// xmlns:MyNamespace="clr-namespace:JsonValueControls"
- ///
- ///
- /// Step 1b) Using this custom control in a XAML file that exists in a different project.
- /// Add this XmlNamespace attribute to the root element of the markup file where it is
- /// to be used:
- ///
- /// xmlns:MyNamespace="clr-namespace:JsonValueControls;assembly=JsonValueControls"
- ///
- /// You will also need to add a project reference from the project where the XAML file lives
- /// to this project and Rebuild to avoid compilation errors:
- ///
- /// Right click on the target project in the Solution Explorer and
- /// "Add Reference"->"Projects"->[Select this project]
- ///
- ///
- /// Step 2)
- /// Go ahead and use your control in the XAML file.
- ///
- /// <MyNamespace:JsonValueViewer/>
- ///
- /// </summary>
- public class JsonValueViewer : TreeView
- {
- /// <summary>
- /// Defines a property for binding a <see cref="JsonValue"/> value to this control.
- /// </summary>
- public static DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(JsonValue), typeof(JsonValueViewer));
- private JsonValue source;
- /// <summary>
- /// Gets or sets the source for this viewer.
- /// </summary>
- public JsonValue Source
- {
- get
- {
- return (JsonValue)this.GetValue(SourceProperty);
- }
- set
- {
- this.source = value;
- this.UpdateTree();
- this.SetValue(SourceProperty, value);
- }
- }
- private static bool IsComplex(JsonValue jsonValue)
- {
- return (jsonValue != null && jsonValue.JsonType != JsonType.Boolean && jsonValue.JsonType != JsonType.Number && jsonValue.JsonType != JsonType.String);
- }
- private void UpdateTree()
- {
- this.Items.Clear();
- JsonValueWrapper jsonWrapper = JsonValueWrapper.WrapJsonValue(null, null, this.source);
- jsonWrapper.DeepChanged += new EventHandler<DeepJsonValueEventArgs>(jsonWrapper_DeepChanged);
- TreeViewItem root = CreateNode(null, this.source, true);
- this.Items.Add(root);
- AddChildrenNodes(root, this.source);
- }
- private void jsonWrapper_DeepChanged(object sender, DeepJsonValueEventArgs e)
- {
- (sender as JsonValueWrapper).Dispose();
- this.UpdateTree();
- }
- private void AddChildrenNodes(TreeViewItem root, JsonValue jsonValue)
- {
- if (jsonValue != null && IsComplex(jsonValue))
- {
- bool isArray = jsonValue.JsonType == JsonType.Array;
- foreach (var child in jsonValue)
- {
- string format = isArray ? "[{0}]" : "\"{0}\"";
- string keyName = string.Format(CultureInfo.InvariantCulture, format, child.Key);
- TreeViewItem node = CreateNode(keyName, child.Value, true);
- root.Items.Add(node);
- AddChildrenNodes(node, child.Value);
- }
- }
- }
- private TreeViewItem CreateNode(string key, JsonValue value, bool isExpanded)
- {
- TreeViewItem result = new TreeViewItem();
- result.IsExpanded = isExpanded;
- StackPanel panel = new StackPanel { Orientation = Orientation.Horizontal };
- string name = null;
- bool addValue = value == null || !IsComplex(value);
- string type = null;
- if (value == null)
- {
- type = "(P)";
- name = " <<null>>";
- }
- else
- {
- switch (value.JsonType)
- {
- case JsonType.Object:
- type = "(O)";
- break;
- case JsonType.Array:
- type = "(A)";
- break;
- default:
- type = "(P)";
- name = " " + value.ToString();
- break;
- }
- }
- panel.Children.Add(new TextBlock { Text = type, Foreground = Brushes.Red });
- if (key != null)
- {
- panel.Children.Add(new TextBlock { Text = " " + key });
- }
- if (addValue)
- {
- panel.Children.Add(new TextBlock { Text = " :" });
- if (name != null)
- {
- panel.Children.Add(new TextBlock { Text = name });
- }
- }
- result.Header = panel;
- return result;
- }
- }
- }
The source code for this project, for the wrapper and for a sample WPF project which uses the control can be found here.