Newtonsoft.json是一款不错的序列化反序列化第三方组件,具体如何使用属于基础知识,此处不再讲解.看以下代码:
public class OutgameEntity
{
public string orderid{get;set;}
public DateTime? payTime { get; set; }
}.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
很简单的一个实体类
假如此时有一个url:http://www.xxx.xx/do.svc/xxx?data=,我们如果要把OutgameEntity实体类的值传过去,就需要反序列化对象为string字符串.
public static string JsonSerialize(T o)
{
string str = JsonConvert.SerializeObject(o, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
return str;
}
new WebClient().DownloadString(url,JsonSerialize(OutgameEntity));
此段代码看似没有任何问题.不过当你接收时,你会发现一点点问题.
"payTime":"\/Date(13884569650000800)\/".
如果这个参数值没有任何意义,那么我们可以忽略这个问题.但是作为程序员,特别是和别的公司进行对接的接口会出现这样的参数值,会给同行造成此人是菜鸟的心理.所以我们应该想办法解决这个不大不小的问题.
如何解决呢?
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
请看以下这个接口:
using Newtonsoft.Json.Schema;
using System; namespace Newtonsoft.Json
{
public abstract class JsonConverter
{
protected JsonConverter(); public virtual bool CanRead { get; }
public virtual bool CanWrite { get; } public abstract bool CanConvert(Type objectType);
public virtual JsonSchema GetSchema();
public abstract object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer);
public abstract void WriteJson(JsonWriter writer, object value, JsonSerializer serializer);
}
}
using Newtonsoft.Json;
using System; namespace Newtonsoft.Json.Converters
{
public abstract class DateTimeConverterBase : JsonConverter
{
protected DateTimeConverterBase(); public override bool CanConvert(Type objectType);
}
}
已经有了DatetimeConverterBse这个父类,那么要实现这个功能就比较好办.
我们去重写实现JsonConverter中的ReadJson和WriteJson方法,如下:
public class UnixDateTimeConverter : DateTimeConverterBase
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType != JsonToken.Integer)
{
throw new Exception(String.Format("日期格式错误,got {0}.", reader.TokenType));
}
var ticks = (long)reader.Value;
var date = new DateTime(1970, 1, 1);
date = date.AddSeconds(ticks);
return date;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
long ticks;
if (value is DateTime)
{
var epoc = new DateTime(1970, 1, 1);
var delta = ((DateTime)value) - epoc;
if (delta.TotalSeconds < 0)
{
throw new ArgumentOutOfRangeException("时间格式错误.1");
}
ticks = (long)delta.TotalSeconds;
}
else
{
throw new Exception("时间格式错误.2");
}
writer.WriteValue(ticks);
}
}
请注意JsonConverter类中有一个方法叫CanConvert(Type t),那由此我们是否可以用这样的方式来实现呢?如代码所示:
public class ModelEntity
{
public string value { get; set; } [JsonConverter(typeof(UnixDateTimeConverter))]
public DateTime createtime { get; set; }
}
OK,debug走起,
private void button1_Click(object sender, EventArgs e)
{
var model = new ModelEntity();
model.birthdate = DateTime.Now;
model.value = "Find a Way";
var str = Common.SerializerHelper<ModelEntity>.JsonSerialize(model);
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
那我修改实体,如下:
再去反序列化呢?
时间戳,高大尚,妈妈再也不用担心我写代码.