JsonObject.cs
上传用户:xhd1221
上传日期:2022-05-28
资源大小:186k
文件大小:17k
源码类别:

外挂编程

开发平台:

C/C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Json
  5. {
  6.     public class JsonObject : JsonObjectModel// : IComparable, IConvertible, IComparable<bool>, IEquatable<bool>
  7.     {
  8.         private string rawjson;
  9.         public bool isValue
  10.         {
  11.             get
  12.             {
  13.                 if (rawjson.Trim() == "") return false;
  14.                 if (isModel || isCollection)
  15.                     return false;
  16.                 return true;
  17.             }
  18.         }
  19.         public bool isModel
  20.         {
  21.             get
  22.             {
  23.                 if (rawjson.Trim() == "") return false;
  24.                 if (rawjson.StartsWith("{"))
  25.                     return true;
  26.                 return false;
  27.             }
  28.         }
  29.         public bool isCollection
  30.         {
  31.             get
  32.             {
  33.                 if (rawjson.Trim() == "") return false;
  34.                 if (rawjson.StartsWith("["))
  35.                     return true;
  36.                 return false;
  37.             }
  38.         }
  39.         /// <summary>
  40.         /// 获取Json.JsonObject中实际包含的元素数
  41.         /// </summary>
  42.         public int Count
  43.         {
  44.             get
  45.             {
  46.                 if (rawjson.Trim() == "") return 0;
  47.                 if (isModel == false)
  48.                 {
  49.                     return 1;
  50.                 }
  51.                 return GetCollection().Count;
  52.             }
  53.         }
  54.         /// <summary> 
  55.         /// 当模型是值对象,返回key 
  56.         /// </summary> 
  57.         public string Key
  58.         {
  59.             get
  60.             {
  61.                 if (isValue)
  62.                     return base._GetKey(rawjson);
  63.                 return null;
  64.             }
  65.         }
  66.         /// <summary> 
  67.         /// 当模型是值对象,返回value 
  68.         /// </summary> 
  69.         public string Value
  70.         {
  71.             get
  72.             {
  73.                 if (!isValue)
  74.                     return null;
  75.                 return base._GetValue(rawjson);
  76.             }
  77.             set
  78.             {
  79.                 rawjson = rawjson.Substring(0, rawjson.IndexOf(':')+1) + value;
  80.             }
  81.         }
  82.         /// <summary>
  83.         /// 获取或设置其键值
  84.         /// </summary>
  85.         /// <param name="key">要获取或设置期键值的键</param>
  86.         /// <returns></returns>
  87.         public virtual object this[object key]
  88.         {
  89.             get
  90.             {
  91.                 if (rawjson.Trim() == "") return null;
  92.                 if (key.GetType() == typeof(int))
  93.                 {
  94.                     if ((int)key < 0)
  95.                         return null;
  96.                     if (isModel == false)
  97.                     {
  98.                         if ((int)key == 0)
  99.                             return Value;
  100.                         else
  101.                             return null;
  102.                     }
  103.                     return GetValue(key.ToString());
  104.                 }
  105.                 else if (key.GetType() == typeof(string))
  106.                 {
  107.                     if (isModel == false)
  108.                         return Value;
  109.                     if (!ContainsKey(key.ToString()))
  110.                         return null;
  111.                     return GetValue(key.ToString());
  112.                 }
  113.                 return null;
  114.             }
  115.             set
  116.             {
  117.                 Insert(key, value, false);
  118.             }
  119.         }
  120.         #region 构造方法
  121.         public JsonObject()
  122.             : this("")
  123.         {
  124.         }
  125.         public JsonObject(object rawjson)
  126.         {
  127.             this.rawjson = rawjson.ToString();
  128.         } 
  129.         #endregion
  130.         #region 拥有的key
  131.         /// <summary> 
  132.         /// 当模型是对象,返回拥有的key 
  133.         /// </summary> 
  134.         /// <returns></returns> 
  135.         public List<string> GetKeys()
  136.         {
  137.             if (isModel==false)
  138.                 return null;
  139.             List<string> list = new List<string>();
  140.             foreach (string subjson in base._GetCollection(this.rawjson))
  141.             {
  142.                 string key = new JsonObject(subjson).Key;
  143.                 if (!string.IsNullOrEmpty(key))
  144.                     list.Add(key);
  145.             }
  146.             return list;
  147.         } 
  148.         #endregion
  149.         #region 确定Json.JsonObject 是否包含特定键
  150.         /// <summary>
  151.         /// 确定Json.JsonObject 是否包含特定键
  152.         /// </summary>
  153.         /// <param name="key">要在Json.JsonObject定位的键</param>
  154.         /// <returns></returns>
  155.         public bool ContainsKey(string key)
  156.         {
  157.             if (isModel==false)
  158.             {
  159.                 if (key == Key)
  160.                 {
  161.                     return true;
  162.                 }
  163.                 return false;
  164.             }
  165.             List<string> keys = GetKeys();
  166.             foreach (string _key in keys)
  167.             {
  168.                 if (key == _key)
  169.                 {
  170.                     return true;
  171.                 }
  172.             }
  173.             return false;
  174.         } 
  175.         #endregion
  176.         #region 获取值
  177.         #region 当模型是对象,key对应是值,则返回key对应的值
  178.         /// <summary> 
  179.         /// 当模型是对象,key对应是值,则返回key对应的值 
  180.         /// </summary> 
  181.         /// <param name="key"></param> 
  182.         /// <returns></returns> 
  183.         public string GetValue(string key)
  184.         {
  185.             if (isModel==false)
  186.                 return null;
  187.             if (string.IsNullOrEmpty(key))
  188.                 return null;
  189.             foreach (string subjson in base._GetCollection(this.rawjson))
  190.             {
  191.                 JsonObject model = new JsonObject(subjson);
  192.                 if (!model.isValue)
  193.                     continue;
  194.                 if (model.Key == key)
  195.                     return model.Value;
  196.             }
  197.             return null;
  198.         }
  199.         #endregion
  200.         #region 模型不是对象,返回 索引 对应的值
  201.         /// <summary>
  202.         /// 模型不是对象,返回 索引 对应的值
  203.         /// </summary>
  204.         /// <param name="index"></param>
  205.         /// <returns></returns>
  206.         public string GetValue(int index)
  207.         {
  208.             if (isModel==false)
  209.                 return Value;
  210.             string subjson = base._GetCollection(this.rawjson)[index];
  211.             JsonObject model = new JsonObject(subjson);
  212.             if (!model.isValue)
  213.                 return null;
  214.             return model.Value;
  215.         }
  216.         #endregion 
  217.         #region 模型不是对象,返回 索引 对应的KEY
  218.         /// <summary>
  219.         /// 模型不是对象,返回 索引 对应的KEY
  220.         /// </summary>
  221.         /// <param name="index"></param>
  222.         /// <returns></returns>
  223.         public string GetKey(int index)
  224.         {
  225.             if (isModel==false)
  226.                 return Key;
  227.             string subjson = base._GetCollection(this.rawjson)[index];
  228.             JsonObject model = new JsonObject(subjson);
  229.             if (!model.isValue)
  230.                 return null;
  231.             return model.Key;
  232.         }
  233.         #endregion  
  234.         #endregion
  235.         #region 模型是对象,key对应是对象,返回key对应的对象
  236.         /// <summary> 
  237.         /// 模型是对象,key对应是对象,返回key对应的对象 
  238.         /// </summary> 
  239.         /// <param name="key"></param> 
  240.         /// <returns></returns> 
  241.         public JsonObject GetJson(string key)
  242.         {
  243.             if (isModel==false)
  244.                 return null;
  245.             if (string.IsNullOrEmpty(key))
  246.                 return null;
  247.             foreach (string subjson in base._GetCollection(this.rawjson))
  248.             {
  249.                 JsonObject model = new JsonObject(subjson);
  250.                 if (!model.isValue)
  251.                     continue;
  252.                 if (model.Key == key)
  253.                 {
  254.                     JsonObject submodel = new JsonObject(model.Value);
  255.                     if (!submodel.isModel)
  256.                         return null;
  257.                     else
  258.                         return submodel;
  259.                 }
  260.             }
  261.             return null;
  262.         } 
  263.         #endregion
  264.         #region 模型是对象,索引 对应是对象,返回 索引 对应的对象
  265.         /// <summary>
  266.         /// 模型是对象,索引 对应是对象,返回 索引 对应的对象
  267.         /// </summary>
  268.         /// <param name="index"></param>
  269.         /// <returns></returns>
  270.         public JsonObject GetJson(int index)
  271.         {
  272.             if (isModel==false)
  273.                 return null;
  274.             string subjson = base._GetCollection(this.rawjson)[index];
  275.             if (subjson.Length < 2)
  276.                 return null;
  277.             JsonObject model = new JsonObject(subjson);
  278.             return model;
  279.             if (!model.isModel)
  280.                 return null;
  281.             JsonObject submodel = new JsonObject(model.Value);
  282.             if (!submodel.isModel)
  283.                 return null;
  284.             else
  285.                 return submodel;
  286.         }
  287.         #endregion
  288.         #region 模型是对象,key对应是集合,返回集合 
  289.         /// <summary> 
  290.         /// 模型是对象,key对应是集合,返回集合 
  291.         /// </summary> 
  292.         /// <param name="key"></param> 
  293.         /// <returns></returns> 
  294.         public JsonObject GetCollection(string key)
  295.         {
  296.             if (isModel==false)
  297.                 return null;
  298.             if (string.IsNullOrEmpty(key))
  299.                 return null;
  300.             foreach (string subjson in base._GetCollection(this.rawjson))
  301.             {
  302.                 JsonObject model = new JsonObject(subjson);
  303.                 if (!model.isValue)
  304.                     continue;
  305.                 if (model.Key == key)
  306.                 {
  307.                     JsonObject submodel = new JsonObject(model.Value);
  308.                     if (!submodel.isCollection)
  309.                         return null;
  310.                     else
  311.                         return submodel;
  312.                 }
  313.             }
  314.             return null;
  315.         } 
  316.         #endregion
  317.         public bool IsCollection(string key)
  318.         {
  319.             if (isModel==false)
  320.                 return false;
  321.             if (string.IsNullOrEmpty(key))
  322.                 return false;
  323.             foreach (string subjson in base._GetCollection(this.rawjson))
  324.             {
  325.                 JsonObject model = new JsonObject(subjson);
  326.                 if (!model.isValue)
  327.                     continue;
  328.                 if (model.Key == key)
  329.                 {
  330.                     JsonObject submodel = new JsonObject(model.Value);
  331.                     return submodel.isCollection;
  332.                 }
  333.             }
  334.             return false;
  335.         }
  336.         #region 模型是集合,返回自身
  337.         /// <summary> 
  338.         /// 模型是集合,返回自身 
  339.         /// </summary> 
  340.         /// <returns></returns> 
  341.         public List<JsonObject> GetCollection()
  342.         {
  343.             List<JsonObject> list = new List<JsonObject>();
  344.             if (isValue)
  345.                 return list;
  346.             foreach (string subjson in base._GetCollection(rawjson))
  347.             {
  348.                 list.Add(new JsonObject(subjson));
  349.             }
  350.             return list;
  351.         } 
  352.         #endregion
  353.         #region 将带有指定键值和值的元素添加到 Json.JsonObject中
  354.         /// <summary>
  355.         /// 将带有指定键值和值的元素添加到 Json.JsonObject中
  356.         /// </summary>
  357.         /// <param name="key"></param>
  358.         /// <param name="value"></param>
  359.         public void Add(string key, string value)
  360.         {
  361.             Insert(key, value, true);
  362.         }
  363.         /// <summary>
  364.         /// 将 Json.JsonObject 元素添加到 Json.JsonObject中
  365.         /// </summary>
  366.         /// <param name="json"></param>
  367.         public void Add(JsonObject json)
  368.         {
  369.             Insert(null, json.ToString(), true);
  370.         } 
  371.         #endregion
  372.         /// <summary>
  373.         /// 从 Json.JsonObject 中移除带有指定键的元素
  374.         /// </summary>
  375.         /// <param name="key"></param>
  376.         public void Remove(string key)
  377.         {
  378.             if (!ContainsKey(key))
  379.                 return;
  380.             string oldValue = GetValue(key.ToString());
  381.             if (oldValue == null) oldValue = "";
  382.             int statsIndex = rawjson.IndexOf(""" + key.ToString() + """);//key的位置
  383.             string jsonq = rawjson.Substring(0, statsIndex);
  384.             statsIndex = statsIndex + key.ToString().Length + oldValue.Length + 1 + 2;//key的长度  加  :的长度 引号长度
  385.             string jsonh = rawjson.Substring(statsIndex, rawjson.Length - statsIndex);
  386.             bool requ = true;
  387.             if (jsonh.StartsWith(",") && requ)
  388.             {
  389.                 jsonh = jsonh.Substring(1);
  390.                 requ = false;
  391.             }
  392.             if (jsonq.EndsWith(",") && requ)
  393.                 jsonq = jsonq.Substring(0, jsonq.Length - 1);
  394.             rawjson = jsonq + jsonh;
  395.         }
  396.         /// <summary>
  397.         /// 从 Json.JsonObject 中移除带有指定键的元素
  398.         /// </summary>
  399.         /// <param name="JsonObject"></param>
  400.         public void Remove(JsonObject json)
  401.         {
  402.             if (isModel==false)
  403.             {
  404.                 return;
  405.             }
  406.             int statsIndex = rawjson.IndexOf(json.ToString());//key的位置
  407.             if (statsIndex < 0) return;
  408.             string jsonq = rawjson.Substring(0, statsIndex);
  409.             statsIndex = statsIndex + json.ToString().Length;//key的长度  加  :的长度 引号长度
  410.             string jsonh = rawjson.Substring(statsIndex, rawjson.Length - statsIndex);
  411.             bool requ = true;
  412.             if (jsonh.StartsWith(",") && requ)
  413.             {
  414.                 jsonh = jsonh.Substring(1);
  415.                 requ = false;
  416.             }
  417.             if (jsonq.EndsWith(",") && requ)
  418.                 jsonq = jsonq.Substring(0, jsonq.Length - 1);
  419.             rawjson = jsonq + jsonh;
  420.         }
  421.         public override string ToString()
  422.         {
  423.             return rawjson;
  424.         }
  425.         private void Insert(object key, object value, bool add)
  426.         {
  427.             object addkey = key;
  428.             if (add==false)
  429.             {
  430.                 if (rawjson.Trim() == "")
  431.                 {
  432.                     Insert(key, value, true);
  433.                     return;
  434.                 }
  435.                 if (key.GetType() == typeof(int))
  436.                 {
  437.                     if ((int)key < 0)
  438.                         throw new Exception("索引值为负值");
  439.                 }
  440.                 if (isModel!=true)
  441.                 {
  442.                     Value = value.ToString();
  443.                     return;
  444.                 }
  445.                 if (key.GetType() == typeof(int))
  446.                 {
  447.                     key = GetKey((int)key);
  448.                 }
  449.                 if (!ContainsKey(key.ToString()))
  450.                 {
  451.                     Insert(key, value, true);
  452.                     return;
  453.                 }
  454.                 else
  455.                 {
  456.                     string oldValue = GetValue(key.ToString());
  457.                     int statsIndex = rawjson.IndexOf(""" + key.ToString() + """);//key的位置
  458.                     statsIndex += key.ToString().Length + 1 + 2;//key的长度 加  :的长度 引号长度
  459.                     string jsonq = rawjson.Substring(0, statsIndex);
  460.                     statsIndex = statsIndex + oldValue.Length;
  461.                     string jsonh = rawjson.Substring(statsIndex, rawjson.Length - statsIndex);
  462.                     if (value.ToString().IndexOf(',') > -1&&!value.ToString().StartsWith("{") && !value.ToString().StartsWith("["))
  463.                     {
  464.                         value = value = """ + value + """;
  465.                     }
  466.                     rawjson = jsonq + value + jsonh;
  467.                     return;
  468.                 }
  469.             }
  470.             if (add)
  471.             {
  472.                 if (value.ToString().IndexOf(',') > -1 && key != null && !value.ToString().StartsWith("{") && !value.ToString().StartsWith("["))
  473.                     value = """ + value + """;
  474.                 string indexValue = """ + addkey + "":" + value;
  475.                 if (key==null)
  476.                     indexValue = value.ToString();
  477.                 if (rawjson == "")
  478.                 {
  479.                     rawjson = indexValue;
  480.                     return;
  481.                 }
  482.                 if (isModel != true)
  483.                 {
  484.                     if (addkey.ToString() == Key)
  485.                     {
  486.                         Value = value.ToString();
  487.                         return;
  488.                     }
  489.                     rawjson = "{" + rawjson + "}";
  490.                 }
  491.                 if (isModel && addkey!=null&&GetValue(addkey.ToString()) != null)
  492.                 {
  493.                     Insert(addkey, value, false);
  494.                     return;
  495.                 }
  496.                 rawjson = rawjson.Insert(this.rawjson.Length - 1, (this.rawjson.Length > 2 ? "," : "") + indexValue);
  497.             }
  498.         }
  499.     }
  500. }