首页 > 学院 > 开发设计 > 正文

Nancy自定义JsonSerializer

2019-11-14 13:37:57
字体:
来源:转载
供稿:网友

 

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>();
}
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表