JsonAttribute.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // JsonKit v0.5 - A simple but flexible Json library in a single .cs file.
  2. //
  3. // Copyright (C) 2014 Topten Software (contact@toptensoftware.com) All rights reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this product
  6. // except in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed under the
  11. // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. // either express or implied. See the License for the specific language governing permissions
  13. // and limitations under the License.
  14. using System;
  15. using System.Reflection;
  16. namespace Topten.JsonKit
  17. {
  18. /// <summary>
  19. /// Used to decorate fields and properties that should be serialized
  20. /// </summary>
  21. /// <remarks>
  22. /// - [Json] on class or struct causes all public fields and properties to be serialized
  23. /// - [Json] on a public or non-public field or property causes that member to be serialized
  24. /// - [JsonExclude] on a field or property causes that field to be not serialized
  25. /// - A class or struct with no [Json] attribute has all public fields/properties serialized
  26. /// - A class or struct with no [Json] attribute but a [Json] attribute on one or more members only serializes those members
  27. ///
  28. /// Use [Json("keyname")] to explicitly specify the key to be used
  29. /// [Json] without the keyname will be serialized using the name of the member with the first letter lowercased.
  30. ///
  31. /// [Json(KeepInstance=true)] causes container/subobject types to be serialized into the existing member instance (if not null)
  32. ///
  33. /// You can also use the system supplied DataContract and DataMember attributes. They'll only be used if there
  34. /// are no JsonKit attributes on the class or it's members. You must specify DataContract on the type and
  35. /// DataMember on any fields/properties that require serialization. There's no need for exclude attribute.
  36. /// When using DataMember, the name of the field or property is used as is - the first letter is left in upper case
  37. /// </remarks>
  38. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field)]
  39. [Obfuscation(Exclude = true, ApplyToMembers = true)] // Don't obfuscate because we use reflection to get function names
  40. public class JsonAttribute : Attribute
  41. {
  42. /// <summary>
  43. /// Constructs a new Json attribute
  44. /// </summary>
  45. public JsonAttribute()
  46. {
  47. Key = null;
  48. }
  49. /// <summary>
  50. /// Constructs a new Json attribute, setting the key used for serialization of this item
  51. /// </summary>
  52. /// <param name="key"></param>
  53. public JsonAttribute(string key)
  54. {
  55. Key = key;
  56. }
  57. /// <summary>
  58. /// Gets the serialization key for this Json item
  59. /// </summary>
  60. public string Key
  61. {
  62. get;
  63. private set;
  64. }
  65. /// <summary>
  66. /// Controls whether a collection is serialized by creating and assigning a new
  67. /// instance, or by clearing and serializing into the existing instance.
  68. /// </summary>
  69. /// <remarks>
  70. /// If true uses ParseInto to parse into the existing object instance
  71. /// If false, creates a new instance as assigns it to the property
  72. /// </remarks>
  73. public bool KeepInstance
  74. {
  75. get;
  76. set;
  77. }
  78. /// <summary>
  79. /// Used to decorate deprecated properties that should be loaded
  80. /// but not saved.
  81. /// </summary>
  82. /// <remarks>
  83. /// If true, the property will be loaded, but not saved
  84. /// Use to upgrade deprecated persisted settings, but not
  85. /// write them back out again
  86. /// </remarks>
  87. public bool Deprecated
  88. {
  89. get;
  90. set;
  91. }
  92. /// <summary>
  93. /// For non-value types controls whether null values should be
  94. /// written as null, or excluded for output.
  95. /// </summary>
  96. public bool ExcludeIfNull
  97. {
  98. get;
  99. set;
  100. }
  101. /// <summary>
  102. /// For collection types controls whether they should be serialized
  103. /// if the collection is empty or null
  104. /// </summary>
  105. public bool ExcludeIfEmpty
  106. {
  107. get;
  108. set;
  109. }
  110. /// <summary>
  111. /// Used to simple value types that match a specific value
  112. /// </summary>
  113. public object ExcludeIfEquals
  114. {
  115. get;
  116. set;
  117. }
  118. /// <summary>
  119. /// If no members are marked with JSON attributes, don't try to serialize
  120. /// the whole type - instead serialize as an empty object.
  121. /// </summary>
  122. public bool ExplicitMembersOnly
  123. {
  124. get;
  125. set;
  126. }
  127. }
  128. }