You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.9 KiB

  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. namespace Newtonsoft.Json.Serialization
  26. {
  27. /// <summary>
  28. /// A base class for resolving how property names and dictionary keys are serialized.
  29. /// </summary>
  30. internal abstract class NamingStrategy
  31. {
  32. /// <summary>
  33. /// A flag indicating whether dictionary keys should be processed.
  34. /// Defaults to <c>false</c>.
  35. /// </summary>
  36. public bool ProcessDictionaryKeys { get; set; }
  37. /// <summary>
  38. /// A flag indicating whether extension data names should be processed.
  39. /// Defaults to <c>false</c>.
  40. /// </summary>
  41. public bool ProcessExtensionDataNames { get; set; }
  42. /// <summary>
  43. /// A flag indicating whether explicitly specified property names,
  44. /// e.g. a property name customized with a <see cref="JsonPropertyAttribute"/>, should be processed.
  45. /// Defaults to <c>false</c>.
  46. /// </summary>
  47. public bool OverrideSpecifiedNames { get; set; }
  48. /// <summary>
  49. /// Gets the serialized name for a given property name.
  50. /// </summary>
  51. /// <param name="name">The initial property name.</param>
  52. /// <param name="hasSpecifiedName">A flag indicating whether the property has had a name explicitly specified.</param>
  53. /// <returns>The serialized property name.</returns>
  54. public virtual string GetPropertyName(string name, bool hasSpecifiedName)
  55. {
  56. if (hasSpecifiedName && !OverrideSpecifiedNames)
  57. {
  58. return name;
  59. }
  60. return ResolvePropertyName(name);
  61. }
  62. /// <summary>
  63. /// Gets the serialized name for a given extension data name.
  64. /// </summary>
  65. /// <param name="name">The initial extension data name.</param>
  66. /// <returns>The serialized extension data name.</returns>
  67. public virtual string GetExtensionDataName(string name)
  68. {
  69. if (!ProcessExtensionDataNames)
  70. {
  71. return name;
  72. }
  73. return ResolvePropertyName(name);
  74. }
  75. /// <summary>
  76. /// Gets the serialized key for a given dictionary key.
  77. /// </summary>
  78. /// <param name="key">The initial dictionary key.</param>
  79. /// <returns>The serialized dictionary key.</returns>
  80. public virtual string GetDictionaryKey(string key)
  81. {
  82. if (!ProcessDictionaryKeys)
  83. {
  84. return key;
  85. }
  86. return ResolvePropertyName(key);
  87. }
  88. /// <summary>
  89. /// Resolves the specified property name.
  90. /// </summary>
  91. /// <param name="name">The property name to resolve.</param>
  92. /// <returns>The resolved property name.</returns>
  93. protected abstract string ResolvePropertyName(string name);
  94. }
  95. }