自制Json 类库之 JsonObject

自制Json 类库之 JsonObject

 JsonObject 用于模拟Json 对象可以存储字符串、Json 键值对

 

JsonObject 代码:

public class JsonObject : Json, IDictionary<string, Json>
    {
        private Dictionary<string, Json> items;

        public JsonObject()
        {
            this.items = new Dictionary<string,Json>();
        }

        public JsonObject(IDictionary<string, Json> dictionary)
        {
            this.items = new Dictionary<string,Json>(dictionary);
        }

        public JsonObject(IEqualityComparer<string> comparer)
        {
            this.items = new Dictionary<string,Json>(comparer);
        }

        public JsonObject(int capacity)
        {
            this.items = new Dictionary<string,Json>(capacity);
        }

        public JsonObject(IDictionary<string, Json> dictionary, IEqualityComparer<string> comparer)
        {
            this.items = new Dictionary<string,Json>(dictionary, comparer);
        }

        public JsonObject(int capacity, IEqualityComparer<string> comparer)
        {
            this.items = new Dictionary<string, Json>(capacity, comparer);
        }

        public void Add(string key, Json value)
        {
            this.items.Add(key, value);
        }

        public bool ContainsKey(string key)
        {
            return this.items.ContainsKey(key);
        }

        public ICollection<string> Keys
        {
            get { return this.items.Keys; }
        }

        public bool Remove(string key)
        {
            return this.Remove(key);
        }

        public bool TryGetValue(string key, out Json value)
        {
            return this.items.TryGetValue(key, out value);
        }

        public ICollection<Json> Values
        {
            get { return this.items.Values; }
        }

        public Json this[string key]
        {
            get { return this.items[key]; }
            set { this.items[key] = value; }
        }

        void ICollection<KeyValuePair<string, Json>>.Add(KeyValuePair<string, Json> item)
        {
            ((IDictionary<string, Json>)this.items).Add(item);
        }

        public void Clear()
        {
            this.items.Clear();
        }

        bool ICollection<KeyValuePair<string, Json>>.Contains(KeyValuePair<string, Json> item)
        {
            return ((IDictionary<string, Json>)this.items).Contains(item);
        }

        void ICollection<KeyValuePair<string, Json>>.CopyTo(KeyValuePair<string, Json>[] array, int arrayIndex)
        {
            ((IDictionary<string, Json>)this.items).CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return this.items.Count; }
        }

        bool ICollection<KeyValuePair<string, Json>>.IsReadOnly
        {
            get { return ((ICollection<KeyValuePair<string, Json>>)this.items).IsReadOnly; }
        }

        bool ICollection<KeyValuePair<string, Json>>.Remove(KeyValuePair<string, Json> item)
        {
            return ((IDictionary<string, Json>)this.items).Remove(item);
        }

        public IEnumerator<KeyValuePair<string, Json>> GetEnumerator()
        {
            return this.items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.items.GetEnumerator();
        }

        public override string ToString()
        {
            var builder = new StringBuilder();
            builder.Append("{");
            string tempKey = null;
            Json tempValue = null;
            foreach (var key in this.items.Keys)
            {
                tempKey = JsonHelper.TransferredMeaning(key);
                tempValue = this.items[key];
                builder.Append("\"" + tempKey + "\":" + tempValue.ToString() + ",");
            }
            if (builder.ToString().EndsWith(","))
            {
                builder.Remove(builder.Length - 1, 1);
            }
            builder.Append("}");
            return builder.ToString();
        }
    }

 

posted @ 2014-12-09 14:48  忤逆小旭  阅读(225)  评论(0)    收藏  举报