I ended up writing a custom JSON serializer wrapping the excellent JSON.NET library. This solution raises exceptions on invalid JSON and so returns 400 Bad Request as expected.
Caveat: This implementation ignores the Accept-Charset header as well as the charset parameter of the Content-Type header, and instead assumes UTF8. If you're not able to assume UTF8 on the wire, you'll want to tweak this code.
public class UseJsonDotNet : IPlugin
{
public JsonSerializerSettings Settings { get; set; }
public void Register(IAppHost appHost)
{
appHost.ContentTypes.Register(
"application/json",
WriteObjectToStream,
ReadObjectFromStream);
}
public void WriteObjectToStream(
IRequest request, object response, Stream target)
{
var s = JsonConvert.SerializeObject(response, Formatting.None, Settings);
using (var writer = new StreamWriter(target, Encoding.UTF8, 1024, true))
{
writer.Write(s);
}
}
public object ReadObjectFromStream(Type type, Stream source)
{
using (var reader = new StreamReader(source, Encoding.UTF8))
{
var s = reader.ReadToEnd();
var o = JsonConvert.DeserializeObject(s, type, Settings);
return o;
}
}
}
To use it, just register it:
Plugins.Add(new UseJsonDotNet { Settings = ... } );