How to disable Nancy JSON to automatically convert C # class code for <string, T> dictionary?
How can I turn off Nancy's automatic conversion between camel and Pascal body during serialization and deserialization from JSON for objects representing dictionaries in C # and JavaScript?
In my case, the keys of these dictionaries are identifiers that should not be changed by automatic code conversion.
In addition, these dictionaries themselves are the values of other object property names / keys.
Here is an example JavaScript object where I want automatic case conversion for an object (from .customersbefore .customersand .addressesto .addresses), but not for the -value sub-objects' identifier ( ID33100a00, abc433D123etc.):
{
customers: {
ID33100a00: 'Percy',
abc433D123: 'Nancy'
},
addresses: {
abc12kkhID: 'Somewhere over the rainbow',
JGHBj45nkc: 'Programmer\ hell',
jaf44vJJcn: 'Desert'
}
}
These dictionary objects are all represented Dictionary<string, T>in C #, for example:
Dictionary<string, Customer> Customers;
Dictionary<string, Address> Addresses;
Unfortunately installation
JsonSettings.RetainCasing = true;
will not automatically convert cases at all.
I also tried to solve the problem by writing my own JavaScriptConverter
as described in the Nancy documentation , but the actual serialization / deserialization to / from strings for object keys happens somewhere else (since the converter does not process JSON strings directly, but IDictionary<string, object>).
I read about the related "error" (or behavior) here .
Newtonsoft.Json . , , JsonSerializer, :
public sealed class CustomJsonSerializer : JsonSerializer
{
public CustomJsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver();
}
}
:
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
base.ConfigureApplicationContainer(container);
container.Register<JsonSerializer,CustomJsonSerializer>().AsSingleton();
}
, , :
public CustomJsonSerializer()
{
Converters.Add(new StringEnumConverter());
ContractResolver = new CamelCasePropertyNamesContractResolver();
}