Skip to content

Commit

Permalink
393:MDictionary增加索引取值或赋值。(2016-10-17)
Browse files Browse the repository at this point in the history
394:XHtmlAction、RSS的OnForeach的参数由Dictionary变为MDictionary(2016-10-17)
395:JsonHelper 修正对数组的输出和还原(2016-10-17)
  • Loading branch information
cyq1162 committed Oct 17, 2016
1 parent 35ee917 commit f407e61
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Action/MAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ public bool Fill()
/// {
/// if(action.Fill("name='路过秋天'")) //或者action.Fill(888) 或者 action.Fill(id=888)
/// {
/// action.SetTo(labUserName);
/// action.UI.SetTo(labUserName);
/// }
/// }
/// </code></example>
Expand Down
2 changes: 1 addition & 1 deletion Orm/DBFast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private static MAction GetMAction<T>()
{
string conn = string.Empty;
MAction action = new MAction(GetTableName<T>(out conn), conn);
//action.SetAopOff();
//action.SetAopState(CYQ.Data.Aop.AopOp.CloseAll);
return action;
}
private static string GetTableName<T>(out string conn)
Expand Down
6 changes: 3 additions & 3 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[assembly: AssemblyTitle("CYQ.Data 数据层(ORM)框架 V5 版本")]
[assembly: AssemblyDescription("论坛:http://www.cyqdata.com/cyqdata")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("秋式软件 (2016-10-16)")]
[assembly: AssemblyCompany("秋式软件 (2016-10-17)")]
[assembly: AssemblyProduct("CYQ.Data 数据层(ORM)框架 V5 版本")]
[assembly: AssemblyCopyright("版权所有 (C) 秋式软件 2010-2020")]
[assembly: AssemblyTrademark("CYQ.Data")]
Expand All @@ -31,5 +31,5 @@
//
// 可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值,
// 方法是按如下所示使用“*”:
[assembly: AssemblyVersion("5.6.6.6")]
[assembly: AssemblyFileVersion("5.6.6.6")]
[assembly: AssemblyVersion("5.6.6.7")]
[assembly: AssemblyFileVersion("5.6.6.7")]
76 changes: 40 additions & 36 deletions Table/MDataRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,40 +1138,32 @@ internal void SetToEntity(ref object obj, MDataRow row)
}
Type propType = p.PropertyType;
object objValue = null;
switch (StaticTool.GetSystemType(ref propType))
SysType sysType = StaticTool.GetSystemType(ref propType);
switch (sysType)
{
case SysType.Enum:
p.SetValue(obj, Enum.Parse(propType, cell.ToString()), null);
break;
case SysType.Base:
object value = StaticTool.ChangeType(cell.Value, p.PropertyType);
p.SetValue(obj, value, null);
break;
case SysType.Array:
if (cell.Value.GetType() == propType)
if (propType.Name == "String")
{
objValue = cell.Value;
//去掉转义符号
if (cell.strValue.IndexOf("\\\"") > -1)
{
p.SetValue(obj, cell.strValue.Replace("\\\"", "\""), null);
}
else
{
p.SetValue(obj, cell.strValue, null);
}
}
else
{
Type arrayType = Type.GetType(propType.FullName.Replace("[]", ""));
MDataTable dtArray = MDataTable.CreateFrom(cell.ToString(), TableSchema.GetColumns(arrayType));
objValue = Activator.CreateInstance(propType, dtArray.Rows.Count);//创建实例
Type objArrayListType = objValue.GetType();
MDataRow item;
for (int i = 0; i < dtArray.Rows.Count; i++)
{
item = dtArray.Rows[i];
object o = GetValue(item, arrayType);
MethodInfo method = objArrayListType.GetMethod("Set");
if (method != null)
{
method.Invoke(objValue, new object[] { i, o });
}
}
object value = StaticTool.ChangeType(cell.Value, p.PropertyType);
p.SetValue(obj, value, null);
}
p.SetValue(obj, objValue, null);
break;
case SysType.Array:
case SysType.Collection:
case SysType.Generic:
if (cell.Value.GetType() == propType)
Expand All @@ -1182,31 +1174,43 @@ internal void SetToEntity(ref object obj, MDataRow row)
{
Type[] argTypes = null;
int len = StaticTool.GetArgumentLength(ref propType, out argTypes);

objValue = Activator.CreateInstance(propType);//创建实例
Type objListType = objValue.GetType();
if (len == 1) // Table
{

MDataTable dt = MDataTable.CreateFrom(cell.ToString());//, SchemaCreate.GetColumns(argTypes[0])
foreach (MDataRow rowItem in dt.Rows)
List<string> items = JsonSplit.SplitEscapeArray(cell.strValue);//内部去掉转义符号
objValue = Activator.CreateInstance(propType, items.Count);//创建实例
Type objListType = objValue.GetType();
bool isArray = sysType == SysType.Array;
for (int i = 0; i < items.Count; i++)
{
object o = GetValue(rowItem, argTypes[0]);
MethodInfo method = objListType.GetMethod("Add");
if (method == null)
MethodInfo method;
if (isArray)
{
method = objListType.GetMethod("Push");
Object item = StaticTool.ChangeType(items[i], Type.GetType(propType.FullName.Replace("[]", "")));
method = objListType.GetMethod("Set");
if (method != null)
{
method.Invoke(objValue, new object[] { i, item });
}
}
if (method != null)
else
{
method.Invoke(objValue, new object[] { o });
Object item = StaticTool.ChangeType(items[i], argTypes[0]);
method = objListType.GetMethod("Add");
if (method == null)
{
method = objListType.GetMethod("Push");
}
if (method != null)
{
method.Invoke(objValue, new object[] { item });
}
}
}
dt = null;
}
else if (len == 2) // row
{
MDataRow mRow = MDataRow.CreateFrom(cell.Value, argTypes[1]);
objValue = Activator.CreateInstance(propType, mRow.Columns.Count);//创建实例
foreach (MDataCell mCell in mRow)
{
object mObj = GetValue(mCell.ToRow(), argTypes[1]);
Expand Down
2 changes: 1 addition & 1 deletion Table/MDataTableJoin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal static MDataTable Join(MDataTable dtA, string tableName, string joinOnN
{
joinOnName = action.Data.Columns.FirstPrimary.ColumnName;
}
//action.SetAopOff();
//action.SetAopState(CYQ.Data.Aop.AopOp.CloseAll);
action.dalHelper.IsAllowRecordSql = false;//屏蔽SQL日志记录 2000数据库大量的In条件会超时。
if (appendColumns.Length > 0)
{
Expand Down
56 changes: 34 additions & 22 deletions Tool/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,39 +198,46 @@ private string Format(string name, string value, bool children)
}
if (!children)
{
// int index = value.IndexOf('"');
SetEscape(ref value);
}
return "\"" + name + "\":" + (!children ? "\"" : "") + value + (!children ? "\"" : "");//;//.Replace("\",\"", "\" , \"").Replace("}{", "} {").Replace("},{", "}, {")
}
private void SetEscape(ref string value)
{
if (value.IndexOfAny(new char[] { '"', '\\' }) > -1)
{
int len = value.Length;
StringBuilder sb = new StringBuilder(len + 10);
for (int i = 0; i < len; i++)
{
switch (value[i])
char c = value[i];
switch (c)
{
case '"':
if (i == 0 || value[i - 1] != '\\')
{
value = value.Insert(i, "\\");
len++;//新插入了一个字符。
i++;//索引往前一个。
sb.Append("\\");
//value.Insert(i, "\\");
//len++;//新插入了一个字符。
//i++;//索引往前一个。
}
break;
case '\\':
if (i == len - 1 || (value[i + 1] != '"'))// && value[i + 1] != '\\'))
{
value = value.Insert(i, "\\");
len++;//新插入了一个字符。
i++;//索引往前一个。
sb.Append("\\");
//value.Insert(i, "\\");
//len++;//新插入了一个字符。
//i++;//索引往前一个。
}
break;
}
sb.Append(c);
}

//if (children)
//{
// value = value.Replace("\\\"", "\"");
//}
value = null;
value = sb.ToString();
}
return "\"" + name + "\":" + (!children ? "\"" : "") + value + (!children ? "\"" : "");//;//.Replace("\",\"", "\" , \"").Replace("}{", "} {").Replace("},{", "}, {")
}

/// <summary>
/// 输出Json字符串
/// </summary>
Expand Down Expand Up @@ -589,20 +596,27 @@ public void Fill(MDataRow row)
else if (groupID == 999)
{
Type t = cell.Struct.ValueType;
if (t.FullName == "System.Object")
{
t = cell.Value.GetType();
}
if (t.Name == "Byte[]")
{
value = Convert.ToBase64String(cell.Value as byte[]);
}
else
{

if (cell.Value is IEnumerable)
{
int len = StaticTool.GetArgumentLength(ref t);
if (len == 1)
{
MDataTable dt = MDataTable.CreateFrom(cell.Value);
value = dt.ToJson(false, false, _RowOp, IsConvertNameToLower);
JsonHelper js = new JsonHelper(false, false);
js._RowOp = _RowOp;
js.DateTimeFormatter = DateTimeFormatter;
js.IsConvertNameToLower = IsConvertNameToLower;
js.Fill(cell.Value);
value = js.ToString();
noQuot = true;
}
else if (len == 2)
Expand All @@ -613,10 +627,7 @@ public void Fill(MDataRow row)
}
else
{
if (t.FullName == "System.Object")
{
t = cell.Value.GetType();
}

if (!t.FullName.StartsWith("System."))//普通对象。
{
MDataRow oRow = new MDataRow(TableSchema.GetColumns(t));
Expand Down Expand Up @@ -717,6 +728,7 @@ public void Fill(object obj)
}
else
{
if (o is String) { SetEscape(ref str); }
jsonItems.Add("\"" + str + "\"");
}
}
Expand Down
63 changes: 62 additions & 1 deletion Tool/JsonSplit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CYQ.Data.Tool
/// <summary>
/// 分隔Json字符串为字典集合。
/// </summary>
internal class JsonSplit
internal partial class JsonSplit
{
internal static bool IsJson(string json)
{
Expand Down Expand Up @@ -452,4 +452,65 @@ private static bool SetCharState(char c, ref CharState cs)


}
internal partial class JsonSplit
{
internal static List<string> SplitEscapeArray(string jsonArray)
{
if (!string.IsNullOrEmpty(jsonArray))
{
jsonArray = jsonArray.Trim(' ', '[', ']');//["a,","bbb,,"]
if (jsonArray.Length > 0)
{
List<string> list = new List<string>();
string[] items = jsonArray.Split(',');
string objStr = string.Empty;
foreach (string item in items)
{
if (objStr == string.Empty) { objStr = item; }
else { objStr += "," + item; }
char firstChar = objStr[0];
if (firstChar == '"' || firstChar == '\'')
{
//检测双引号的数量
if (GetCharCount(objStr, firstChar) % 2 == 0)//引号成双
{
list.Add(objStr.Trim(firstChar).Replace("\\" + firstChar, firstChar.ToString()));
objStr = string.Empty;
}
}
else
{
list.Add(item);
objStr = string.Empty;
}
}
return list;
}


}
return null;
}
/// <summary>
/// 获取字符在字符串出现的次数
/// </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;
}
}
}
Loading

0 comments on commit f407e61

Please sign in to comment.