From bd4f2d1c7d6905bad7d7f276963fa9412590736c Mon Sep 17 00:00:00 2001 From: Aster <272657997@qq.com> Date: Wed, 18 Oct 2017 02:53:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0AppConfig.JsonEscapte?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=A1=B9=EF=BC=88Default=E3=80=81Yes?= =?UTF-8?q?=E3=80=81No=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Action/AppConfig.cs | 16 +++++++++++++++- Action/NoSqlAction.cs | 2 +- Table/MDataRow.cs | 2 +- Table/MDataTable.cs | 2 +- Tool/JsonHelper.cs | 42 ++++++++++++++++++++++++------------------ 5 files changed, 42 insertions(+), 22 deletions(-) diff --git a/Action/AppConfig.cs b/Action/AppConfig.cs index 22610da0..7a18b0b4 100644 --- a/Action/AppConfig.cs +++ b/Action/AppConfig.cs @@ -179,7 +179,21 @@ public static bool IsEnumToInt SetApp("IsEnumToInt", value.ToString()); } } - + /// + /// 是否ToJson输出时自动转义特殊符号("\ \r \t等) + /// 可配置项为(Default、Yes、No) + /// + public static string JsonEscape + { + get + { + return GetApp("JsonEscape", "Default"); + } + set + { + SetApp("JsonEscape", value); + } + } /// /// Aop 插件配置项 示例配置:[ 完整类名,程序集(dll)名称]<add key="Aop" value="Web.Aop.AopAction,Aop"/> /// diff --git a/Action/NoSqlAction.cs b/Action/NoSqlAction.cs index 16388c03..090592b8 100644 --- a/Action/NoSqlAction.cs +++ b/Action/NoSqlAction.cs @@ -541,7 +541,7 @@ private void Save() string text = string.Empty; if (string.IsNullOrEmpty(text)) { - text = _DalType == DalType.Txt ? Table.ToJson(false, true).Replace("},{", "},\r\n{").Trim('[', ']') : Table.ToXml(); + text = _DalType == DalType.Txt ? Table.ToJson(false, true, RowOp.None, false, EscapeOp.Default).Replace("},{", "},\r\n{").Trim('[', ']') : Table.ToXml(); } int tryAgainCount = 3; bool isError = false; diff --git a/Table/MDataRow.cs b/Table/MDataRow.cs index 0eed218c..a988f389 100644 --- a/Table/MDataRow.cs +++ b/Table/MDataRow.cs @@ -404,7 +404,7 @@ public MDataRow Set(object key, object value, int state) if (cell != null) { cell.Value = value; - if (state > 0 && state < 3) + if (state > -1 && state < 3) { cell.State = state; } diff --git a/Table/MDataTable.cs b/Table/MDataTable.cs index 7caba42c..390514eb 100644 --- a/Table/MDataTable.cs +++ b/Table/MDataTable.cs @@ -464,7 +464,7 @@ public string ToJson(bool addHead, bool addSchema, RowOp rowOp) public string ToJson(bool addHead, bool addSchema, RowOp rowOp, bool isConvertNameToLower) { - return ToJson(addHead, addSchema, rowOp, isConvertNameToLower, EscapeOp.Default); + return ToJson(addHead, addSchema, rowOp, isConvertNameToLower, JsonHelper.DefaultEscape); } /// תѡ public string ToJson(bool addHead, bool addSchema, RowOp rowOp, bool isConvertNameToLower, EscapeOp op) diff --git a/Tool/JsonHelper.cs b/Tool/JsonHelper.cs index 0e87251e..7a6906f7 100644 --- a/Tool/JsonHelper.cs +++ b/Tool/JsonHelper.cs @@ -31,11 +31,19 @@ public enum EscapeOp /// public partial class JsonHelper { - + + internal static EscapeOp DefaultEscape + { + get + { + return (EscapeOp)Enum.Parse(typeof(EscapeOp), AppConfig.JsonEscape); + } + } #region 实例属性 public JsonHelper() { + } /// with easyui header ?是否带输出头 public JsonHelper(bool addHead) @@ -55,7 +63,7 @@ public JsonHelper(bool addHead, bool addSchema) /// Escape options /// 转义符号 /// - public EscapeOp Escape = EscapeOp.Default; + public EscapeOp Escape= EscapeOp.Default; /// /// convert filed to lower /// 是否将名称转为小写 @@ -240,8 +248,8 @@ private string Format(string name, string value, bool children) } private void SetEscape(ref string value) { - //if (value.IndexOfAny(new char[] { '"', '\\' }) > -1) - if (value.IndexOfAny(new char[] { '"' }) > -1) + if (Escape == EscapeOp.No) { return; } + if (value.IndexOfAny(new char[] { '"', '\\' }) > -1)//easyui 输出时需要处理\\符号 { bool isInsert = false; int len = value.Length; @@ -256,20 +264,18 @@ private void SetEscape(ref string value) { isInsert = true; sb.Append("\\"); - //value.Insert(i, "\\"); - //len++;//新插入了一个字符。 - //i++;//索引往前一个。 } break; - //case '\\': - // if (i == len - 1 || (value[i + 1] != '"'))// && value[i + 1] != '\\')) - // { - // sb.Append("\\"); - // //value.Insert(i, "\\"); - // //len++;//新插入了一个字符。 - // //i++;//索引往前一个。 - // } - // break; + case '\\': + if (Escape == EscapeOp.Yes) + { + if (i == len - 1 || (value[i + 1] != '"'))// && value[i + 1] != '\\')) + { + isInsert = true; + sb.Append("\\"); + } + } + break; } sb.Append(c); } @@ -365,7 +371,7 @@ public override string ToString() sb.Append(footText.ToString() + "}"); } string json = sb.ToString(); - if ((Escape == EscapeOp.Default && AppConfig.IsWeb) || Escape == EscapeOp.Yes) // Web应用 + if (AppConfig.IsWeb || Escape != EscapeOp.No) // Web应用 { json = json.Replace("\n", "
").Replace("\t", " ").Replace("\r", " "); } @@ -1003,7 +1009,7 @@ internal static MDataTable ToMDataTable(string jsonOrFileName, MDataColumn mdc) } #endregion } - else if (mdc!=null && mdc.Count == 1) + else if (mdc != null && mdc.Count == 1) { string[] items = json.Trim('[', ']').Split(','); foreach (string item in items)