When serializing object to JSON using Newtonsoft it’s possible to change the naming of the properties by updating the serializer naming conventions as detailed here.
It’s also possible to create your own custom naming strategy if none of the inbuild ones do the dop. This can be done by inheriting from the NamingStrategy
base class.
In my case I need to create a naming strategy for Pascal case which is detailed below. This is based on the code for the offical CamelCaseNamingStrategy.
/// <summary>
/// Uses Pascal case naming strategy, first letter is capitalized, and first letter
/// of subsequent words are capitalized. This is the recommended naming strategy
/// for .NET classes, interfaces, properties, method names, etc.
/// Example: SomePropertyName
/// </summary>
public class PascalCaseNamingStrategy : NamingStrategy
{
/// <summary>
/// Initializes a new instance of the <see cref="PascalCaseNamingStrategy"/> class.
/// </summary>
/// <param name="processDictionaryKeys">
/// A flag indicating whether dictionary keys should be processed.
/// </param>
/// <param name="overrideSpecifiedNames">
/// A flag indicating whether explicitly specified property names should be processed,
/// e.g. a property name customized with a <see cref="JsonPropertyAttribute"/>.
/// </param>
public PascalCaseNamingStrategy(bool processDictionaryKeys, bool overrideSpecifiedNames)
{
ProcessDictionaryKeys = processDictionaryKeys;
OverrideSpecifiedNames = overrideSpecifiedNames;
}
/// <summary>
/// Initializes a new instance of the <see cref="PascalCaseNamingStrategy"/> class.
/// </summary>
/// <param name="processDictionaryKeys">
/// A flag indicating whether dictionary keys should be processed.
/// </param>
/// <param name="overrideSpecifiedNames">
/// A flag indicating whether explicitly specified property names should be processed,
/// e.g. a property name customized with a <see cref="JsonPropertyAttribute"/>.
/// </param>
/// <param name="processExtensionDataNames">
/// A flag indicating whether extension data names should be processed.
/// </param>
public PascalCaseNamingStrategy(bool processDictionaryKeys, bool overrideSpecifiedNames, bool processExtensionDataNames)
: this(processDictionaryKeys, overrideSpecifiedNames)
{
ProcessExtensionDataNames = processExtensionDataNames;
}
/// <summary>
/// Initializes a new instance of the <see cref="PascalCaseNamingStrategy"/> class.
/// </summary>
public PascalCaseNamingStrategy()
{
}
/// <summary>
/// Resolves the specified property name.
/// </summary>
/// <param name="name">The property name to resolve.</param>
/// <returns>The resolved property name.</returns>
protected override string ResolvePropertyName(string name)
{
return ToPascalCase(name);
}
private static string ToPascalCase(string s)
{
if (string.IsNullOrEmpty(s) || !char.IsUpper(s[0]))
{
return s;
}
char[] chars = s.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (i == 0 && !char.IsUpper(chars[i]))
{
chars[i] = ToUpper(chars[i]);
break;
}
else if (i == 0)
{
break;
}
if (i == 1 && !char.IsUpper(chars[i]))
{
break;
}
bool hasNext = (i + 1 < chars.Length);
if (i > 0 && hasNext && !char.IsUpper(chars[i + 1]))
{
if (char.IsSeparator(chars[i + 1]))
{
chars[i] = ToLower(chars[i]);
}
break;
}
chars[i] = ToLower(chars[i]);
}
return new string(chars);
}
private static char ToLower(char c)
{
#if HAVE_CHAR_TO_LOWER_WITH_CULTURE
c = char.ToLower(c, CultureInfo.InvariantCulture);
#else
c = char.ToLowerInvariant(c);
#endif
return c;
}
private static char ToUpper(char c)
{
#if HAVE_CHAR_TO_UPPER_WITH_CULTURE
c = char.ToUpper(c, CultureInfo.InvariantCulture);
#else
c = char.ToUpperInvariant(c);
#endif
return c;
}
}
I then needed to pass the serialized JSON string as the body in a RestShap request, this can be done like so.
_restClient = new RestClient(_options.DataCollectorUrl);
JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
{
ContractResolver = new DefaultContractResolver()
{
NamingStrategy = new PascalCaseNamingStrategy()
}
};
_restClient.UseNewtonsoftJson(jsonSerializerSettings);
0 Comments