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.

152 lines
5.8 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. using System;
  26. using System.ComponentModel;
  27. namespace Newtonsoft.Json.Linq
  28. {
  29. /// <summary>
  30. /// Represents a view of a <see cref="JProperty"/>.
  31. /// </summary>
  32. internal class JPropertyDescriptor : PropertyDescriptor
  33. {
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="JPropertyDescriptor"/> class.
  36. /// </summary>
  37. /// <param name="name">The name.</param>
  38. public JPropertyDescriptor(string name)
  39. : base(name, null)
  40. {
  41. }
  42. private static JObject CastInstance(object instance)
  43. {
  44. return (JObject)instance;
  45. }
  46. /// <summary>
  47. /// When overridden in a derived class, returns whether resetting an object changes its value.
  48. /// </summary>
  49. /// <returns>
  50. /// <c>true</c> if resetting the component changes its value; otherwise, <c>false</c>.
  51. /// </returns>
  52. /// <param name="component">The component to test for reset capability.</param>
  53. public override bool CanResetValue(object component)
  54. {
  55. return false;
  56. }
  57. /// <summary>
  58. /// When overridden in a derived class, gets the current value of the property on a component.
  59. /// </summary>
  60. /// <returns>
  61. /// The value of a property for a given component.
  62. /// </returns>
  63. /// <param name="component">The component with the property for which to retrieve the value.</param>
  64. public override object GetValue(object component)
  65. {
  66. JToken token = CastInstance(component)[Name];
  67. return token;
  68. }
  69. /// <summary>
  70. /// When overridden in a derived class, resets the value for this property of the component to the default value.
  71. /// </summary>
  72. /// <param name="component">The component with the property value that is to be reset to the default value.</param>
  73. public override void ResetValue(object component)
  74. {
  75. }
  76. /// <summary>
  77. /// When overridden in a derived class, sets the value of the component to a different value.
  78. /// </summary>
  79. /// <param name="component">The component with the property value that is to be set.</param>
  80. /// <param name="value">The new value.</param>
  81. public override void SetValue(object component, object value)
  82. {
  83. JToken token = value as JToken ?? new JValue(value);
  84. CastInstance(component)[Name] = token;
  85. }
  86. /// <summary>
  87. /// When overridden in a derived class, determines a value indicating whether the value of this property needs to be persisted.
  88. /// </summary>
  89. /// <returns>
  90. /// <c>true</c> if the property should be persisted; otherwise, <c>false</c>.
  91. /// </returns>
  92. /// <param name="component">The component with the property to be examined for persistence.</param>
  93. public override bool ShouldSerializeValue(object component)
  94. {
  95. return false;
  96. }
  97. /// <summary>
  98. /// When overridden in a derived class, gets the type of the component this property is bound to.
  99. /// </summary>
  100. /// <returns>
  101. /// A <see cref="Type"/> that represents the type of component this property is bound to.
  102. /// When the <see cref="PropertyDescriptor.GetValue(Object)"/> or
  103. /// <see cref="PropertyDescriptor.SetValue(Object, Object)"/>
  104. /// methods are invoked, the object specified might be an instance of this type.
  105. /// </returns>
  106. public override Type ComponentType => typeof(JObject);
  107. /// <summary>
  108. /// When overridden in a derived class, gets a value indicating whether this property is read-only.
  109. /// </summary>
  110. /// <returns>
  111. /// <c>true</c> if the property is read-only; otherwise, <c>false</c>.
  112. /// </returns>
  113. public override bool IsReadOnly => false;
  114. /// <summary>
  115. /// When overridden in a derived class, gets the type of the property.
  116. /// </summary>
  117. /// <returns>
  118. /// A <see cref="Type"/> that represents the type of the property.
  119. /// </returns>
  120. public override Type PropertyType => typeof(object);
  121. /// <summary>
  122. /// Gets the hash code for the name of the member.
  123. /// </summary>
  124. /// <value></value>
  125. /// <returns>
  126. /// The hash code for the name of the member.
  127. /// </returns>
  128. protected override int NameHashCode
  129. {
  130. get
  131. {
  132. // override property to fix up an error in its documentation
  133. int nameHashCode = base.NameHashCode;
  134. return nameHashCode;
  135. }
  136. }
  137. }
  138. }