1

Say i have a JRaw property where i place a Pascal cased JSON into.

public class C
{
   public JRaw Prop {get;set;}
}

var a = new JRaw("{\"A\":42}");
var c = new C { Prop = a };

Now when i serialize this i can force c to be lowercase, like so:

var result = JsonConvert.SerializeObject(c, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
// i have "{\"c\":{\"A\":42}}"

However, is there any way to make JRaw also camelcased? e.g. i want to get

// "{\"c\":{\"a\":42}}"
zaitsman
  • 8,984
  • 6
  • 47
  • 79
  • See [JObject & CamelCase conversion with JSON.Net](https://stackoverflow.com/a/15106674/3744182) which states that, according to Newtonsoft, contract resolvers are ignored when reading and writing a `JObject`. Some workarounds are also given. – dbc Apr 17 '18 at 03:21

1 Answers1

1

Here's a way to do it although it may not be the most efficient as it requires you to serialize your object, deserialize, then serialize it again. I believe the problem is the serializer is not detecting your "A" JSON property properly since it's a raw JSON string as opposed to an object property. This will convert your JRaw JSON string to a dynamic object (ExpandoObject) allowing the "A" JSON property to become an object property temporarily. This'll then be picked up by the serializer once the object is serialized, resulting in all nested property keys being camel case.

using System;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Dynamic;

public class Program
{
    public class C
    {
        public JRaw Prop { get; set; }
    }

    public static void Main()
    {

        var a = new JRaw("{\"A\":42}");
        var c = new C { Prop = a };

        var camelCaseSettings = new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };

        var result = JsonConvert.SerializeObject(c, camelCaseSettings);

        var interimObject = JsonConvert.DeserializeObject<ExpandoObject>(result);
        result = JsonConvert.SerializeObject(interimObject, camelCaseSettings);

        Console.WriteLine(result);
        Console.ReadKey();
    }
}

Output: {"prop":{"a":42}}

Jake Miller
  • 2,432
  • 2
  • 24
  • 39
  • Yeah, this seems to work but you're right -> it is double serialization, and essentially instead of doing that i can `camelCase` at the source when saving this to the db. The whole point of using `JRaw` really was to avoid the need to reserialize when returning pre-formatted JSON to the API consumers – zaitsman Apr 17 '18 at 02:27
  • There's probably an external library to convert your serialized JSON to camel case to avoid having to serialize twice but it's not part of Newtonsoft.Json. I'd go for that route if you're unable to find a better solution. – Jake Miller Apr 17 '18 at 02:34
  • @zaitsman if you are doing that for perfomance (to avoid reserialization) - then store already camelCased json in database in the first place. – Evk Apr 17 '18 at 07:24