码迷,mamicode.com
首页 > Web开发 > 详细

Nancy 自定义JsonSerializer

时间:2015-12-31 22:54:20      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

 

1、创建自己的jsonserializer类

public class CustomJsonNetSerializer : JsonSerializer, ISerializer
{
public CustomJsonNetSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver();
DateFormatHandling = DateFormatHandling.IsoDateFormat;
Formatting = Formatting.None;
}

public bool CanSerialize(string contentType)
{
return contentType.Equals("application/json", StringComparison.OrdinalIgnoreCase);
}

public void Serialize<TModel>(string contentType, TModel model, Stream outputStream)
{
using (var streamWriter = new StreamWriter(outputStream))
using (var jsonWriter = new JsonTextWriter(streamWriter))
{
Serialize(jsonWriter, model);
}
}

public IEnumerable<string> Extensions { get { yield return "json"; } }
}

2、重写DefaultNancyBootstrapper类

public class Bootstrapper : DefaultNancyBootstrapper
{

protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
base.ConfigureApplicationContainer(container);

container.Register<ISerializer, CustomJsonNetSerializer>();
}
}

Nancy 自定义JsonSerializer

标签:

原文地址:http://www.cnblogs.com/wangchaozhi/p/5092730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!