namespace asepnetcorewebapi.utils {
public class ResultInfo<T>
{
public const int FLAG_NULL = 0;
public static readonly int FLAG_FROM_PARENT = 1;
public const int FLAG_HAS_VALUE = 2;
public int resultflag { get; set; }
public T returnvalue { get; set; }
public static ResultInfo<T> createNull(T v)
{
ResultInfo<T> result = new ResultInfo<T>();
result.resultflag = FLAG_NULL;
result.returnvalue = v;
return result;
}
public static void xx()
{
}
public static ResultInfo<T> createExpandParent(T v)
{
ResultInfo<T> result = new ResultInfo<T>();
result.resultflag = FLAG_FROM_PARENT;
result.returnvalue = v;
return result;
}
}
class JSONUtil
{
public static int getIntValueX(JObject objectarg, string key)
{
return getIntValueX(objectarg, key, 0);
}
internal static bool isJSONObject(string value)
{
if (value == null || value.Length < 2 ){
return false;
}
if (!value.StartsWith("{")||!value.EndsWith("}"))
{
return false;
}
return true;
}
public static int getIntValueX(JObject objectarg, string key, int defaultValue)
{
return getIntValue(objectarg, key, defaultValue).returnvalue;
}
public static ResultInfo<int> getIntValue(JObject objectarg, string key, int defaultValue=-1)
{
if (!jsonKeyIsExist(objectarg, key))
{
ResultInfo<int> info = new ResultInfo<int>();
info.resultflag = ResultInfo<int>.FLAG_FROM_PARENT;
info.returnvalue = defaultValue;
return info;
}
if (jsonValueIsNull(objectarg, key))
{
ResultInfo<int> info = new ResultInfo<int>();
info.resultflag = ResultInfo<int>.FLAG_NULL;
info.returnvalue = defaultValue;
return info;
}
try
{
JToken value = objectarg.GetValue(key);
ResultInfo<int> info = new ResultInfo<int>();
info.resultflag = ResultInfo<int>.FLAG_HAS_VALUE;
info.returnvalue = Int32.Parse(value.ToString());
return info;
}
catch (Exception e)
{
//e.ToString();
ResultInfo<int> info = new ResultInfo<int>();
info.resultflag = ResultInfo<int>.FLAG_NULL;
info.returnvalue = defaultValue;
return info;
}
}
public static bool getBooleanValueX(JObject objectarg, string key, bool defaultValue)
{
return getBooleanValue(objectarg, key, defaultValue).returnvalue;
}
public static ResultInfo<bool> getBooleanValue(JObject objectarg, string key, bool defaultValue)
{
if (!jsonKeyIsExist(objectarg, key))
{
ResultInfo<bool> info = new ResultInfo<bool>();
info.resultflag = ResultInfo<bool>.FLAG_FROM_PARENT;
info.returnvalue = defaultValue;
return info;
}
if (jsonValueIsNull(objectarg, key))
{
ResultInfo<bool> info = new ResultInfo<bool>();
info.resultflag = ResultInfo<bool>.FLAG_NULL;
info.returnvalue = defaultValue;
return info;
}
try
{
JToken value = objectarg.GetValue(key);
ResultInfo<bool> info = new ResultInfo<bool>();
info.resultflag = ResultInfo<bool>.FLAG_HAS_VALUE;
info.returnvalue = bool.Parse(value.ToString());
return info;
}
catch (Exception e)
{
//e.ToString();
ResultInfo<bool> info = new ResultInfo<bool>();
info.resultflag = ResultInfo<bool>.FLAG_NULL;
info.returnvalue = defaultValue;
return info;
}
}
public static string getStringValueX(JObject objectarg, string key, string value="")
{
return getStringValue(objectarg, key, value).returnvalue;
}
public static ResultInfo<string> getStringValue(JObject objectarg, string key, string defaultValue="")
{
if (!jsonKeyIsExist(objectarg, key))
{
ResultInfo<string> info = new ResultInfo<string>();
info.resultflag = ResultInfo<string>.FLAG_FROM_PARENT;
info.returnvalue = defaultValue;
return info;
}
if (jsonValueIsNull(objectarg, key))
{
ResultInfo<string> info = new ResultInfo<string>();
info.resultflag = ResultInfo<string>.FLAG_NULL;
info.returnvalue = defaultValue;
return info;
}
try
{
JToken value = objectarg.GetValue(key);
ResultInfo<string> info = new ResultInfo<string>();
info.resultflag = ResultInfo<string>.FLAG_HAS_VALUE;
info.returnvalue = value.ToString();
return info;
}
catch (Exception e)
{
//e.ToString();
ResultInfo<string> info = new ResultInfo<string>();
info.resultflag = ResultInfo<string>.FLAG_NULL;
info.returnvalue = defaultValue;
return info;
}
}
public static JObject getJSONObjectValueX(JObject objectarg, string key, JObject defaultValue)
{
return getJSONObjectValue(objectarg, key, defaultValue).returnvalue;
}
public static ResultInfo<JObject> getJSONObjectValue(JObject objectarg, string key, JObject defaultValue)
{
if (!jsonKeyIsExist(objectarg, key))
{
ResultInfo<JObject> info = new ResultInfo<JObject>();
info.resultflag = ResultInfo<JObject>.FLAG_FROM_PARENT;
info.returnvalue = defaultValue;
return info;
}
if (jsonValueIsNull(objectarg, key))
{
ResultInfo<JObject> info = new ResultInfo<JObject>();
info.resultflag = ResultInfo<JObject>.FLAG_NULL;
info.returnvalue = defaultValue;
return info;
}
try
{
JToken value = objectarg.GetValue(key);
ResultInfo<JObject> info = new ResultInfo<JObject>();
info.resultflag = ResultInfo<JObject>.FLAG_HAS_VALUE;
info.returnvalue = value as JObject;
return info;
}
catch (Exception e)
{
//e.ToString();
ResultInfo<JObject> info = new ResultInfo<JObject>();
info.resultflag = ResultInfo<JObject>.FLAG_NULL;
info.returnvalue = defaultValue;
return info;
}
}
public static ResultInfo<JArray> getJSONArrayValue(JObject objectarg, string key, JArray defaultValue)
{
if (!jsonKeyIsExist(objectarg, key))
{
ResultInfo<JArray> info = new ResultInfo<JArray>();
info.resultflag = ResultInfo<JArray>.FLAG_FROM_PARENT;
info.returnvalue = defaultValue;
return info;
}
if (jsonValueIsNull(objectarg, key))
{
ResultInfo<JArray> info = new ResultInfo<JArray>();
info.resultflag = ResultInfo<JObject>.FLAG_NULL;
info.returnvalue = defaultValue;
return info;
}
try
{
JToken value = objectarg.GetValue(key);
ResultInfo<JArray> info = new ResultInfo<JArray>();
info.resultflag = ResultInfo<JArray>.FLAG_HAS_VALUE;
info.returnvalue = value as JArray;
return info;
}
catch (Exception e)
{
//e.ToString();
ResultInfo<JArray> info = new ResultInfo<JArray>();
info.resultflag = ResultInfo<JArray>.FLAG_NULL;
info.returnvalue = defaultValue;
return info;
}
}
public static float getFloatValue(JObject objectarg, string key)
{
ResultInfo<float> result = getFloatValue(objectarg, key, 0);
if (result.resultflag == ResultInfo<float>.FLAG_HAS_VALUE)
{
}
return result.returnvalue;
}
public static bool jsonKeyIsExist(JObject objectarg, string key)
{
if (key == "reuse")
{
}
return objectarg.GetValue(key) != null;
}
public static bool jsonValueIsNull(JObject objectarg, string key)
{
JProperty jobjecttemp = objectarg.Property(key);
if (jobjecttemp == null)
{
return true;
}
JToken obj = jobjecttemp.Value;
if (obj.Type == JTokenType.Null)
{
return true;
}
else
{
return false;
}
//return objectarg.Property(key) == null;//只能判断key是否存在
}
public static JTokenType getJSONValueType(JValue vlaue)
{
return vlaue.Type;
}
public static ResultInfo<float> getFloatValue(JObject objectarg, string key, float defaultValue)
{
//bool keyisexist = jsonobj2.Property("name") == null || jsonobj2.Property("name").ToString() == "";
if (!jsonKeyIsExist(objectarg, key))
{
return ResultInfo<float>.createExpandParent(defaultValue);
}
if (jsonValueIsNull(objectarg, key))
{
return ResultInfo<float>.createNull(defaultValue);
}
if (jsonValueIsErrorType(objectarg, key))
{
throw new FormatException("错误的类型" + objectarg.Type);
}
object obj = objectarg.GetValue(key);//hasValues
if (obj == null)
{
return ResultInfo<float>.createNull(defaultValue);
}
else
{
try
{
ResultInfo<float> info = new ResultInfo<float>();
info.resultflag = ResultInfo<int>.FLAG_HAS_VALUE;
info.returnvalue = float.Parse(obj.ToString());
return info;
}
catch (Exception e)
{
//e.ToString();
ResultInfo<float> info = new ResultInfo<float>();
info.resultflag = ResultInfo<float>.FLAG_NULL;
info.returnvalue = float.Parse(obj.ToString());
return info;
}
}
}
private static bool jsonValueIsErrorType(JObject objectarg, string key)
{
if (objectarg.Type == JTokenType.Undefined)
{
return true;// syntactic error .
}
else
{
return false;
}
//throw new NotImplementedException();
}
private static bool jsonValueIsAspectType(JObject objectarg, string key, JTokenType aspectType)
{
if (objectarg.Type == aspectType)
{
return true;
}
else
{
return false;
}
}
}
}
本文由博客一文多发平台 OpenWrite 发布!
|