-Initial naming strategy implementation

This commit is contained in:
James Newton-King
2016-05-28 22:58:05 +12:00
parent d453089b58
commit 02c6a1e247
7 changed files with 114 additions and 16 deletions
+15 -2
View File
@@ -59,9 +59,9 @@ namespace Newtonsoft.Json
public Type ItemConverterType { get; set; }
/// <summary>
/// The parameter list to use when constructing the JsonConverter described by ItemConverterType.
/// The parameter list to use when constructing the <see cref="JsonConverter"/> described by ItemConverterType.
/// If null, the default constructor is used.
/// When non-null, there must be a constructor defined in the JsonConverter that exactly matches the number,
/// When non-null, there must be a constructor defined in the <see cref="JsonConverter"/> that exactly matches the number,
/// order, and type of these parameters.
/// </summary>
/// <example>
@@ -69,6 +69,10 @@ namespace Newtonsoft.Json
/// </example>
public object[] ItemConverterParameters { get; set; }
/// <summary>
/// Gets the <see cref="Type"/> of the <see cref="INamingStrategy"/>.
/// </summary>
/// <value>The <see cref="Type"/> of the <see cref="INamingStrategy"/>.</value>
public Type NamingStrategyType
{
get { return _namingStrategyType; }
@@ -79,6 +83,15 @@ namespace Newtonsoft.Json
}
}
/// <summary>
/// The parameter list to use when constructing the <see cref="INamingStrategy"/> described by NamingStrategyType.
/// If null, the default constructor is used.
/// When non-null, there must be a constructor defined in the <see cref="INamingStrategy"/> that exactly matches the number,
/// order, and type of these parameters.
/// </summary>
/// <example>
/// [JsonContainer(NamingStrategyType = typeof(MyNamingStrategy), NamingStrategyParameters = new object[] { 123, "Four" })]
/// </example>
public object[] NamingStrategyParameters
{
get { return _namingStrategyParameters; }
@@ -38,16 +38,16 @@ namespace Newtonsoft.Json
private readonly Type _converterType;
/// <summary>
/// Gets the <see cref="Type"/> of the converter.
/// Gets the <see cref="Type"/> of the <see cref="JsonConverter"/>.
/// </summary>
/// <value>The <see cref="Type"/> of the converter.</value>
/// <value>The <see cref="Type"/> of the <see cref="JsonConverter"/>.</value>
public Type ConverterType
{
get { return _converterType; }
}
/// <summary>
/// The parameter list to use when constructing the JsonConverter described by ConverterType.
/// The parameter list to use when constructing the <see cref="JsonConverter"/> described by ConverterType.
/// If null, the default constructor is used.
/// </summary>
public object[] ConverterParameters { get; private set; }
@@ -55,7 +55,7 @@ namespace Newtonsoft.Json
/// <summary>
/// Initializes a new instance of the <see cref="JsonConverterAttribute"/> class.
/// </summary>
/// <param name="converterType">Type of the converter.</param>
/// <param name="converterType">Type of the <see cref="JsonConverter"/>.</param>
public JsonConverterAttribute(Type converterType)
{
if (converterType == null)
@@ -69,8 +69,8 @@ namespace Newtonsoft.Json
/// <summary>
/// Initializes a new instance of the <see cref="JsonConverterAttribute"/> class.
/// </summary>
/// <param name="converterType">Type of the converter.</param>
/// <param name="converterParameters">Parameter list to use when constructing the JsonConverter. Can be null.</param>
/// <param name="converterType">Type of the <see cref="JsonConverter"/>.</param>
/// <param name="converterParameters">Parameter list to use when constructing the <see cref="JsonConverter"/>. Can be null.</param>
public JsonConverterAttribute(Type converterType, params object[] converterParameters)
: this(converterType)
{
+19 -4
View File
@@ -24,6 +24,7 @@
#endregion
using System;
using Newtonsoft.Json.Serialization;
namespace Newtonsoft.Json
{
@@ -48,15 +49,15 @@ namespace Newtonsoft.Json
internal TypeNameHandling? _itemTypeNameHandling;
/// <summary>
/// Gets or sets the converter used when serializing the property's collection items.
/// Gets or sets the <see cref="JsonConverter"/> used when serializing the property's collection items.
/// </summary>
/// <value>The collection's items converter.</value>
/// <value>The collection's items <see cref="JsonConverter"/>.</value>
public Type ItemConverterType { get; set; }
/// <summary>
/// The parameter list to use when constructing the JsonConverter described by ItemConverterType.
/// The parameter list to use when constructing the <see cref="JsonConverter"/> described by ItemConverterType.
/// If null, the default constructor is used.
/// When non-null, there must be a constructor defined in the JsonConverter that exactly matches the number,
/// When non-null, there must be a constructor defined in the <see cref="JsonConverter"/> that exactly matches the number,
/// order, and type of these parameters.
/// </summary>
/// <example>
@@ -64,7 +65,21 @@ namespace Newtonsoft.Json
/// </example>
public object[] ItemConverterParameters { get; set; }
/// <summary>
/// Gets the <see cref="Type"/> of the <see cref="INamingStrategy"/>.
/// </summary>
/// <value>The <see cref="Type"/> of the <see cref="INamingStrategy"/>.</value>
public Type NamingStrategyType { get; set; }
/// <summary>
/// The parameter list to use when constructing the <see cref="INamingStrategy"/> described by NamingStrategyType.
/// If null, the default constructor is used.
/// When non-null, there must be a constructor defined in the <see cref="INamingStrategy"/> that exactly matches the number,
/// order, and type of these parameters.
/// </summary>
/// <example>
/// [JsonProperty(NamingStrategyType = typeof(MyNamingStrategy), NamingStrategyParameters = new object[] { 123, "Four" })]
/// </example>
public object[] NamingStrategyParameters { get; set; }
/// <summary>
@@ -2,21 +2,53 @@
namespace Newtonsoft.Json.Serialization
{
/// <summary>
/// A camel case naming strategy.
/// </summary>
public class CamelCaseNamingStrategy : INamingStrategy
{
/// <summary>
/// A flag indicating whether dictionary keys should be camel cased.
/// Defaults to <c>false</c>.
/// </summary>
public bool CamelCaseDictionaryKeys { get; set; }
/// <summary>
/// A flag indicating whether explicitly specified property names,
/// e.g. a property name customized with a <see cref="JsonPropertyAttribute"/>, should be camel cased.
/// Defaults to <c>false</c>.
/// </summary>
public bool OverrideSpecifiedNames { get; set; }
public CamelCaseNamingStrategy()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CamelCaseNamingStrategy"/> class.
/// </summary>
/// <param name="camelCaseDictionaryKeys">
/// A flag indicating whether dictionary keys should be camel cased.
/// </param>
/// <param name="overrideSpecifiedNames">
/// A flag indicating whether explicitly specified property names,
/// e.g. a property name customized with a <see cref="JsonPropertyAttribute"/>, should be camel cased.
/// </param>
public CamelCaseNamingStrategy(bool camelCaseDictionaryKeys, bool overrideSpecifiedNames)
{
CamelCaseDictionaryKeys = camelCaseDictionaryKeys;
OverrideSpecifiedNames = overrideSpecifiedNames;
}
/// <summary>
/// Initializes a new instance of the <see cref="CamelCaseNamingStrategy"/> class.
/// </summary>
public CamelCaseNamingStrategy()
{
}
/// <summary>
/// Gets the serialized name for a given property name.
/// </summary>
/// <param name="s">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 string GetPropertyName(string s, bool hasSpecifiedName)
{
if (hasSpecifiedName && !OverrideSpecifiedNames)
@@ -27,6 +59,11 @@ namespace Newtonsoft.Json.Serialization
return StringUtils.ToCamelCase(s);
}
/// <summary>
/// Gets the serialized key for a given dictionary key.
/// </summary>
/// <param name="s">The initial dictionary key.</param>
/// <returns>A dictionary key.</returns>
public string GetDictionaryKey(string s)
{
if (!CamelCaseDictionaryKeys)
@@ -184,6 +184,10 @@ namespace Newtonsoft.Json.Serialization
public bool IgnoreSerializableAttribute { get; set; }
#endif
/// <summary>
/// Gets or sets the naming strategy used to resolve how property names and dictionary keys are serialized.
/// </summary>
/// <value>The naming strategy used to resolve how property names and dictionary keys are serialized.</value>
public INamingStrategy NamingStrategy { get; set; }
/// <summary>
@@ -1,12 +1,26 @@
namespace Newtonsoft.Json.Serialization
{
/// <summary>
/// The default naming strategy. Property names and dictionary keys are unchanged.
/// </summary>
public class DefaultNamingStrategy : INamingStrategy
{
/// <summary>
/// Gets the serialized name for a given property name.
/// </summary>
/// <param name="s">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 string GetPropertyName(string s, bool hasSpecifiedName)
{
return s;
}
/// <summary>
/// Gets the serialized key for a given dictionary key.
/// </summary>
/// <param name="s">The initial dictionary key.</param>
/// <returns>A dictionary key.</returns>
public string GetDictionaryKey(string s)
{
return s;
@@ -1,8 +1,23 @@
namespace Newtonsoft.Json.Serialization
{
/// <summary>
/// An interface for resolving how property names and dictionary keys are serialized.
/// </summary>
public interface INamingStrategy
{
/// <summary>
/// Gets the serialized name for a given property name.
/// </summary>
/// <param name="s">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>
string GetPropertyName(string s, bool hasSpecifiedName);
/// <summary>
/// Gets the serialized key for a given dictionary key.
/// </summary>
/// <param name="s">The initial dictionary key.</param>
/// <returns>A dictionary key.</returns>
string GetDictionaryKey(string s);
}
}