public JsonObject ToJson()
{
//对象转化成json
JsonObject json = new JsonObject();
//放置实体类型
json["EntityType"] = EntityType;
foreach (KeyValuePair<string, object> kvp in _customPropertyValues.AsEnumerable())
{
//该属性不用保存
if (NotSave != null && NotSave.Split(',').Contains(kvp.Key))
{
continue;
}
//空值直接赋值
else if (kvp.Value == null)
{
PropertyInfo pi = GetProperty(kvp.Key);
//空一对多关系不能发送到后台,否则会清空子
if (pi.PropertyType == typeof(BaseObjectList))
{
continue;
}
json[kvp.Key] = null;
}
//整数类型,可以为空
else if (kvp.Value is int)
json[kvp.Key] = (int)kvp.Value;
//double类型,可以为空
else if (kvp.Value is double)
json[kvp.Key] = (double)kvp.Value;
//decimal
else if (kvp.Value is decimal)
json[kvp.Key] = (decimal)kvp.Value;
//bool型
else if (kvp.Value is bool)
json[kvp.Key] = (bool)kvp.Value;
//字符串
else if (kvp.Value is string)
json[kvp.Key] = kvp.Value as string;
//日期
else if (kvp.Value is DateTime)
{
//DateTime from1970 = new DateTime(1970, 1, 1,0,0,0);
DateTime from1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local);
DateTime valDate = (DateTime)kvp.Value;
TimeSpan ts = valDate.ToUniversalTime() - from1970;
json[kvp.Key] = (Int64)(ts.TotalMilliseconds + 0.5);
}
//列表数据
else if (kvp.Value is BaseObjectList)
json[kvp.Key] = (kvp.Value as BaseObjectList).ToJson();
//其他对象
else if (kvp.Value is GeneralObject)
json[kvp.Key] = (kvp.Value as GeneralObject).ToJson();
else
throw new Exception("不认识的字段类型, " + kvp.Value.GetType());
}
return json;
}
#endregion