Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you handle differing naming conventions when serializing C# objects to JSON?

I am using ASP.Net to serialize classes designed in C# to JSON. My Javascript application then requests those objects with AJAX. I have done this on a couple of projects now, but I run into the problem that the C# standard naming conventions use PascalCase for the public members, and in Javascript they are generally camelCase.

This particularly becomes an issue when I have some Javascript-only classes that use camelCase and some mixed-use classes that use PascalCase since I designed them in C# originally.

How does everyone else deal with this sort of problem? Do you just pick one or the other? If so which one is more broadly chosen? Or is there perhaps a clever way to user a JSON serializer to switch between the two naming conventions?

like image 512
Code Commander Avatar asked Dec 12 '11 23:12

Code Commander


People also ask

What is JsonProperty annotation C#?

JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.

Should JSON keys be camelCase?

The JSON syntax does not impose any restrictions on the strings used as names,... There is no standard naming of keys in JSON and that camelCase or snake_case should work fine.

How do you serialize and deserialize an object in C# using JSON?

It returns JSON data in string format. In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it calls the static method DeserializeObject() of the JsonConvert class by passing JSON data.

What is serialize and deserialize in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).


2 Answers

You could use JSON.net to serialize the data for you, and you can even tell it to use camelCase. This question asks something similar. Here's a code example for reference:

Product product = new Product {
    ExpiryDate = new DateTime(2010, 12, 20, 18, 1, 0, DateTimeKind.Utc), Name = "Widget", Price = 9.99m, Sizes = new[] {
        "Small", "Medium", "Large"
    }
};

string json =
JsonConvert.SerializeObject(
    product, 
    Formatting.Indented, 
    new JsonSerializerSettings {
    ContractResolver = new CamelCasePropertyNamesContractResolver()
});

Don't worry about the performance of JSON.net either, as the performance of it versus native serialization is comparable (better in most cases).

like image 65
JesseBuesking Avatar answered Oct 22 '22 18:10

JesseBuesking


If you are using the DataContractJsonSerializer, you can specify the name using the DataMemberAttribute.Name property:

[DataContract]
public class User
{
    [DataMember(Name = "user_id")]
    public int UserId { get; set; }

    [DataMember(Name = "user_name")]
    public string UserName { get; set; }
}

will serialize to

{"user_id":123,"user_name":"John Doe"}
like image 10
Jeff Ogata Avatar answered Oct 22 '22 17:10

Jeff Ogata