ASP.NET Core 3.0 System.Text.Json Camel Case Serialization
AddJsonOptions()
would config System.Text.Json
only for MVC. If you want to use JsonSerializer
in your own code you should pass the config to it.
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
var json = "{\"firstname\":\"John\",\"lastname\":\"Smith\"}";
var person = JsonSerializer.Parse<Person>(json, options);
How to serialize object in ASP.NET Core/6 using a camelCase?
You can use the object: JsonSerializerOptions. You have to pass it as a parameter after your object like this:
JsonSerializerOptions options = new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
System.Text.Json.JsonSerializer.Serialize(data, options);
How do I serialize object properties to lower case using System.Text.Json?
If you really are looking for all lowercase property names and not camel-case property names e.g. FullName
becomes fullname
and not fullName
, there's nothing built-in - you will have to create your own JsonNamingPolicy
like so:
LowerCaseNamingPolicy.cs
public class LowerCaseNamingPolicy : JsonNamingPolicy
{
public override string ConvertName(string name)
{
if (string.IsNullOrEmpty(name) || !char.IsUpper(name[0]))
return name;
return name.ToLower();
}
}
Usage:
var options = new JsonSerializerOptions {
PropertyNamingPolicy = new LowerCaseNamingPolicy(),
};
var json = JsonSerializer.Serialize(estoOst, options);
However, I think you're looking for camel-case naming, considering you mentioned Json.NET which also doesn't have a lowercase naming policy.
If so, set the JsonSerializerOptions.PropertyNamingPolicy
property to JsonNamingPolicy.CamelCase
to serialise property names into a camel-case format:
var options = new JsonSerializerOptions {
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
var json = JsonSerializer.Serialize(estoOst, options);
Here's a working demo to output & show the difference of output for both methods:
public class Program
{
public static void Main()
{
var person = new Person
{
Age = 100,
FullName = "Lorem Ipsum"
};
var camelCaseJsonSerializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
var lowercaseJsonSerializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = new LowerCaseNamingPolicy()};
var camelCaseJson = JsonSerializer.Serialize(person, camelCaseJsonSerializerOptions);
var lowercaseJson = JsonSerializer.Serialize(person, lowercaseJsonSerializerOptions);
Console.WriteLine(camelCaseJson);
Console.WriteLine(lowercaseJson);
}
}
public class Person
{
public string FullName { get; set; }
public int Age { get; set; }
}
public class LowerCaseNamingPolicy : JsonNamingPolicy
{
public override string ConvertName(string name)
{
if (string.IsNullOrEmpty(name) || !char.IsUpper(name[0]))
return name;
return name.ToLower();
}
}
Output:
{"fullName":"Lorem Ipsum","age":100}
{"fullname":"Lorem Ipsum","age":100}
System.Text.Json does not camelize properties of the dynamic property
ExpandoObject
will be treated as dictionary, so you need to set DictionaryKeyPolicy
in addition to PropertyNamingPolicy
:
dynamic relatedItems = new ExpandoObject();
relatedItems.LastName = "Last";
var result = new
{
FirstName = "First",
RelatedItems = relatedItems
};
var s=JsonSerializer.Serialize(result, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
});
Console.WriteLine(s); // prints {"firstName":"First","relatedItems":{"lastName":"Last"}}
How to turn off or handle camelCasing in JSON response ASP.NET Core?
In ASP.NET Core <3.0, JSON properties are camelCased by default (per this announcement).
You can disable this by replacing
services.AddMvc();
with
services
.AddMvc()
.AddJsonOptions(opt => opt.SerializerSettings.ContractResolver
= new DefaultContractResolver());
in your Startup.cs file. You'll have to add using Newtonsoft.Json.Serialization;
to the top of the file.
With the DefaultContractResolver
in place, the property names will be represented verbatim in the JSON output. No need for DataMember
attributes.
Related Topics
Getting All Controllers and Actions Names in C#
Get Value of C# Dynamic Property via String
What Are Best Practices for Using Smtpclient, Sendasync and Dispose Under .Net 4.0
Get Thumbnail Image of Video File in C#
Is There a Way of Setting a Property Once Only in C#
C# - Wcf - Inter-Process Communication
ASP.NET Webapi2 Enable Cors Not Working with Aspnet.Webapi.Cors 5.2.3
Method Overloading VS Optional Parameter in C# 4.0
How to Create an Expression Tree to Represent 'String.Contains("Term")' in C#
Can Bindings Create Memory Leaks in Wpf
C# Entity-Framework: How to Combine a .Find and .Include on a Model Object
Cannot Convert Lambda Expression to Type 'System.Delegate'
How to Show the Sum of in a Datagridview Column
How to Detect a Process Start & End Using C# in Windows
Why Does Stylecop Recommend Prefixing Method or Property Calls with "This"