// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
namespace LightJson
{
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
///
/// Represents a key-value pair collection of JsonValue objects.
///
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(JsonObjectDebugView))]
internal sealed class JsonObject : IEnumerable>, IEnumerable
{
private IDictionary properties;
///
/// Initializes a new instance of the class.
///
public JsonObject()
{
this.properties = new Dictionary();
}
///
/// Gets the number of properties in this JsonObject.
///
/// The number of properties in this JsonObject.
public int Count {
get {
return this.properties.Count;
}
}
///
/// Gets or sets the property with the given key.
///
/// The key of the property to get or set.
///
/// The getter will return JsonValue.Null if the given key is not associated with any value.
///
public JsonValue this[string key] {
get {
JsonValue value;
if (this.properties.TryGetValue(key, out value)) {
return value;
} else {
return JsonValue.Null;
}
}
set {
this.properties[key] = value;
}
}
///
/// Adds a key with a null value to this collection.
///
/// The key of the property to be added.
/// Returns this JsonObject.
/// The that was added.
public JsonObject Add(string key)
{
return this.Add(key, JsonValue.Null);
}
///
/// Adds a value associated with a key to this collection.
///
/// The key of the property to be added.
/// The value of the property to be added.
/// Returns this JsonObject.
public JsonObject Add(string key, JsonValue value)
{
this.properties.Add(key, value);
return this;
}
///
/// Removes a property with the given key.
///
/// The key of the property to be removed.
///
/// Returns true if the given key is found and removed; otherwise, false.
///
public bool Remove(string key)
{
return this.properties.Remove(key);
}
///
/// Clears the contents of this collection.
///
/// Returns this JsonObject.
public JsonObject Clear()
{
this.properties.Clear();
return this;
}
///
/// Changes the key of one of the items in the collection.
///
///
/// This method has no effects if the oldKey does not exists.
/// If the newKey already exists, the value will be overwritten.
///
/// The name of the key to be changed.
/// The new name of the key.
/// Returns this JsonObject.
public JsonObject Rename(string oldKey, string newKey)
{
if (oldKey == newKey) {
// Renaming to the same name just does nothing
return this;
}
JsonValue value;
if (this.properties.TryGetValue(oldKey, out value)) {
this[newKey] = value;
this.Remove(oldKey);
}
return this;
}
///
/// Determines whether this collection contains an item assosiated with the given key.
///
/// The key to locate in this collection.
/// Returns true if the key is found; otherwise, false.
public bool ContainsKey(string key)
{
return this.properties.ContainsKey(key);
}
///
/// Determines whether this collection contains the given JsonValue.
///
/// The value to locate in this collection.
/// Returns true if the value is found; otherwise, false.
public bool Contains(JsonValue value)
{
return this.properties.Values.Contains(value);
}
///
/// Returns an enumerator that iterates through this collection.
///
/// The enumerator that iterates through this collection.
public IEnumerator> GetEnumerator()
{
return this.properties.GetEnumerator();
}
///
/// Returns an enumerator that iterates through this collection.
///
/// The enumerator that iterates through this collection.
IEnumerator IEnumerable.GetEnumerator()
{
return this.properties.Values.GetEnumerator();
}
///
/// Returns an enumerator that iterates through this collection.
///
/// The enumerator that iterates through this collection.
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
[ExcludeFromCodeCoverage]
private class JsonObjectDebugView
{
private JsonObject jsonObject;
public JsonObjectDebugView(JsonObject jsonObject)
{
this.jsonObject = jsonObject;
}
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public KeyValuePair[] Keys {
get {
var keys = new KeyValuePair[this.jsonObject.Count];
var i = 0;
foreach (var property in this.jsonObject) {
keys[i] = new KeyValuePair(property.Key, property.Value);
i += 1;
}
return keys;
}
}
[DebuggerDisplay("{value.ToString(),nq}", Name = "{key}", Type = "JsonValue({Type})")]
public class KeyValuePair
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string key;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private JsonValue value;
public KeyValuePair(string key, JsonValue value)
{
this.key = key;
this.value = value;
}
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public object View {
get {
if (this.value.IsJsonObject) {
return (JsonObject)this.value;
} else if (this.value.IsJsonArray) {
return (JsonArray)this.value;
} else {
return this.value;
}
}
}
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private JsonValueType Type {
get {
return this.value.Type;
}
}
}
}
}
}