SingletonBaseTemplate
public static byte[] writeValueAsZipByte(List<CraneDataDtls> dtls) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
GZIPOutputStream outputStream = null;
try {
outputStream = new GZIPOutputStream(byteOutputStream);
mapper.writeValue(outputStream, dtls);
} catch (JsonGenerationException e) {
LOG.error("Error when convert Pojo to Json ", e);
throw new UnhandledException("Server Error, please try again, or contact supportor\r\n");
} catch (JsonMappingException e) {
LOG.error("Error when convert Pojo to Json ", e);
throw new UnhandledException("Server Error, please try again, or contact supportor\r\n");
} catch (IOException e) {
LOG.error("Error when convert Pojo to Json ", e);
throw new UnhandledException("Server Error, please try again, or contact supportor\r\n");
}
byte[] arr = byteOutputStream.toByteArray();
return arr;
}
Java 上述代码:
JSON 数据转换 :
代码:
//{
// "Name": "Apple",
// "Expiry": "2008-12-28T00:00:00",
// "Sizes": [
// "Small"
// ]
//}
static void Main(string[] args)
{
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(, , );
product.Sizes = new string[] { "Small" ,"Large"};
string json = JsonConvert.SerializeObject(product);
Debug.WriteLine(json);
Console.ReadKey(); Product productClone = JsonConvert.DeserializeObject<Product>(json);
Debug.WriteLine(productClone.Name);
Debug.WriteLine(productClone.Expiry);
for (int i = ; i < productClone.Sizes.Length; i++)
{
Debug.Write(productClone.Sizes[i] + " ");
}
Console.ReadKey(); }
https://json.codeplex.com/downloads/get/832296
http://www.cnblogs.com/piaopiao7891/p/3624779.html
public class Customer
{
public string CustomerName;
public int Unid;
}
public void CallCustomerToJson()
{
Customer cc = new Customer { Unid = , CustomerName = "John" };
string strJson = CustomerToJsonSerialize(cc); Console.WriteLine(strJson);
} public void CallJsonToCustomer()
{
string strJson = "{\"CustomerName\":\"John\",\"Unid\":1}";
Customer c = JsonToCustomerDeserialize(strJson) as Customer; Console.WriteLine(c.Unid + " " + c.CustomerName);
} public void GenericCallCustomerToJson()
{
Customer cc = new Customer { Unid = , CustomerName = "John" };
string strJsons = GenericToJson<Customer>(cc);
Console.WriteLine(strJsons);
} public void GenericCallJsonToCustomer()
{
string strJson = "{\"CustomerName\":\"John\",\"Unid\":1}";
Customer c = GenericFromJson<Customer>(strJson);
Console.WriteLine(c.Unid + " " + c.CustomerName);
} #region Json /// <summary>
/// Json Serialize
/// </summary>
/// <param name="customer"></param>
/// <returns></returns>
public string CustomerToJsonSerialize(Customer customer)
{
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(Customer)); MemoryStream ms = new MemoryStream();
ds.WriteObject(ms, customer); string strReturn = Encoding.UTF8.GetString(ms.ToArray());
ms.Close(); return strReturn;
} /// <summary>
/// Json DeSerialize
/// </summary>
/// <param name="strJson"></param>
/// <returns></returns>
public object JsonToCustomerDeserialize(string strJson)
{
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(Customer));
MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(strJson)); return ds.ReadObject(ms);
} /// <summary>
/// GenericToJson
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string GenericToJson<T>(T t)
{
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
ds.WriteObject(ms, t); string strReturn = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return strReturn;
} /// <summary>
/// GenericFromJson
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="strJson"></param>
/// <returns></returns>
public static T GenericFromJson<T>(string strJson) where T : class
{
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson)); return ds.ReadObject(ms) as T;
} #endregion
[DataContract]//必须申明,否则无法序列化
public class Person
{
[DataMember(Name = "PersonName")]//必须申明,否则无法序列化
private string Name; [DataMember(Name = "PersonSex")]//必须申明,否则无法序列化
private bool Sex; public Person(string name, bool sex)
{
this.Name = name;
this.Sex = sex;
} public override string ToString()
{
return "姓名:" + this.Name + "\t性别:" + (this.Sex ? "男" : "女");
} } [DataContract]
public class Programmer : Person
{
[DataMember(Name = "ProgramLanguage")]
private List<string> Languages; public Programmer(string name, bool sex, List<string> languages)
: base(name,sex)
{
this.Languages = languages;
} public override string ToString()
{
StringBuilder str = new StringBuilder();
str.Append(base.ToString() + "\t编程语言:");
foreach (string lang in this.Languages)
{
str.Append(lang + " ");
} return str.ToString();
} } public void DataContractSampleCode()
{
string filePath = @"D:\ObjectJson.txt"; List<string> languages = null;
languages = new List<string>(new string[] { "C#", "Java" });
languages = new List<string>(new string[] { "C#", "C++" });
languages = new List<string>(new string[] { "C#", "C++", "C", "Java" }); List<Programmer> list = new List<Programmer>();
list.Add(new Programmer("Coder1", true, languages));
list.Add(new Programmer("Coder2", false, languages));
list.Add(new Programmer("Coder3", true, languages)); //序列化对象
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Programmer>)); MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, list);//将对象转换成json存储
string jsonStr = Encoding.Default.GetString(ms.ToArray());
File.WriteAllText(filePath, jsonStr);
ms.Position = ;
ms.Dispose();//关闭内存流 //反序列化对象
list.Clear(); using (FileStream fStream = File.Open(filePath,FileMode.OpenOrCreate))
{
list = (List<Programmer>)ser.ReadObject(fStream);
foreach (Programmer p in list)
{
Console.WriteLine(p);
}
} }
http://www.cnblogs.com/junbird-nest/archive/2012/03/23/2413441.html
http://www.cnblogs.com/phoenixtrees/archive/2011/05/01/2033847.html
SingletonBaseTemplate的更多相关文章
随机推荐
- oracle员工表和部门表基本操作
emp 员工表(empno 员工号/ename 员工姓名/job 工作/mgr 上级编号/hiredate 受雇日期/sal 薪金/comm 佣金/deptno 部门编号) dept 部门表(dept ...
- mysql 序列号生成器 (自定义函数)
https://yq.aliyun.com/articles/42600 http://bbs.csdn.net/topics/360203885 http://www.tuicool.com/art ...
- 小强的HTML5移动开发之路(49)——HTML5开发神器HBuilder
今天给大家介绍一款开发HTML5的神器--HBuilder. 下载地址:http://www.dcloud.net.cn/ 一.新建文件 可以看到支持web app开发和普通网站前端开发,我们首先建立 ...
- tipc
TIPC SOCKET实现分析 http://ju.outofmemory.cn/entry/158241
- hdfs的基本原理和基本操作总结
hdfs基本原理 Hadoop分布式文件系统(HDFS)被设计成适合执行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有非常多共同点. 但同一时候,它和 ...
- 并发编程--CAS自旋锁
在前两篇博客中我们介绍了并发编程--volatile应用与原理和并发编程--synchronized的实现原理(二),接下来我们介绍一下CAS自旋锁相关的知识. 一.自旋锁提出的背景 由于在多处理器系 ...
- Browser security standards via access control
A computing system is operable to contain a security module within an operating system. This securit ...
- kindeditor 4 指定生成文件的时间日期/动态获取My97的时间
最近后台要求要指定上传附件的时间日期,编辑器是kindeditor,仔细研究后发现可以借助clickToolbar事件,传递时间到后台,但是中途发现传递的时间总是初始化my97的时间,改变后的时间只有 ...
- zookeeper 半数可用/选举机制
1.半数可用机制,半数可用指的是zk集群中一半以上的机器正常时集群才能正常工作 已经启动了hadoop002(follower),hadoop003(leader) 下面停止hadoop002 在ha ...
- 分布式服务网关(Web Api)
分布式服务网关(Web Api) kmp算法又称“看毛片”算法,是一个效率非常高的字符串匹配算法.不过由于其难以理解,所以在很长的一段时间内一直没有搞懂.虽然网上有很多资料,但是鲜见好的博客能简单明了 ...