基线 SVN r240
SVN-Revision: r240
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lskj.Push.Util
|
||||
{
|
||||
class Base64
|
||||
{
|
||||
public static String getBase64Encode(String str)
|
||||
{
|
||||
byte[] bytes = Encoding.Default.GetBytes(str);
|
||||
//
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Runtime.Serialization.Json;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Script.Serialization;
|
||||
using System.Diagnostics;
|
||||
using Lskj.Push.Report;
|
||||
|
||||
namespace Lskj.Push.Util
|
||||
{
|
||||
public class JsonTool
|
||||
{
|
||||
// 从一个对象信息生成Json串
|
||||
public static string ObjectToJson(object obj)
|
||||
{
|
||||
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
|
||||
MemoryStream stream = new MemoryStream();
|
||||
serializer.WriteObject(stream, obj);
|
||||
byte[] dataBytes = new byte[stream.Length];
|
||||
stream.Position = 0;
|
||||
stream.Read(dataBytes, 0, (int)stream.Length);
|
||||
return Encoding.UTF8.GetString(dataBytes).Replace("\\","");
|
||||
}
|
||||
|
||||
// 从一个Json串生成对象信息
|
||||
public static object JsonToObject(string jsonString, object obj)
|
||||
{
|
||||
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
|
||||
MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
|
||||
return serializer.ReadObject(mStream);
|
||||
}
|
||||
|
||||
// 从一个对象信息生成Json串
|
||||
public static string DictionaryToJson(Dictionary<String, Object> dict)
|
||||
{
|
||||
StringBuilder json = new StringBuilder();
|
||||
|
||||
foreach (KeyValuePair<String, Object> pair in dict)
|
||||
{
|
||||
json.Append("\"").Append(pair.Key).Append("\"").Append(":").Append(ValueToJson(pair.Value)).Append(",");
|
||||
}
|
||||
//Console.WriteLine("json String ******"+json);
|
||||
if (json.Length > 0)
|
||||
{
|
||||
json.Remove(json.Length -1, 1);
|
||||
}
|
||||
json.Append("}");
|
||||
json.Insert(0, "{");
|
||||
|
||||
return json.ToString();
|
||||
}
|
||||
|
||||
public static List<ReceivedResult.Received> JsonList(string jsonString)
|
||||
{
|
||||
JavaScriptSerializer Serializer = new JavaScriptSerializer();
|
||||
List<ReceivedResult.Received> jsonclassList = Serializer.Deserialize<List<ReceivedResult.Received>>(jsonString);
|
||||
return jsonclassList;
|
||||
}
|
||||
//从dictionary 的value中解析出字符串
|
||||
private static string ValueToJson(object value)
|
||||
{
|
||||
Type type = value.GetType();
|
||||
if(type==typeof(int)){
|
||||
|
||||
return value.ToString();
|
||||
}else if(type==typeof(string)){
|
||||
|
||||
return "\""+value+"\"";
|
||||
|
||||
}else if(type==typeof(List<int>)||type==typeof(List<string>)){
|
||||
|
||||
return ObjectToJson(value);
|
||||
|
||||
}else if(type==typeof(Dictionary<string,object>)){
|
||||
|
||||
return JsonTool.DictionaryToJson((Dictionary<string, object>)value);
|
||||
}
|
||||
else{
|
||||
Debug.WriteLine("Type in Dictionary is error!");
|
||||
return "type erro";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Lskj.Push.Util
|
||||
{
|
||||
class Md5
|
||||
{
|
||||
public static String getMD5Hash(String str)
|
||||
{
|
||||
// MD5 md5 = new MD5CryptoServiceProvider();
|
||||
//byte[] res = md5.ComputeHash(Encoding.Default.GetBytes(str), 0, str.Length);
|
||||
//char[] temp = new char[res.Length];
|
||||
//System.Array.Copy(res, temp, res.Length);
|
||||
//return new String(temp);
|
||||
|
||||
|
||||
// 创建MD5类的默认实例:MD5CryptoServiceProvider
|
||||
|
||||
|
||||
MD5 md5 = MD5.Create();
|
||||
|
||||
|
||||
byte[] bs = Encoding.UTF8.GetBytes(str);
|
||||
|
||||
|
||||
byte[] hs = md5.ComputeHash(bs);
|
||||
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
|
||||
foreach (byte b in hs)
|
||||
{
|
||||
|
||||
|
||||
// 以十六进制格式格式化
|
||||
|
||||
|
||||
sb.Append(b.ToString("x2"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return sb.ToString();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lskj.Push.Util
|
||||
{
|
||||
class Preconditions
|
||||
{
|
||||
public static void checkArgument(bool expression)
|
||||
{
|
||||
if (!expression)
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
}
|
||||
public static void checkArgument(bool expression, object errorMessage)
|
||||
{
|
||||
if (!expression)
|
||||
{
|
||||
throw new ArgumentException(errorMessage.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lskj.Push.Util
|
||||
{
|
||||
class StringUtil
|
||||
{
|
||||
|
||||
public bool IsNumber(String strNumber)
|
||||
{
|
||||
Regex objNotNumberPattern=new Regex("[^0-9.-]");
|
||||
Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
|
||||
Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
|
||||
String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
|
||||
String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
|
||||
Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
|
||||
|
||||
return !objNotNumberPattern.IsMatch(strNumber) &&
|
||||
!objTwoDotPattern.IsMatch(strNumber) &&
|
||||
!objTwoMinusPattern.IsMatch(strNumber) &&
|
||||
objNumberPattern.IsMatch(strNumber);
|
||||
}
|
||||
|
||||
public static bool IsNumeric(string value)
|
||||
{
|
||||
return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
|
||||
}
|
||||
public static bool IsInt(string value)
|
||||
{
|
||||
return Regex.IsMatch(value, @"^[+-]?\d*$");
|
||||
}
|
||||
public static bool IsUnsign(string value)
|
||||
{
|
||||
return Regex.IsMatch(value, @"^\d*[.]?\d*$");
|
||||
}
|
||||
public static String arrayToString(String[] values)
|
||||
{
|
||||
if (null == values) return "";
|
||||
|
||||
StringBuilder buffer = new StringBuilder(values.Length);
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
buffer.Append(values[i]).Append(",");
|
||||
}
|
||||
if (buffer.Length > 0)
|
||||
{
|
||||
return buffer.ToString().Substring(0, buffer.Length - 1);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user