In this article, we’re going to learn how to serialize a PascalCase JObject to a camelCase format. We will do this in two ways by using the most familiar JSON library in the .NET ecosystem which is Newtonsoft.Json.
Let’s begin.
Overview of JObject Serialization in .NET
Serializing JSON objects is a common practice in .NET applications.
When we serialize a JObject, it ignores the usual contract resolvers, such as the CamelCasePropertyNamesContractResolver
, which are used to transform property names during serialization.
JObject represents an already parsed JSON structure, and modifying it with a contract resolver would require additional processing that JObject is not designed to handle directly.
Instead, transformations like converting property names to camelCase need to be done explicitly on JObject instances. We will demonstrate this while using ExpandoObject
to dynamically handle property names.
Setting up Newtonsoft.Json
Let’s set up our application and explore different techniques to serialize a PascalCase Json .NET JObject to camelCase. We will use a console application and create a library to handle this.
Firstly, let’s install Newtonsoft.Json
library for serialization. We can use the NuGet Package Manager in Visual Studio or the .NET CLI with the dotnet add package Newtonsoft.Json
command.
Next, let’s create our objects.
Let’s start by creating the House
object which will contain a property that will be dynamic:
public class House { public int NoOfRooms { get; set; } public decimal SizeInSqft { get; set; } public JObject OtherDetails { get; set; } }
Here, we add NoOfRooms
, SizeInSqft
and OtherDetails
to our House
object which is a dynamic property that can hold any JSON data.
Also, we need to make available the measurements of our new house to the builder. Therefore, let’s create a Measurement
class:
public class Measurement { public int Width { get; set; } public int Length { get; set; } }
Here, we add the Width
and Length
properties to the Measurement
object for the builder to calculate the area of the house.
Next, let’s create a static method for the conversion:
public class FormatJson { public static JObject ConvertToCamel(JObject jsonObject) { var settings = JsonSerializer.Create(new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); return JObject.FromObject(jsonObject.ToObject<ExpandoObject>(), settings); } }
Here, we add a static method, ConvertToCamel()
in the FormatJson
class. This method changes the format to camelCase and returns the result to the invoker.
In order to do this, we pass our jsonObject
and settings
variable to JObject.FromObject()
. Also, we use ExpandoObject
because its dynamic nature allows us to handle a list of properties that we don’t know in advance. This allows us to control how we create our JObject.
Serializing Using JsonSerializerSettings
Finally, let’s implement our Program
class. We start by creating an instance of the House
object:
var houseInput = new House { SizeInSqft = 1000, NoOfRooms = 2, OtherDetails = JObject.FromObject(new Measurement { Width = 50, Length = 100 }) };
Here, we add the strongly-typed object Measurement
and assign it to OtherDetails
property, which is a dynamic property that can store any type of data as a JObject.
Next, let’s serialize our object in our Program
class:
var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; var result = JsonConvert.SerializeObject(houseInput, settings); Console.WriteLine("Width and Length property in pascal case using JsonConvert DefaultSettings: {0}", result);
We create a settings
variable and assign the CamelCasePropertyNamesContractResolver
. We then pass our houseInput
instance as the first variable and the settings
variable to the JsonConvert.SerializeObject()
method.
Let’s see our result from the console output:
Width and Length property in pascal case using JsonConvert DefaultSettings: { "noOfRooms": 2, "sizeInSqft": 1000, "otherDetails": { "Width": 50, "Length": 100 } }
We see our result clearly in camelCase. But, we have an issue. As we observe the end of our JSON result, Width
and Length
property is still in the Pascal format.
Our builders will not like this inconsistency, therefore, let’s go ahead to fix this.
We have to modify our Program
class:
var convertedUnit = FormatJson.ConvertToCamel(JObject.FromObject(houseInput)); result = JsonConvert.SerializeObject(convertedUnit); Console.WriteLine("Json converted to camel case using CamelCasePropertyNamesContractResolver: {0}", result);
We invoke our method ConvertToCamel
from the FormatJson
library and then serialize the result.
Let’s see our result once more:
Json converted to camel case using CamelCasePropertyNamesContractResolver: { "noOfRooms": 2, "sizeInSqft": 1000, "otherDetails": { "width": 50, "length": 100 } }
We got our expected output for our builders to progress with their work.
Generally, this method is the best. The main advantage of this method is that it tackles the problem directly without affecting your entire application.
Using JsonConvert Global Serialization Settings
Global serialization settings is an alternative and one of the oldest methods, as it has been here for a while. It is easy to set up and requires very little code, although, it is generally not advisable to use because it affects all serialization project-wide.
We should use this approach only when we want the format to be camelCase all through our application.
Let’s first configure JsonConvert.DefaultSettings
:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
We assign the CamelCasePropertyNamesContractResolver()
object to the ContractResolver
property.
It is best if we set this in the Program
class. Since we are using a console application, let’s set this at the beginning of our Program
class.
Furthermore, we serialize our instance:
var result = JsonConvert.SerializeObject(houseInput); Console.WriteLine("Json converted to camel case using JsonConvert DefaultSettings: {0}", result);
Let’s see our desired outcome:
Json converted to camel case using JsonConvert DefaultSettings: { "noOfRooms": 2, "sizeInSqft": 1000, "otherDetails": { "width": 50, "length": 100 } }
We achieved the expectations our builders wanted.
Conclusion
To serialize a PascalCase JObject to camelCase using Newtonsoft.Json’s JsonSerializerSettings, we utilized the CamelCasePropertyNamesContractResolver in our JsonSerializerSettings. We ensured that our JSON properties were converted to camelCase, satisfying the requirements for our real estate company’s data transmission. We used a custom method that directly addresses the problem without affecting the entire application, unlike the global Newtonsoft.Json settings, which alter serialization behavior throughout the project.