Skip to content

Commit

Permalink
增加AppConfig.JsonEscapte配置项(Default、Yes、No)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyq1162 committed Oct 17, 2017
1 parent 3e76cb3 commit bd4f2d1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
16 changes: 15 additions & 1 deletion Action/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,21 @@ public static bool IsEnumToInt
SetApp("IsEnumToInt", value.ToString());
}
}

/// <summary>
/// 是否ToJson输出时自动转义特殊符号("\ \r \t等)
/// 可配置项为(Default、Yes、No)
/// </summary>
public static string JsonEscape
{
get
{
return GetApp("JsonEscape", "Default");
}
set
{
SetApp("JsonEscape", value);
}
}
/// <summary>
/// Aop 插件配置项 示例配置:[ 完整类名,程序集(dll)名称]&lt;add key="Aop" value="Web.Aop.AopAction,Aop"/>
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Action/NoSqlAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Table/MDataRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Table/MDataTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
/// <param name="op">符号转义选项</param>
public string ToJson(bool addHead, bool addSchema, RowOp rowOp, bool isConvertNameToLower, EscapeOp op)
Expand Down
42 changes: 24 additions & 18 deletions Tool/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ public enum EscapeOp
/// </summary>
public partial class JsonHelper
{


internal static EscapeOp DefaultEscape
{
get
{
return (EscapeOp)Enum.Parse(typeof(EscapeOp), AppConfig.JsonEscape);
}
}
#region 实例属性

public JsonHelper()
{

}
/// <param name="addHead">with easyui header ?<para>是否带输出头</para></param>
public JsonHelper(bool addHead)
Expand All @@ -55,7 +63,7 @@ public JsonHelper(bool addHead, bool addSchema)
/// Escape options
/// <para>转义符号</para>
/// </summary>
public EscapeOp Escape = EscapeOp.Default;
public EscapeOp Escape= EscapeOp.Default;
/// <summary>
/// convert filed to lower
/// <para>是否将名称转为小写</para>
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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", "<br/>").Replace("\t", " ").Replace("\r", " ");
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bd4f2d1

Please sign in to comment.