I have the following installed:
<package id="Nancy" version="1.4.2" targetFramework="net4" />
<package id="Nancy.Owin" version="1.4.1" targetFramework="net4" />
<package id="Nancy.Serialization.JsonNet" version="1.4.1" targetFramework="net4" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net4" />
<package id="Owin" version="1.0" targetFramework="net4" />
And also:
public sealed class Startup
{
public void Configuration(IAppBuilder app)
{
var options = new NancyOptions();
app.UseNancy(options);
}
}
public sealed class CustomJsonNetSerializer : JsonSerializer
{
public CustomJsonNetSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver();
DateFormatHandling = DateFormatHandling.IsoDateFormat;
Formatting = Formatting.Indented;
}
}
Then in my Bootstrapper:
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
base.ConfigureApplicationContainer(container);
container.Register<JsonSerializer, CustomJsonNetSerializer>();
}
And finally in the route:
public sealed class ApiBase : NancyModule
{
public ApiBase() : base("api/")
{
Get["user/"] = o => Response.AsJson(new { Context.CurrentUser, Time = DateTime.UtcNow});
}
}
However when running this app, the CustomJsonNetSerializer is never instantiated, it looks like Nancy is using a different implementation possibly the ISerializer.
Please note I want to know why this solution is failing as I am following the official doc rather than doing other implementation i.e implementing an ISerializer.
Any idea?
[Update] I could not figure out what was wrong so I ended up with:
public sealed 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"; } }
}
And:
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
base.ConfigureApplicationContainer(container);
container.Register<ISerializer, CustomJsonNetSerializer>();
}