Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert any pascal case JSON object to camel case JSON object?

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"}}
        }


    }
}
like image 331
001 Avatar asked Mar 24 '17 07:03

001


People also ask

What is the difference between camel case and Pascal case?

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.

Should JSON be CamelCase?

Use camel case for all JSON property names The camel case property naming policy: Applies to serialization and deserialization.

What is PascalCase format?

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.

How will you transform a JSON file to C# objects?

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.


1 Answers

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"
like image 137
Andrey Alonzov Avatar answered Sep 19 '22 12:09

Andrey Alonzov