.net providing lowercase json keys

Hi everyone, there is an easy way to use json in .Net to ensure that keys are sent as lowercase.

I am currently using the newtonsoft.json library and just using

string loginRequest = JsonConvert.SerializeObject(auth);

In this case auth is just the next object

 public class Authority { public string Username { get; set; } public string ApiToken { get; set; } } 

The result is

 {"Username":"Mark","ApiToken":"xyzABC1234"} 

Is there anyway that the username and apitoken keys pass as lowercase letters?

I don't want to just run it through string.tolower (), of course, because the values ​​for the username and apitoken are a mixed case.

I understand that I can program this and create a json string manually, but I need it in about twenty lines of json data and see if I can save some time. Think about whether there are already created libraries that allow you to enter lowercase letters to create keys.

+75
Jun 09 2018-11-11T00:
source share
4 answers

To do this, you can create your own custom resolver. The following contract converter converts all keys to lowercase:

 public class LowercaseContractResolver : DefaultContractResolver { protected override string ResolvePropertyName(string propertyName) { return propertyName.ToLower(); } } 

Using:

 var settings = new JsonSerializerSettings(); settings.ContractResolver = new LowercaseContractResolver(); var json = JsonConvert.SerializeObject(authority, Formatting.Indented, settings); 

Wil result:

 {"username":"Mark","apitoken":"xyzABC1234"} 



If you always want to serialize using LowercaseContractResolver , consider moving it to a class to avoid repetition:

 public class LowercaseJsonSerializer { private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings { ContractResolver = new LowercaseContractResolver() }; public static string SerializeObject(object o) { return JsonConvert.SerializeObject(o, Formatting.Indented, Settings); } public class LowercaseContractResolver : DefaultContractResolver { protected override string ResolvePropertyName(string propertyName) { return propertyName.ToLower(); } } } 

What can be used as follows:

 var json = LowercaseJsonSerializer.SerializeObject(new { Foo = "bar" }); // { "foo": "bar" } 



ASP.NET MVC4 / WebAPI

If you use ASP.NET MVC4 / WebAPI, you can use CamelCasePropertyNamesContractResolver from the Newtonsoft.Json library, which is enabled by default.

+131
Jun 09 2018-11-11T00:
source share
 protected void Application_Start() { JsonConfig.Configure(); } public static class JsonConfig { public static void Configure(){ var formatters = GlobalConfiguration.Configuration.Formatters; var jsonFormatter = formatters.JsonFormatter; var settings = jsonFormatter.SerializerSettings; settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); } } 
+16
Dec 10 '15 at 1:04
source share

In Json.NET 9.0.1 and later, you can ensure that all property names are converted to lowercase using custom NamingStrategy . This class extracts the logic of the algorithmic reassignment of property names from the contract recognizer to a separate, lightweight object that can be set to DefaultContractResolver.NamingStrategy . This avoids the need to create custom ContractResolver and, therefore, it can be easier to integrate into structures that already have their own contract solvers.

Define LowercaseNamingStrategy as follows:

 public class LowercaseNamingStrategy : NamingStrategy { protected override string ResolvePropertyName(string name) { return name.ToLowerInvariant(); } } 

Then serialize as follows:

 var settings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = new LowercaseNamingStrategy() }, }; string loginRequest = JsonConvert.SerializeObject(auth, settings); 

Notes -

  • Using string.ToLowerInvariant() ensures that the same contract is generated in all locales.

  • To manage overridden property names, dictionary names, and extension data names are lower, you can set NamingStrategy.OverrideSpecifiedNames , NamingStrategy.ProcessDictionaryKeys or NamingStrategy.ProcessExtensionDataNames (Json.NET 10.0.1 and later) to true .

  • You may want to cache the contract recognizer for the best performance .

  • If you do not have access to the serializer parameters in your structure, you can apply NamingStrategy directly to your object as follows:

     [JsonObject(NamingStrategyType = typeof(LowercaseNamingStrategy))] public class Authority { public string Username { get; set; } public string ApiToken { get; set; } } 
  • Do not modify NamingStrategy CamelCasePropertyNamesContractResolver . This contract resolver shares type information around the world in all of its instances, so changing any instance can have unexpected side effects.

+2
Dec 15 '17 at 20:20
source share

you can use "JsonProperty":

Using:

 public class Authority { [JsonProperty("userName")] // or [JsonProperty("username")] public string Username { get; set; } [JsonProperty("apiToken")] // or [JsonProperty("apitoken")] public string ApiToken { get; set; } } var json = JsonConvert.SerializeObject(authority); 
+1
Nov 24 '17 at 0:18
source share



All Articles