JsonAttribute.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. namespace Topten.JsonKit
  16. {
  17. /// <summary>
  18. /// Used to decorate fields and properties that should be serialized
  19. /// </summary>
  20. /// <remarks>
  21. /// - [Json] on class or struct causes all public fields and properties to be serialized
  22. /// - [Json] on a public or non-public field or property causes that member to be serialized
  23. /// - [JsonExclude] on a field or property causes that field to be not serialized
  24. /// - A class or struct with no [Json] attribute has all public fields/properties serialized
  25. /// - A class or struct with no [Json] attribute but a [Json] attribute on one or more members only serializes those members
  26. ///
  27. /// Use [Json("keyname")] to explicitly specify the key to be used
  28. /// [Json] without the keyname will be serialized using the name of the member with the first letter lowercased.
  29. ///
  30. /// [Json(KeepInstance=true)] causes container/subobject types to be serialized into the existing member instance (if not null)
  31. ///
  32. /// You can also use the system supplied DataContract and DataMember attributes. They'll only be used if there
  33. /// are no JsonKit attributes on the class or it's members. You must specify DataContract on the type and
  34. /// DataMember on any fields/properties that require serialization. There's no need for exclude attribute.
  35. /// When using DataMember, the name of the field or property is used as is - the first letter is left in upper case
  36. /// </remarks>
  37. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field)]
  38. public class JsonAttribute : Attribute
  39. {
  40. /// <summary>
  41. /// Constructs a new Json attribute
  42. /// </summary>
  43. public JsonAttribute()
  44. {
  45. Key = null;
  46. }
  47. /// <summary>
  48. /// Constructs a new Json attribute, setting the key used for serialization of this item
  49. /// </summary>
  50. /// <param name="key"></param>
  51. public JsonAttribute(string key)
  52. {
  53. Key = key;
  54. }
  55. /// <summary>
  56. /// Gets the serialization key for this Json item
  57. /// </summary>
  58. public string Key
  59. {
  60. get;
  61. private set;
  62. }
  63. /// <summary>
  64. /// Controls whether a collection is serialized by creating and assigning a new
  65. /// instance, or by clearing and serializing into the existing instance.
  66. /// </summary>
  67. /// <remarks>
  68. /// If true uses ParseInto to parse into the existing object instance
  69. /// If false, creates a new instance as assigns it to the property
  70. /// </remarks>
  71. public bool KeepInstance
  72. {
  73. get;
  74. set;
  75. }
  76. /// <summary>
  77. /// Used to decorate deprecated properties that should be loaded
  78. /// but not saved.
  79. /// </summary>
  80. /// <remarks>
  81. /// If true, the property will be loaded, but not saved
  82. /// Use to upgrade deprecated persisted settings, but not
  83. /// write them back out again
  84. /// </remarks>
  85. public bool Deprecated
  86. {
  87. get;
  88. set;
  89. }
  90. /// <summary>
  91. /// For non-value types controls whether null values should be
  92. /// written as null, or excluded for output.
  93. /// </summary>
  94. public bool ExcludeIfNull
  95. {
  96. get;
  97. set;
  98. }
  99. /// <summary>
  100. /// For collection types controls whether they should be serialized
  101. /// if the collection is empty.
  102. /// </summary>
  103. public bool ExcludeIfEmpty
  104. {
  105. get;
  106. set;
  107. }
  108. }
  109. }