using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text.RegularExpressions;
namespace WCS_Client
{
///
/// 扩展.json序列反序列化
///
public static partial class JsonExtensions
{
///
/// 转成json对象
///
/// json字串
///
public static object ToJson(this string Json)
{
return Json == null ? null : JsonConvert.DeserializeObject(Json);
}
///
/// 转成json字串
///
/// 对象
///
public static string ToJson(this object obj)
{
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
return JsonConvert.SerializeObject(obj, timeConverter);
}
///
/// 转成json字串
///
/// 对象
/// 时间格式化
///
public static string ToJson(this object obj, string datetimeformats)
{
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformats };
return JsonConvert.SerializeObject(obj, timeConverter);
}
///
/// 字串反序列化成指定对象实体
///
/// 实体类型
/// 字串
///
public static T ToObject(this string Json)
{
return Json == null ? default(T) : JsonConvert.DeserializeObject(Json);
}
///
/// 字串反序列化成指定对象实体(列表)
///
/// 实体类型
/// 字串
///
public static List ToList(this string Json)
{
return Json == null ? null : JsonConvert.DeserializeObject>(Json);
}
///
/// 字串反序列化成DataTable
///
/// 字串
///
public static DataTable ToTable(this string Json)
{
return Json == null ? null : JsonConvert.DeserializeObject(Json);
}
///
/// 字串反序列化成linq对象
///
/// 字串
///
public static JObject ToJObject(this string Json)
{
return Json == null ? JObject.Parse("{}") : JObject.Parse(Json.Replace(" ", ""));
}
public static DataTable GetAttributesDataTable(this object obj)
{
var tb = new DataTable(obj.GetType().Name);
tb.Columns.Add("信号说明", typeof(string));
tb.Columns.Add("信号名称", typeof(string));
tb.Columns.Add("信号值", typeof(string));
Type t = obj.GetType();
//属性遍历
foreach (System.Reflection.PropertyInfo pi in t.GetProperties())
{
AttributeCollection attributes = TypeDescriptor.GetProperties(t)[pi.Name].Attributes;
DescriptionAttribute myAttribute = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
string describle = myAttribute.Description;
string proname = pi.Name;
var abc = pi.GetValue(obj, null);
var values = new object[3];
values[0] = describle;
values[1] = proname;
switch (proname)
{
case "TaskID":
values[1] = "任务号";
break;
case "TaskType":
values[1] = "任务类型";
break;
case "WorkMode":
values[1] = "运行模式";
break;
case "SystemStatus":
values[1] = "运行状态";
break;
case "StartPosition":
values[1] = "起始地址";
break;
case "PH_Status":
values[1] = "光电信号";
break;
case "DestPosition":
values[1] = "目标地址";
break;
//case "ACT_V":
// values[1] = "实际速度";
// break;
case "GOODSSTART":
values[1] = "起始地址";
break;
case "GOODSEND":
values[1] = "目标地址";
break;
case "REQUEST":
values[1] = "请求";
break;
case "RES1":
values[1] = "手动入库";
break;
case "HEIGHT":
values[1] = "高度";
break;
case "GOODSCODE":
values[1] = "货物编码";
break;
case "GOODSTYPE":
values[1] = "货物类型";
break;
case "GOODSSIZE":
values[1] = "货物尺寸";
break;
case "PH_STATUS":
values[1] = "光电信号";
break;
case "TASKNUM":
values[1] = "任务号";
break;
}
string P_regex = "^[\u4E00-\u9FA5]{0,}$";
var match = Regex.IsMatch(values[1].ToString(), P_regex, RegexOptions.IgnoreCase);
if (!match) continue;
values[2] = abc;
tb.Rows.Add(values);
}
return tb;
}
}
}