using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Furion.JsonSerialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Myshipping.Core;
///
/// Json序列化工具类
///
public static class JsonUtil
{
///
/// JSON 字符串转 Object
///
///
///
///
public static T ToObject(this string json)
{
json = json.Replace(" ", "");
return json == null ? default(T) : JsonConvert.DeserializeObject(json);
}
///
/// JSON 字符串转 Object
///
///
///
public static object ToObject(this string Json)
{
return string.IsNullOrEmpty(Json) ? null : JsonConvert.DeserializeObject(Json);
}
///
/// Object 转 JSON字符串
///
///
///
public static string ToJsonString(this object obj)
{
return obj == null ? string.Empty : JsonConvert.SerializeObject(obj);
}
///
/// JSON 字符串转 JObject
///
///
///
public static JObject ToJObject(this string json)
{
return json == null ? JObject.Parse("{}") : JObject.Parse(json.Replace(" ", ""));
}
///
/// Dictionary 字符串转 Object
///
///
///
///
public static T ToObject(this IDictionary dictionary)
{
return dictionary.ToJsonString().ToObject();
}
///
/// 把数组转为逗号连接的字符串
///
///
///
///
public static string ArrayToString(dynamic data, string Str)
{
string resStr = Str;
foreach (var item in data)
{
if (resStr != "")
{
resStr += ",";
}
if (item is string)
{
resStr += item;
}
else
{
resStr += item.ToString();
}
}
return resStr;
}
///
/// 判断是否有交集
///
///
///
///
///
public static bool IsArrayIntersection(List list1, List list2)
{
List t = list1.Distinct().ToList();
var exceptArr = t.Except(list2).ToList();
if (exceptArr.Count < t.Count)
{
return true;
}
else
{
return false;
}
}
///
/// 处理输入的数据,变为大写,全角改为半角
///
/// 数据对象
/// 不需要转大写的字段
public static void PropToUpper(object model, params string[] excepProp)
{
var props = model.GetType().GetProperties();
foreach (PropertyInfo prop in props)
{
var propName = prop.Name;
if (excepProp != null && excepProp.Length > 0)
{
if (!excepProp.Contains(propName))
{
if (prop.PropertyType == typeof(string))
{
object sourceVal = prop.GetValue(model);
if (sourceVal != null)
{
prop.SetValue(model, ToDBC(sourceVal.ToString().ToUpper()));
}
}
}
}
}
}
///
/// 全角改为半角
///
///
///
public static string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
return new string(c);
}
///
/// 长TAB键的识别替换空格
///
public static string TrimFields(object model)
{
var props = model.GetType().GetProperties();
foreach (PropertyInfo prop in props)
{
var propName = prop.Name;
if (prop.PropertyType == typeof(string))
{
object sourceVal = prop.GetValue(model);
if (sourceVal != null)
{
prop.SetValue(model, sourceVal.ToString().Replace(" ", " "));
prop.SetValue(model, sourceVal.ToString().Replace(" ", " "));
prop.SetValue(model, sourceVal.ToString().Replace(",", ","));
prop.SetValue(model, sourceVal.ToString().Replace("。", "."));
prop.SetValue(model, sourceVal.ToString().Replace("?", "?"));
prop.SetValue(model, sourceVal.ToString().Replace("!", "!"));
prop.SetValue(model, sourceVal.ToString().Replace("《", "<<"));
prop.SetValue(model, sourceVal.ToString().Replace("》", ">>"));
prop.SetValue(model, sourceVal.ToString().Replace("》", ">>"));
prop.SetValue(model, sourceVal.ToString().Replace(";", ";"));
prop.SetValue(model, sourceVal.ToString().Replace("‘", "'"));
prop.SetValue(model, sourceVal.ToString().Replace("’", "'"));
prop.SetValue(model, sourceVal.ToString().Replace("、", ","));
if(Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]")&& propName.ToUpper() == "MBLNO")
{
return "提单号存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "HBLNO")
{
return "分提单号存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "VESSEL")
{
return "船名存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "VOYNO")
{
return "海关航次存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "VOYNOINNER")
{
return "内部航次存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "PLACERECEIPT")
{
return "收货地存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "PORTLOAD")
{
return "起运港存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "PORTDISCHARGE")
{
return "卸货港存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "PLACEDELIVERY")
{
return "交货地存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "DESTINATION")
{
return "目的地存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "MARKS")
{
return "唛头存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "HSCODE")
{
return "HS代码存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "DESCRIPTION")
{
return "货描存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "BOOKINGNO")
{
return "订舱编号存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "BOOKINGNO")
{
return "订舱编号存在中文字符";
}
if (Regex.IsMatch(sourceVal.ToString(), @"[\u4e00-\u9fa5]") && propName.ToUpper() == "PONO")
{
return "PONO存在中文字符";
}
}
}
}
return "";
}
}