Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JObject & CamelCase conversion with JSON.Net

Tags:

c#

json.net

How can I convert a generic JObject to camelCase plain json string? I've tried with JsonSerializerSettings but doesn't work (Newtonsoft.Json 4.5.11)

[Test] public void should_convert_to_camel_case() {     var serializer = JsonSerializer.Create(new JsonSerializerSettings()     {         ContractResolver = new CamelCasePropertyNamesContractResolver()     });      var jo = new JObject();     jo["CamelCase"] = 1;      var stringWriter = new StringWriter();     var writer = new JsonTextWriter(stringWriter);     serializer.Serialize(writer,jo);      var serialized = stringWriter.ToString();      Assert.AreEqual("{\"camelCase\":1}", serialized); } 

UPDATE According to http://json.codeplex.com/workitem/23853 that cannot be done (tnx to @nick_w for the link)

like image 876
Andrea Balducci Avatar asked Feb 26 '13 10:02

Andrea Balducci


People also ask

What is a JObject?

JObject. It represents a JSON Object. It helps to parse JSON data and apply querying (LINQ) to filter out required data. It is presented in Newtonsoft.

What is JObject and JToken?

The JToken hierarchy looks like this: JToken - abstract base class JContainer - abstract base class of JTokens that can contain other JTokens JArray - represents a JSON array (contains an ordered list of JTokens) JObject - represents a JSON object (contains a collection of JProperties) JProperty - represents a JSON ...

How do you value a JObject?

The simplest way to get a value from LINQ to JSON is to use the Item[Object] index on JObject/JArray and then cast the returned JValue to the type you want. JObject/JArray can also be queried using LINQ.

What does JObject parse do?

The method JObject. Parse() is a JObject class method. This parse method is used to parse a JSON string into a C# object. It parses the data of string based on its key value.


2 Answers

This question starts from a JObject and wants to work to a camel-cased JSON object. If you are actually starting from an object and want to get to a JObject that is already camelcased, then you can do this:

var serializer = new JsonSerializer() {     ContractResolver = new CamelCasePropertyNamesContractResolver() }; var jo = JObject.FromObject(someDataContract, serializer); 

The resulting 'jo' will be camelcased.

like image 97
Steve Benz Avatar answered Oct 13 '22 19:10

Steve Benz


According to this Json.NET issue, when serializing a JObject this way the contract resolver is ignored:

When serializing a JObject the contract resolvers seems to be ignored. Surely this is not how it is supposed to be?
Closed Jan 30, 2013 at 8:50 AM by JamesNK
That does make sense but it is too big a breaking change I'm afraid.

Inspired by the workaround on that page, you could do something like this:

var jo = new JObject(); jo["CamelCase"] = 1;  string json = JsonConvert.SerializeObject(jo); var jObject = JsonConvert.DeserializeObject<ExpandoObject>(json);  var settings = new JsonSerializerSettings() {     ContractResolver = new CamelCasePropertyNamesContractResolver() };  var serialized = JsonConvert.SerializeObject(jObject, settings);  Assert.AreEqual("{\"camelCase\":1}", serialized); 

Edit:

Good point about the Dictionary<string, object>. So doing it this way skips the additional JsonConvert.SerializeObject, but it also mitigates the need for the ExpandoObject, which is important if you are using .NET 3.5.

Dictionary<string, object> jo = new Dictionary<string, object>(); jo.Add("CamelCase", 1);  var settings = new JsonSerializerSettings() {     ContractResolver = new CamelCasePropertyNamesContractResolver() };  var serialized = JsonConvert.SerializeObject(jo, settings);  Assert.AreEqual("{\"camelCase\":1}", serialized); 
like image 41
nick_w Avatar answered Oct 13 '22 17:10

nick_w