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)
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.
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 ...
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With