6

I am serializing an object of this format using Newtonsoft.JSON:

class Message
{
    public HeaderType Header { get; set; }
    public object Body { get; set; }
}

I want to turn the Header and Body properties into camel case, while presevering the case of the properties of the thing assigned to Body.

So if the message looks like:

var result = new Message() { Header = myHeader, Body = new SomeClass() { A = 1 }});

I want the output to look like:

{ header = myHeader, body = { A = 1 } } // I realize this isn't valid C#

Right now, I'm doing this to get the camel case conversion, but of course it's affecting everything.

string stringRepresentationOfObj = JsonConvert.SerializeObject(obj, new JsonSerializerSettings {
    ContractResolver = new DefaultContractResolver {
        NamingStrategy = new CamelCaseNamingStrategy()
    }
});

Is there a way to ignore certain parts of the object? I see that the docs call out OverrideSpecifiedNames, ProcessDictionaryKeys, and ProcessExtensionDataNames, but it doesn't look like that's what I want.

Am I forced to use a some kind of custom naming strategy? How can I achieve this?

1

2 Answers 2

9

You can configure a CamelCaseNamingStrategy to not camel case properties that already have a name specified with an attribute, Check documentation here

Specify property name as below

[JsonProperty(PropertyName = "Name")]
public string Name{ get; set; }

And in CamelCaseNamingStrategy set OverrideSpecifiedNames = false

string stringRepresentationOfObj = JsonConvert.SerializeObject(obj, new JsonSerializerSettings
{
    ContractResolver = new DefaultContractResolver
    {
        NamingStrategy = new CamelCaseNamingStrategy()
        {
            OverrideSpecifiedNames = false
        }
    }
});

Another way is to modify your type only using JsonObject attribute as below, this will force your type properties only to be in camelCase and any nested properties will not be affected.

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class ApiMessage
{
    public HeaderType Header { get; set; }

    public object Body { get; set; }
}

Also, add JsonObject attribute to HeaderType class

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class HeaderType
{
    public string MyPropertyA { get; set; }

    public string MyPropertyB { get; set; }
}

Your result should be as below

{
  "header": {
    "myPropertyA": "AAA",
    "myPropertyB": "BBB"
  },
  "body": {
    "ObjectPropertyA": "Value A",
    "ObjectPropertyB": "Value B",
    "ObjectPropertyC": "Value C"
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

OverrideSpecifiedNames is by default set to false. Second, I was looking for a solution where you don't have to attribute every property with JsonProperty. Imagine that the value of Body comes from god knows where, and I don't have access to it. I was looking for a more general solution where I could make every property inside Body not turn into camel case.
Check this JSON.NET custom contract resolvers library Wildcards section
From a glance, it doesn't look like that's what I want. It excludes all props of a class. In my case, I have a property named Body, and I want to exclude all of its properties.
It looks like that attribute is applied to the whole class. I want everything to be camelcased except properties in Body. You changed Header to a string, but in my example it's not. It's an object, and I still want to camel case its properties.
This answer saved me and it is apparently rare, took way too long to find it after searching for NamingStrategy exclusions for an hour
|
1

You can create your own resolver to behave this way.

You'd create one, possibly have it look for a new attribute (which you'd create), that you can then use to decorate the properties you don't want camelCased.

1 Comment

I don't want any property on Body to be camel cased. I'd rather not force everyone to attribute their properties if I can help it. An example of this would help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.