I tried using CamelCasePropertyNamesContractResolver, however it does not convert pascal property names into camel casing?
Note: this is an example only, my json input is unknown, I only the json pascal casing.
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
namespace Example
{
class Program
{
static void Main(string[] args)
{
object myJsonInput = @"{'Id':'123','Name':'abc'}"; //Example only, any json.
object myJsonOutput;
var jsonSerializersettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
myJsonOutput = JsonConvert.DeserializeObject<object>(myJsonInput.ToString(),jsonSerializersettings);
//{{"Id": "123","Name": "abc"}}
}
}
}
Camel case and Pascal case are similar. Both demand variables made from compound words and have the first letter of each appended word written with an uppercase letter. The difference is that Pascal case requires the first letter to be uppercase as well, while camel case does not.
Use camel case for all JSON property names The camel case property naming policy: Applies to serialization and deserialization.
Pascal case -- or PascalCase -- is a programming naming convention where the first letter of each compound word in a variable is capitalized. The use of descriptive variable names is a software development best practice. However, modern programming languages do not allow variables names to include blank spaces.
Use the JavaScriptSerializer class to provide serialization and deserialization functionality for AJAX-enabled ASP.NET web applications. The JavaScriptSerializer. Deserialize() method converts the specified JSON string to the type of the specified generic parameter object.
Humanizer has functions such as Pascalize and Camelize. You can use it or just look at their sorce code.
/// <summary>
/// By default, pascalize converts strings to UpperCamelCase also removing underscores
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string Pascalize(this string input)
{
return Regex.Replace(input, "(?:^|_)(.)", match => match.Groups[1].Value.ToUpper());
}
/// <summary>
/// Same as Pascalize except that the first character is lower case
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string Camelize(this string input)
{
var word = Pascalize(input);
return word.Substring(0, 1).ToLower() + word.Substring(1);
}
Results being the following:
"some_title".Pascalize() => "SomeTitle"
"some_title".Camelize() => "someTitle"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With