741 lines
30 KiB
C#
741 lines
30 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Text.RegularExpressions;
|
|||
|
|
using System.Reflection;
|
|||
|
|
using CYQ.Data.Table;
|
|||
|
|
using CYQ.Data.Tool;
|
|||
|
|
|
|||
|
|
namespace CYQ.Data.Json
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ָ<EFBFBD>Json<6F>ַ<EFBFBD><D6B7><EFBFBD>Ϊ<EFBFBD>ֵ伯<D6B5>ϡ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
internal partial class JsonSplit
|
|||
|
|
{
|
|||
|
|
internal static bool IsJson(string json)
|
|||
|
|
{
|
|||
|
|
return IsJson(json, false);
|
|||
|
|
}
|
|||
|
|
internal static bool IsJson(string json, bool isStrictMode)
|
|||
|
|
{
|
|||
|
|
int errIndex;
|
|||
|
|
return IsJson(json, isStrictMode, out errIndex);
|
|||
|
|
}
|
|||
|
|
internal static bool IsJson(string json, bool isStrictMode, out int errIndex)
|
|||
|
|
{
|
|||
|
|
errIndex = 0;
|
|||
|
|
|
|||
|
|
if (string.IsNullOrEmpty(json) || json.Length < 2 ||
|
|||
|
|
((json[0] != '{' && json[json.Length - 1] != '}') && (json[0] != '[' && json[json.Length - 1] != ']')))
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
CharState cs = new CharState(isStrictMode);
|
|||
|
|
for (int i = 0; i < json.Length; i++)
|
|||
|
|
{
|
|||
|
|
//char c = ;
|
|||
|
|
if (cs.IsKeyword(json[i]) && cs.childrenStart)//<2F><><EFBFBD>ùؼ<C3B9><D8BC><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
|
|||
|
|
{
|
|||
|
|
int err;
|
|||
|
|
int length = GetValueLength(isStrictMode, json, i, true, out err);
|
|||
|
|
cs.childrenStart = false;
|
|||
|
|
if (err > 0)
|
|||
|
|
{
|
|||
|
|
errIndex = i + err;
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
i = i + length - 1;
|
|||
|
|
}
|
|||
|
|
if (cs.isError)
|
|||
|
|
{
|
|||
|
|
errIndex = i;
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return !cs.arrayStart && !cs.jsonStart; //ֻҪ<D6BB><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>رգ<D8B1><D5A3><EFBFBD>ʧ<EFBFBD><CAA7>
|
|||
|
|
}
|
|||
|
|
internal static List<Dictionary<string, string>> Split(string json)
|
|||
|
|
{
|
|||
|
|
return Split(json, 0);
|
|||
|
|
}
|
|||
|
|
///// <summary>
|
|||
|
|
///// <20><><EFBFBD><EFBFBD>Json
|
|||
|
|
///// </summary>
|
|||
|
|
///// <returns></returns>
|
|||
|
|
//internal static List<Dictionary<string, string>> Split(string json, int topN)
|
|||
|
|
//{
|
|||
|
|
// List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
|
|||
|
|
|
|||
|
|
// if (!string.IsNullOrEmpty(json))
|
|||
|
|
// {
|
|||
|
|
// Dictionary<string, string> dic = new Dictionary<string, string>(16, StringComparer.OrdinalIgnoreCase);
|
|||
|
|
// int keyIndex = -1, valueIndex = -1;
|
|||
|
|
// int keyLength = 0, valueLength = 0;
|
|||
|
|
// CharState cs = new CharState(false);
|
|||
|
|
// try
|
|||
|
|
// {
|
|||
|
|
// #region <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
// for (int i = 0; i < json.Length; i++)
|
|||
|
|
// {
|
|||
|
|
// char c = json[i];
|
|||
|
|
// if (!cs.IsKeyword(c))//<2F><><EFBFBD>ùؼ<C3B9><D8BC><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
|
|||
|
|
// {
|
|||
|
|
// if (cs.jsonStart)//Json<6F><6E><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
// {
|
|||
|
|
// if (cs.keyStart > 0)
|
|||
|
|
// {
|
|||
|
|
// if (keyIndex == -1)
|
|||
|
|
// {
|
|||
|
|
// keyIndex = i;
|
|||
|
|
// }
|
|||
|
|
// keyLength++;
|
|||
|
|
// }
|
|||
|
|
// else if (cs.valueStart > 0)
|
|||
|
|
// {
|
|||
|
|
// if (valueIndex == -1)
|
|||
|
|
// {
|
|||
|
|
// valueIndex = i;
|
|||
|
|
// }
|
|||
|
|
// valueLength++;
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// else if (!cs.arrayStart)//json<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飬<EFBFBD><E9A3AC><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>
|
|||
|
|
// {
|
|||
|
|
// break;
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// else if (cs.childrenStart)//<2F><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ֵ״̬<D7B4>¡<EFBFBD>
|
|||
|
|
// {
|
|||
|
|
// int temp;
|
|||
|
|
// valueLength = GetValueLength(false, json, i, false, out temp);//<2F>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶȿ<D9B6><C8BF><EFBFBD>10<31><30>
|
|||
|
|
// valueIndex = i;
|
|||
|
|
// cs.childrenStart = false;
|
|||
|
|
// cs.valueStart = 0;
|
|||
|
|
// cs.setDicValue = true;
|
|||
|
|
// i = i + valueLength - 1;
|
|||
|
|
// }
|
|||
|
|
// if (cs.setDicValue)//<2F><><EFBFBD>ü<EFBFBD>ֵ<EFBFBD>ԡ<EFBFBD>
|
|||
|
|
// {
|
|||
|
|
// if (keyLength > 0)
|
|||
|
|
// {
|
|||
|
|
// string k = json.Substring(keyIndex, keyLength);// key.ToString();
|
|||
|
|
// if (!dic.ContainsKey(k))
|
|||
|
|
// {
|
|||
|
|
// string val = valueLength > 0 ? json.Substring(valueIndex, valueLength) : ""; //value.ToString();
|
|||
|
|
// bool isNull = json[i - 5] == ':' && json[i] != '"' && valueLength == 4 && val == "null";
|
|||
|
|
// if (isNull)
|
|||
|
|
// {
|
|||
|
|
// val = "";
|
|||
|
|
// }
|
|||
|
|
// dic.Add(k, val);
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// cs.setDicValue = false;
|
|||
|
|
// keyIndex = valueIndex = -1;
|
|||
|
|
// keyLength = valueLength = 0;
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// if (!cs.jsonStart && dic.Count > 0)
|
|||
|
|
// {
|
|||
|
|
// result.Add(dic);
|
|||
|
|
// if (topN > 0 && result.Count >= topN)
|
|||
|
|
// {
|
|||
|
|
// return result;
|
|||
|
|
// }
|
|||
|
|
// if (cs.arrayStart)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>顣
|
|||
|
|
// {
|
|||
|
|
// dic = new Dictionary<string, string>(16, StringComparer.OrdinalIgnoreCase);
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// #endregion
|
|||
|
|
// }
|
|||
|
|
// catch (Exception err)
|
|||
|
|
// {
|
|||
|
|
// Log.Write(err, LogType.Error);
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// return result;
|
|||
|
|
//}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>Json
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
internal static List<Dictionary<string, string>> Split(string json, int topN)
|
|||
|
|
{
|
|||
|
|
List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
|
|||
|
|
|
|||
|
|
if (!string.IsNullOrEmpty(json))
|
|||
|
|
{
|
|||
|
|
Dictionary<string, string> dic = new Dictionary<string, string>(16, StringComparer.OrdinalIgnoreCase);
|
|||
|
|
//string key = string.Empty;
|
|||
|
|
StringBuilder key = new StringBuilder(32);
|
|||
|
|
StringBuilder value = new StringBuilder();
|
|||
|
|
CharState cs = new CharState(false);
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
for (int i = 0; i < json.Length; i++)
|
|||
|
|
{
|
|||
|
|
char c = json[i];
|
|||
|
|
if (!cs.IsKeyword(c))//<2F><><EFBFBD>ùؼ<C3B9><D8BC><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
|
|||
|
|
{
|
|||
|
|
if (cs.jsonStart)//Json<6F><6E><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (cs.keyStart > 0)
|
|||
|
|
{
|
|||
|
|
key.Append(c);
|
|||
|
|
}
|
|||
|
|
else if (cs.valueStart > 0)
|
|||
|
|
{
|
|||
|
|
value.Append(c);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (!cs.arrayStart)//json<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飬<EFBFBD><E9A3AC><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (cs.childrenStart)//<2F><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ֵ״̬<D7B4>¡<EFBFBD>
|
|||
|
|
{
|
|||
|
|
//string item = json.Substring(i);
|
|||
|
|
int temp;
|
|||
|
|
// int length = GetValueLength(false, json.Substring(i), false, out temp);//<2F><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD><C5BB>Ŀռ䣬<D5BC><E4A3AC>json<6F><6E>i<EFBFBD><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>string
|
|||
|
|
int length = GetValueLength(false, json, i, false, out temp);//<2F>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶȿ<D9B6><C8BF><EFBFBD>10<31><30>
|
|||
|
|
|
|||
|
|
value.Length = 0;
|
|||
|
|
value.Append(json.Substring(i, length));
|
|||
|
|
cs.childrenStart = false;
|
|||
|
|
cs.valueStart = 0;
|
|||
|
|
//cs.state = 0;
|
|||
|
|
cs.setDicValue = true;
|
|||
|
|
i = i + length - 1;
|
|||
|
|
}
|
|||
|
|
if (cs.setDicValue)//<2F><><EFBFBD>ü<EFBFBD>ֵ<EFBFBD>ԡ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (key.Length > 0)
|
|||
|
|
{
|
|||
|
|
string k = key.ToString();
|
|||
|
|
if (!dic.ContainsKey(k))
|
|||
|
|
{
|
|||
|
|
string val = value.ToString();
|
|||
|
|
bool isNull = i > 4 && json[i - 5] == ':' && json[i] != '"' && value.Length == 4 && val == "null";
|
|||
|
|
if (isNull)
|
|||
|
|
{
|
|||
|
|
val = "";
|
|||
|
|
}
|
|||
|
|
dic.Add(k, val);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
cs.setDicValue = false;
|
|||
|
|
key.Length = 0;
|
|||
|
|
value.Length = 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!cs.jsonStart && dic.Count > 0)
|
|||
|
|
{
|
|||
|
|
result.Add(dic);
|
|||
|
|
if (topN > 0 && result.Count >= topN)
|
|||
|
|
{
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
if (cs.arrayStart)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>顣
|
|||
|
|
{
|
|||
|
|
dic = new Dictionary<string, string>(16, StringComparer.OrdinalIgnoreCase);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Log.Write(err, LogType.Error);
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
key = null;
|
|||
|
|
value = null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
/*
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡֵ<C8A1>ij<EFBFBD><C4B3>ȣ<EFBFBD><C8A3><EFBFBD>JsonֵǶ<D6B5><C7B6><EFBFBD><EFBFBD>"{"<22><>"["<22><>ͷʱ<CDB7><CAB1>
|
|||
|
|
/// </summary>
|
|||
|
|
private static int GetValueLength(bool isStrictMode, string json, bool breakOnErr, out int errIndex)
|
|||
|
|
{
|
|||
|
|
errIndex = 0;
|
|||
|
|
int len = json.Length - 1;
|
|||
|
|
if (!string.IsNullOrEmpty(json))
|
|||
|
|
{
|
|||
|
|
CharState cs = new CharState(isStrictMode);
|
|||
|
|
char c;
|
|||
|
|
for (int i = 0; i < json.Length; i++)
|
|||
|
|
{
|
|||
|
|
c = json[i];
|
|||
|
|
if (!cs.IsKeyword(c))//<2F><><EFBFBD>ùؼ<C3B9><D8BC><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
|
|||
|
|
{
|
|||
|
|
if (!cs.jsonStart && !cs.arrayStart)//json<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飬<EFBFBD><E9A3AC><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (cs.childrenStart)//<2F><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ֵ״̬<D7B4>¡<EFBFBD>
|
|||
|
|
{
|
|||
|
|
int length = GetValueLength(isStrictMode, json.Substring(i), breakOnErr, out errIndex);//<2F>ݹ<EFBFBD><DDB9><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
cs.childrenStart = false;
|
|||
|
|
cs.valueStart = 0;
|
|||
|
|
//cs.state = 0;
|
|||
|
|
i = i + length - 1;
|
|||
|
|
}
|
|||
|
|
if (breakOnErr && cs.isError)
|
|||
|
|
{
|
|||
|
|
errIndex = i;
|
|||
|
|
return i;
|
|||
|
|
}
|
|||
|
|
if (!cs.jsonStart && !cs.arrayStart)//<2F><>¼<EFBFBD><C2BC>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>λ<EFBFBD>á<EFBFBD>
|
|||
|
|
{
|
|||
|
|
len = i + 1;//<2F><><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD><EFBFBD><EFBFBD>+1
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return len;
|
|||
|
|
}
|
|||
|
|
*/
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡֵ<C8A1>ij<EFBFBD><C4B3>ȣ<EFBFBD><C8A3><EFBFBD>JsonֵǶ<D6B5><C7B6><EFBFBD><EFBFBD>"{"<22><>"["<22><>ͷʱ<CDB7><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
private static int GetValueLength(bool isStrictMode, string json, int startIndex, bool breakOnErr, out int errIndex)
|
|||
|
|
{
|
|||
|
|
errIndex = 0;
|
|||
|
|
int len = json.Length - 1 - startIndex;
|
|||
|
|
if (!string.IsNullOrEmpty(json))
|
|||
|
|
{
|
|||
|
|
CharState cs = new CharState(isStrictMode);
|
|||
|
|
char c;
|
|||
|
|
for (int i = startIndex; i < json.Length; i++)
|
|||
|
|
{
|
|||
|
|
c = json[i];
|
|||
|
|
if (!cs.IsKeyword(c))//<2F><><EFBFBD>ùؼ<C3B9><D8BC><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
|
|||
|
|
{
|
|||
|
|
if (!cs.jsonStart && !cs.arrayStart)//json<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飬<EFBFBD><E9A3AC><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (cs.childrenStart)//<2F><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ֵ״̬<D7B4>¡<EFBFBD>
|
|||
|
|
{
|
|||
|
|
int length = GetValueLength(isStrictMode, json, i, breakOnErr, out errIndex);//<2F>ݹ<EFBFBD><DDB9><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
cs.childrenStart = false;
|
|||
|
|
cs.valueStart = 0;
|
|||
|
|
i = i + length - 1;
|
|||
|
|
}
|
|||
|
|
if (breakOnErr && cs.isError)
|
|||
|
|
{
|
|||
|
|
errIndex = i;
|
|||
|
|
return i - startIndex;
|
|||
|
|
}
|
|||
|
|
if (!cs.jsonStart && !cs.arrayStart)//<2F><>¼<EFBFBD><C2BC>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>λ<EFBFBD>á<EFBFBD>
|
|||
|
|
{
|
|||
|
|
len = i + 1;//<2F><><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD><EFBFBD><EFBFBD>+1
|
|||
|
|
len = len - startIndex;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return len;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD>չתʵ<EFBFBD><EFBFBD>T
|
|||
|
|
|
|||
|
|
internal static T ToEntity<T>(string json, EscapeOp op)
|
|||
|
|
{
|
|||
|
|
List<T> t = ToList<T>(json, 0, op);
|
|||
|
|
if (t.Count > 0)
|
|||
|
|
{
|
|||
|
|
return t[0];
|
|||
|
|
}
|
|||
|
|
return default(T);
|
|||
|
|
}
|
|||
|
|
internal static List<T> ToList<T>(string json, int topN, EscapeOp op)
|
|||
|
|
{
|
|||
|
|
Type t = typeof(T);
|
|||
|
|
if (t.IsValueType || t.Name == "String")
|
|||
|
|
{
|
|||
|
|
return new MDataRow().GetObj(typeof(List<T>), json) as List<T>;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
List<T> result = new List<T>();
|
|||
|
|
|
|||
|
|
if (!string.IsNullOrEmpty(json) && json.Length > 4)
|
|||
|
|
{
|
|||
|
|
// object entity = Activator.CreateInstance(t);
|
|||
|
|
T entity = Activator.CreateInstance<T>();
|
|||
|
|
bool hasSetValue = false;
|
|||
|
|
List<PropertyInfo> pInfoList = ReflectTool.GetPropertyList(t);
|
|||
|
|
List<FieldInfo> fInfoList = ReflectTool.GetFieldList(t);
|
|||
|
|
//string key = string.Empty;
|
|||
|
|
StringBuilder key = new StringBuilder(32);
|
|||
|
|
StringBuilder value = new StringBuilder();
|
|||
|
|
CharState cs = new CharState(false);
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
char c;
|
|||
|
|
for (int i = 0; i < json.Length; i++)
|
|||
|
|
{
|
|||
|
|
c = json[i];
|
|||
|
|
if (!cs.IsKeyword(c))//<2F><><EFBFBD>ùؼ<C3B9><D8BC><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
|
|||
|
|
{
|
|||
|
|
if (cs.jsonStart)//Json<6F><6E><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (cs.keyStart > 0)
|
|||
|
|
{
|
|||
|
|
key.Append(c);
|
|||
|
|
}
|
|||
|
|
else if (cs.valueStart > 0)
|
|||
|
|
{
|
|||
|
|
value.Append(c);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (!cs.arrayStart)//json<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飬<EFBFBD><E9A3AC><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (cs.childrenStart)//<2F><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ֵ״̬<D7B4>¡<EFBFBD>
|
|||
|
|
{
|
|||
|
|
int temp;
|
|||
|
|
int length = GetValueLength(false, json, i, false, out temp);//<2F>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶȿ<D9B6><C8BF><EFBFBD>10<31><30>
|
|||
|
|
value.Length = 0;
|
|||
|
|
value.Append(json.Substring(i, length));
|
|||
|
|
cs.childrenStart = false;
|
|||
|
|
cs.valueStart = 0;
|
|||
|
|
cs.setDicValue = true;
|
|||
|
|
i = i + length - 1;
|
|||
|
|
}
|
|||
|
|
if (cs.setDicValue)//<2F><><EFBFBD>ü<EFBFBD>ֵ<EFBFBD>ԡ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (key.Length > 0)
|
|||
|
|
{
|
|||
|
|
string k = key.ToString();
|
|||
|
|
string val = value.ToString();//.TrimEnd('\r', '\n', '\t');
|
|||
|
|
bool isNull = i > 4 && json[i - 5] == ':' && json[i] != '"' && val.Length == 4 && val == "null";
|
|||
|
|
if (isNull)
|
|||
|
|
{
|
|||
|
|
val = "";
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
val = JsonHelper.UnEscape(val, op);
|
|||
|
|
}
|
|||
|
|
bool hasProperty = false;
|
|||
|
|
object o = val;
|
|||
|
|
foreach (PropertyInfo p in pInfoList)
|
|||
|
|
{
|
|||
|
|
if (String.Compare(p.Name, k, StringComparison.OrdinalIgnoreCase) == 0)
|
|||
|
|
{
|
|||
|
|
hasProperty = true;
|
|||
|
|
if (p.CanWrite)
|
|||
|
|
{
|
|||
|
|
if (p.PropertyType.Name != "String")
|
|||
|
|
{
|
|||
|
|
o = ConvertTool.ChangeType(val, p.PropertyType);
|
|||
|
|
}
|
|||
|
|
p.SetValue(entity, o, null);
|
|||
|
|
hasSetValue = true;
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (!hasProperty && fInfoList.Count > 0)
|
|||
|
|
{
|
|||
|
|
foreach (FieldInfo f in fInfoList)
|
|||
|
|
{
|
|||
|
|
if (String.Compare(f.Name, k, StringComparison.OrdinalIgnoreCase) == 0)
|
|||
|
|
{
|
|||
|
|
if (f.FieldType.Name != "String")
|
|||
|
|
{
|
|||
|
|
o = ConvertTool.ChangeType(val, f.FieldType);
|
|||
|
|
}
|
|||
|
|
f.SetValue(entity, o);
|
|||
|
|
hasSetValue = true;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
cs.setDicValue = false;
|
|||
|
|
key.Length = 0;
|
|||
|
|
value.Length = 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!cs.jsonStart && hasSetValue)
|
|||
|
|
{
|
|||
|
|
result.Add(entity);
|
|||
|
|
if (topN > 0 && result.Count >= topN)
|
|||
|
|
{
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
if (cs.arrayStart)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>顣
|
|||
|
|
{
|
|||
|
|
entity = Activator.CreateInstance<T>();
|
|||
|
|
hasSetValue = false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Log.Write(err, LogType.Error);
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
key = null;
|
|||
|
|
value = null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// ֧<><D6A7>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>һ
|
|||
|
|
/// </summary>
|
|||
|
|
internal static object ToEntityOrList(Type t, string json, EscapeOp op)
|
|||
|
|
{
|
|||
|
|
Type toType = t;
|
|||
|
|
object listObj = null;
|
|||
|
|
MethodInfo method = null;
|
|||
|
|
if (t.IsGenericType && (t.Name.StartsWith("List") || t.Name.StartsWith("IList") || t.Name.StartsWith("MList")))
|
|||
|
|
{
|
|||
|
|
Type[] paraTypeList = null;
|
|||
|
|
ReflectTool.GetArgumentLength(ref t, out paraTypeList);
|
|||
|
|
toType = paraTypeList[0];
|
|||
|
|
if (toType.IsValueType || toType.Name == "String")
|
|||
|
|
{
|
|||
|
|
return new MDataRow().GetObj(t, json);
|
|||
|
|
}
|
|||
|
|
listObj = Activator.CreateInstance(t);//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>
|
|||
|
|
|
|||
|
|
method = t.GetMethod("Add");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!string.IsNullOrEmpty(json))
|
|||
|
|
{
|
|||
|
|
object entity = Activator.CreateInstance(toType);
|
|||
|
|
bool hasSetValue = false;
|
|||
|
|
List<PropertyInfo> pInfoList = ReflectTool.GetPropertyList(toType);
|
|||
|
|
List<FieldInfo> fInfoList = ReflectTool.GetFieldList(toType);
|
|||
|
|
//string key = string.Empty;
|
|||
|
|
StringBuilder key = new StringBuilder(32);
|
|||
|
|
StringBuilder value = new StringBuilder();
|
|||
|
|
CharState cs = new CharState(false);
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
char c;
|
|||
|
|
for (int i = 0; i < json.Length; i++)
|
|||
|
|
{
|
|||
|
|
c = json[i];
|
|||
|
|
if (!cs.IsKeyword(c))//<2F><><EFBFBD>ùؼ<C3B9><D8BC><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
|
|||
|
|
{
|
|||
|
|
if (cs.jsonStart)//Json<6F><6E><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (cs.keyStart > 0)
|
|||
|
|
{
|
|||
|
|
key.Append(c);
|
|||
|
|
}
|
|||
|
|
else if (cs.valueStart > 0)
|
|||
|
|
{
|
|||
|
|
value.Append(c);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (!cs.arrayStart)//json<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飬<EFBFBD><E9A3AC><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (cs.childrenStart)//<2F><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ֵ״̬<D7B4>¡<EFBFBD>
|
|||
|
|
{
|
|||
|
|
int temp;
|
|||
|
|
int length = GetValueLength(false, json, i, false, out temp);//<2F>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶȿ<D9B6><C8BF><EFBFBD>10<31><30>
|
|||
|
|
value.Length = 0;
|
|||
|
|
value.Append(json.Substring(i, length));
|
|||
|
|
cs.childrenStart = false;
|
|||
|
|
cs.valueStart = 0;
|
|||
|
|
cs.setDicValue = true;
|
|||
|
|
i = i + length - 1;
|
|||
|
|
}
|
|||
|
|
if (cs.setDicValue)//<2F><><EFBFBD>ü<EFBFBD>ֵ<EFBFBD>ԡ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (key.Length > 0)
|
|||
|
|
{
|
|||
|
|
string k = key.ToString();
|
|||
|
|
string val = value.ToString();//.TrimEnd('\r', '\n', '\t');
|
|||
|
|
bool isNull = i > 4 && json[i - 5] == ':' && json[i] != '"' && val.Length == 4 && val == "null";
|
|||
|
|
if (isNull)
|
|||
|
|
{
|
|||
|
|
val = "";
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
val = JsonHelper.UnEscape(val, op);
|
|||
|
|
}
|
|||
|
|
bool hasProperty = false;
|
|||
|
|
object o = val;
|
|||
|
|
foreach (PropertyInfo p in pInfoList)
|
|||
|
|
{
|
|||
|
|
if (String.Compare(p.Name, k, StringComparison.OrdinalIgnoreCase) == 0)
|
|||
|
|
{
|
|||
|
|
hasProperty = true;
|
|||
|
|
if (p.CanWrite)
|
|||
|
|
{
|
|||
|
|
if (p.PropertyType.Name != "String")
|
|||
|
|
{
|
|||
|
|
o = ConvertTool.ChangeType(val, p.PropertyType);
|
|||
|
|
}
|
|||
|
|
p.SetValue(entity, o, null);
|
|||
|
|
hasSetValue = true;
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (!hasProperty && fInfoList.Count > 0)
|
|||
|
|
{
|
|||
|
|
foreach (FieldInfo f in fInfoList)
|
|||
|
|
{
|
|||
|
|
if (String.Compare(f.Name, k, StringComparison.OrdinalIgnoreCase) == 0)
|
|||
|
|
{
|
|||
|
|
if (f.FieldType.Name != "String")
|
|||
|
|
{
|
|||
|
|
o = ConvertTool.ChangeType(val, f.FieldType);
|
|||
|
|
}
|
|||
|
|
f.SetValue(entity, o);
|
|||
|
|
hasSetValue = true;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
cs.setDicValue = false;
|
|||
|
|
key.Length = 0;
|
|||
|
|
value.Length = 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!cs.jsonStart && hasSetValue)
|
|||
|
|
{
|
|||
|
|
if (method != null)
|
|||
|
|
{
|
|||
|
|
method.Invoke(listObj, new object[] { entity });
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return entity;
|
|||
|
|
}
|
|||
|
|
if (cs.arrayStart)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>顣
|
|||
|
|
{
|
|||
|
|
entity = Activator.CreateInstance(toType);
|
|||
|
|
hasSetValue = false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Log.Write(err, LogType.Error);
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
key = null;
|
|||
|
|
value = null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return listObj;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
internal partial class JsonSplit
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>json<6F><6E><EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD><D6B3>ַ<EFBFBD><D6B7><EFBFBD>List
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="jsonArray">["a,","bbb,,"]</param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
internal static List<string> SplitEscapeArray(string jsonArray)
|
|||
|
|
{
|
|||
|
|
if (!string.IsNullOrEmpty(jsonArray))
|
|||
|
|
{
|
|||
|
|
jsonArray = jsonArray.Trim(' ', '[', ']');//["a,","bbb,,"]
|
|||
|
|
List<string> list = new List<string>();
|
|||
|
|
if (jsonArray.Length > 0)
|
|||
|
|
{
|
|||
|
|
string[] items = jsonArray.Split(',');
|
|||
|
|
string objStr = string.Empty;
|
|||
|
|
foreach (string value in items)
|
|||
|
|
{
|
|||
|
|
string item = value.Trim('\r', '\n', '\t', ' ');
|
|||
|
|
if (objStr == string.Empty)
|
|||
|
|
{
|
|||
|
|
objStr = item;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
objStr += "," + item;
|
|||
|
|
}
|
|||
|
|
char firstChar = objStr[0];
|
|||
|
|
if (firstChar == '"' || firstChar == '\'')
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>˫<EFBFBD><CBAB><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (GetCharCount(objStr, firstChar) % 2 == 0)//<2F><><EFBFBD>ų<EFBFBD>˫
|
|||
|
|
{
|
|||
|
|
list.Add(objStr.Trim(firstChar).Replace("\\" + firstChar, firstChar.ToString()));
|
|||
|
|
objStr = string.Empty;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
list.Add(item);
|
|||
|
|
objStr = string.Empty;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return list;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵĴ<D6B5><C4B4><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="c"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
private static int GetCharCount(string item, char c)
|
|||
|
|
{
|
|||
|
|
int num = 0;
|
|||
|
|
for (int i = 0; i < item.Length; i++)
|
|||
|
|
{
|
|||
|
|
if (item[i] == '\\')
|
|||
|
|
{
|
|||
|
|
i++;
|
|||
|
|
}
|
|||
|
|
else if (item[i] == c)
|
|||
|
|
{
|
|||
|
|
num++;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return num;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|