-Refactored naming strategy to be an abstract base class

This commit is contained in:
James Newton-King
2016-05-28 23:56:58 +12:00
parent 02c6a1e247
commit 52fbb613d7
26 changed files with 594 additions and 114 deletions
@@ -0,0 +1,59 @@
namespace Newtonsoft.Json.Serialization
{
/// <summary>
/// A base class for resolving how property names and dictionary keys are serialized.
/// </summary>
public abstract class NamingStrategy
{
/// <summary>
/// A flag indicating whether dictionary keys should be processed.
/// Defaults to <c>false</c>.
/// </summary>
public bool ProcessDictionaryKeys { get; set; }
/// <summary>
/// A flag indicating whether explicitly specified property names,
/// e.g. a property name customized with a <see cref="JsonPropertyAttribute"/>, should be processed.
/// Defaults to <c>false</c>.
/// </summary>
public bool OverrideSpecifiedNames { get; set; }
/// <summary>
/// Gets the serialized name for a given property name.
/// </summary>
/// <param name="name">The initial property name.</param>
/// <param name="hasSpecifiedName">A flag indicating whether the property has had a name explicitly specfied.</param>
/// <returns>A property name.</returns>
public virtual string GetPropertyName(string name, bool hasSpecifiedName)
{
if (hasSpecifiedName && !OverrideSpecifiedNames)
{
return name;
}
return ResolvePropertyName(name);
}
/// <summary>
/// Gets the serialized key for a given dictionary key.
/// </summary>
/// <param name="key">The initial dictionary key.</param>
/// <returns>A dictionary key.</returns>
public virtual string GetDictionaryKey(string key)
{
if (!ProcessDictionaryKeys)
{
return key;
}
return ResolvePropertyName(key);
}
/// <summary>
/// Resolves the specified property name.
/// </summary>
/// <param name="name">The property name to resolve.</param>
/// <returns>The resolved property name.</returns>
protected abstract string ResolvePropertyName(string name);
}
}