332 lines
12 KiB
C#
332 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Furion.FriendlyException;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Collections.Specialized;
|
|
using System.Data;
|
|
using System.Reflection;
|
|
using System.Text.RegularExpressions;
|
|
using System.Xml;
|
|
using RoadFlow.Utility;
|
|
|
|
namespace Ewide.RoadFlowLite.Utility
|
|
{
|
|
public static class JsonExtsions
|
|
{
|
|
|
|
|
|
public static JObject ToJObject(this string str, bool isToLower = true)
|
|
{
|
|
return JObject.Parse(isToLower ? str.ToLower() : str);
|
|
}
|
|
public static JArray ToJArray(this string str, bool isToLower = true)
|
|
{
|
|
return JArray.Parse(isToLower ? str.ToLower() : str);
|
|
}
|
|
public static string GetJsonValue(this JObject jObject, string jsonName, bool isToLower = false, bool isThrowExp = false)
|
|
{
|
|
if (jObject == null)
|
|
throw Oops.Oh("参数为空");
|
|
JToken token = jObject[isToLower ? jsonName.ToLower() : jsonName];
|
|
if (token == null)
|
|
{
|
|
foreach (var item in jObject)
|
|
{
|
|
if (item.Key.ToLower() == jsonName.ToLower())
|
|
token = item.Value;
|
|
}
|
|
}
|
|
if (token == null)
|
|
{
|
|
|
|
if (isThrowExp)
|
|
throw Oops.Oh(jsonName + "是必需的");
|
|
return string.Empty;
|
|
}
|
|
else
|
|
return token.ToString();
|
|
}
|
|
public static string GetJsonValue(this JArray jObject, string jsonName, bool isToLower = true)
|
|
{
|
|
JToken token = jObject[isToLower ? jsonName.ToLower() : jsonName];
|
|
if (token == null)
|
|
return string.Empty;
|
|
else
|
|
return token.ToString();
|
|
}
|
|
public static string GetJsonValue(this string param, string jsonName, bool isToLower = true, bool isThrowExp = false)
|
|
{
|
|
JObject jObject = param.ToJObject(isToLower);
|
|
return GetJsonValue(jObject, jsonName, isToLower, isThrowExp);
|
|
}
|
|
public static DateTime? GetJsonDateTimeValue(this string jObjectstr, string jsonName, bool isToLower = true)
|
|
{
|
|
return jObjectstr.ToJObject(isToLower).GetJsonDateTimeValue(jsonName, isToLower);
|
|
}
|
|
public static DateTime? GetJsonDateTimeValue(this JObject jObject, string jsonName, bool isToLower = true)
|
|
{
|
|
string v = GetJsonValue(jObject, jsonName, isToLower);
|
|
if (DateTime.TryParse(v, out DateTime dateTime))
|
|
{
|
|
return dateTime;
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
public static decimal? GetJsonDecimalValue(this string jObjectstr, string jsonName, bool isToLower = true)
|
|
{
|
|
return jObjectstr.ToJObject(isToLower).GetJsonDecimalValue(jsonName, isToLower);
|
|
}
|
|
public static decimal? GetJsonDecimalValue(this JObject jObject, string jsonName, bool isToLower = true)
|
|
{
|
|
string v = GetJsonValue(jObject, jsonName, isToLower);
|
|
if (decimal.TryParse(v, out decimal d))
|
|
{
|
|
return d;
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
public static double? GetJsonDoubleValue(this string jObjectstr, string jsonName, bool isToLower = true)
|
|
{
|
|
return jObjectstr.ToJObject(isToLower).GetJsonDoubleValue(jsonName, isToLower);
|
|
}
|
|
public static double? GetJsonDoubleValue(this JObject jObject, string jsonName, bool isToLower = true)
|
|
{
|
|
string v = GetJsonValue(jObject, jsonName, isToLower);
|
|
if (double.TryParse(v, out double d))
|
|
{
|
|
return d;
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
public static int? GetJsonIntValue(this string jObjectstr, string jsonName, bool isToLower = true)
|
|
{
|
|
return jObjectstr.ToJObject(isToLower).GetJsonIntValue(jsonName, isToLower);
|
|
}
|
|
public static int? GetJsonIntValue(this JObject jObject, string jsonName, bool isToLower = true, bool isThrowExp = false)
|
|
{
|
|
string v = GetJsonValue(jObject, jsonName, isToLower, isThrowExp);
|
|
if (int.TryParse(v, out int i))
|
|
{
|
|
return i;
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
public static long? GetJsonLongValue(this string jObjectstr, string jsonName, bool isToLower = true, bool isThrowExp = false)
|
|
{
|
|
return jObjectstr.ToJObject(isToLower).GetJsonLongValue(jsonName, isToLower, isThrowExp);
|
|
}
|
|
public static long? GetJsonLongValue(this JObject jObject, string jsonName, bool isToLower = true, bool isThrowExp = false)
|
|
{
|
|
if (jObject == null)
|
|
{
|
|
if (isThrowExp)
|
|
throw Oops.Oh(jsonName + "是必需的");
|
|
return null;
|
|
}
|
|
string v = GetJsonValue(jObject, jsonName, isToLower);
|
|
if (long.TryParse(v, out long d))
|
|
{
|
|
return d;
|
|
}
|
|
else
|
|
{
|
|
if (isThrowExp)
|
|
throw Oops.Oh(jsonName + "是必需的");
|
|
return null;
|
|
}
|
|
}
|
|
public static Guid? GetJsonGuidValue(this string jObjectstr, string jsonName, bool isToLower = true, bool isThrowExp = false)
|
|
{
|
|
return jObjectstr.ToJObject(isToLower).GetJsonGuidValue(jsonName, isToLower, isThrowExp);
|
|
}
|
|
public static Guid? GetJsonGuidValue(this JObject jObject, string jsonName, bool isToLower = true, bool isThrowExp = false)
|
|
{
|
|
string v = GetJsonValue(jObject, jsonName, isToLower);
|
|
if (Guid.TryParse(v, out Guid d))
|
|
{
|
|
return d;
|
|
}
|
|
else
|
|
{
|
|
if (isThrowExp)
|
|
throw Oops.Oh(jsonName + "是必需的");
|
|
return null;
|
|
}
|
|
}
|
|
public static bool? GetJsonBoolValue(this JObject jObject, string jsonName, bool isToLower = true)
|
|
{
|
|
string v = jObject.GetJsonValue(jsonName, isToLower);
|
|
if (bool.TryParse(v, out bool d))
|
|
{
|
|
return d;
|
|
}
|
|
else
|
|
return null;
|
|
}
|
|
public static bool? GetJsonBoolValue(this string jObjectstr, string jsonName, bool isToLower = true)
|
|
{
|
|
return jObjectstr.ToJObject(isToLower).GetJsonBoolValue(jsonName, isToLower);
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="jarrat"></param>
|
|
/// <param name="index"></param>
|
|
/// <returns></returns>
|
|
public static string GetArrayValue(this JArray jarrat, int index)
|
|
{
|
|
JToken token = jarrat[index];
|
|
if (token == null)
|
|
return string.Empty;
|
|
else
|
|
return token.ToString();
|
|
}
|
|
public static JArray GetJsonJArrayValue(this string jObjectstr, string jsonName, bool isToLower = true)
|
|
{
|
|
return jObjectstr.ToJObject(isToLower).GetJsonJArrayValue(jsonName, isToLower);
|
|
}
|
|
public static JArray GetJsonJArrayValue(this JObject jObject, string jsonName, bool isToLower = true)
|
|
{
|
|
return jObject[isToLower ? jsonName.ToLower() : jsonName] as JArray;
|
|
}
|
|
public static JArray GetJsonJArray(this string param, string jsonName, bool isToLower = true)
|
|
{
|
|
var jObject = param.ToJObject(isToLower);
|
|
return jObject[jsonName] as JArray;
|
|
}
|
|
|
|
/// <summary>
|
|
/// json字符串转T
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="page"></param>
|
|
/// <returns></returns>
|
|
public static T ToEntity<T>(this JObject jobj, bool isToLower = true) where T : new()
|
|
{
|
|
var obj = new T();
|
|
PropertyInfo[] ProList = typeof(T).GetProperties();
|
|
foreach (PropertyInfo pro in ProList)
|
|
{
|
|
var propName = pro.PropertyType.Name.ToLower();
|
|
if ("nullable`1" == propName)
|
|
{
|
|
propName = pro.PropertyType.GetGenericArguments()[0].Name.ToLower();
|
|
}
|
|
object propertyValue = pro.GetValue(obj, null);
|
|
bool isnull = false;
|
|
if (propertyValue == null)
|
|
{
|
|
isnull = true;
|
|
}
|
|
else
|
|
{
|
|
switch (propName)
|
|
{
|
|
case "guid":
|
|
isnull = Guid.Parse(propertyValue.ToString()) == Guid.Empty;
|
|
break;
|
|
case "string":
|
|
isnull = string.IsNullOrEmpty(propertyValue.ToString());
|
|
break;
|
|
case "int":
|
|
case "int32":
|
|
case "int64":
|
|
isnull = propertyValue.ToString().ToInt(0) == 0;
|
|
break;
|
|
}
|
|
}
|
|
if (isnull || true)
|
|
{
|
|
object value = null;
|
|
switch (propName)
|
|
{
|
|
case "guid":
|
|
value = jobj.GetJsonGuidValue(pro.Name, isToLower);
|
|
break;
|
|
case "string":
|
|
value = jobj.GetJsonValue(pro.Name, isToLower);
|
|
break;
|
|
case "int":
|
|
case "int32":
|
|
value = jobj.GetJsonIntValue(pro.Name, isToLower);
|
|
break;
|
|
case "int64":
|
|
value = jobj.GetJsonLongValue(pro.Name, isToLower);
|
|
break;
|
|
case "datetime":
|
|
value = jobj.GetJsonDateTimeValue(pro.Name, isToLower);
|
|
break;
|
|
case "decimal":
|
|
value = jobj.GetJsonDecimalValue(pro.Name, isToLower);
|
|
break;
|
|
case "list`1":
|
|
value = jobj.GetJsonJArrayValue(pro.Name, isToLower);
|
|
break;
|
|
case "boolean":
|
|
value = jobj.GetJsonBoolValue(pro.Name, isToLower);
|
|
break;
|
|
default:
|
|
throw Oops.Oh("补充类型:" + propName);
|
|
}
|
|
pro.SetValue(obj, value, null);
|
|
}
|
|
|
|
}
|
|
return obj;
|
|
|
|
|
|
//PropertyInfo[] ProList = typeof(T).GetProperties();
|
|
//foreach (PropertyInfo pro in ProList)
|
|
//{
|
|
// try
|
|
// {
|
|
// if (page.Request.Form.AllKeys.Contains(pro.Name))
|
|
// {
|
|
// string value = page.Request.Form[pro.Name];
|
|
// string pptypeName = pro.PropertyType.Name;
|
|
// if (pptypeName == "Nullable`1")
|
|
// {
|
|
// if (pro.PropertyType.GetGenericArguments()[0] == typeof(Guid))
|
|
// {
|
|
// pro.SetValue(obj, Guid.Parse(value), null);
|
|
// }
|
|
// else
|
|
// {
|
|
// pro.SetValue(obj, Convert.ChangeType(value, pro.PropertyType.GetGenericArguments()[0]), null);
|
|
// }
|
|
|
|
// }
|
|
// else
|
|
// {
|
|
// pro.SetValue(obj, Convert.ChangeType(value, pro.PropertyType), null);
|
|
// }
|
|
// }
|
|
// }
|
|
// catch (Exception ee)
|
|
// {
|
|
|
|
// }
|
|
//}
|
|
//return obj;
|
|
}
|
|
/// <summary>
|
|
/// json字符串转T
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="page"></param>
|
|
/// <returns></returns>
|
|
public static T ToEntity<T>(this string argsparam, bool isToLower = true) where T : new()
|
|
{
|
|
return argsparam.ToJObject(isToLower).ToEntity<T>();
|
|
}
|
|
|
|
|
|
}
|
|
}
|