JsonAttribute.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // Define JsonKit_NO_DYNAMIC to disable Expando support
  15. // Define JsonKit_NO_EMIT to disable Reflection.Emit
  16. // Define JsonKit_NO_DATACONTRACT to disable support for [DataContract]/[DataMember]
  17. using System;
  18. namespace Topten.JsonKit
  19. {
  20. // Used to decorate fields and properties that should be serialized
  21. //
  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. //
  38. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field)]
  39. public class JsonAttribute : Attribute
  40. {
  41. public JsonAttribute()
  42. {
  43. _key = null;
  44. }
  45. public JsonAttribute(string key)
  46. {
  47. _key = key;
  48. }
  49. // Key used to save this field/property
  50. string _key;
  51. public string Key
  52. {
  53. get { return _key; }
  54. }
  55. // If true uses ParseInto to parse into the existing object instance
  56. // If false, creates a new instance as assigns it to the property
  57. public bool KeepInstance
  58. {
  59. get;
  60. set;
  61. }
  62. // If true, the property will be loaded, but not saved
  63. // Use to upgrade deprecated persisted settings, but not
  64. // write them back out again
  65. public bool Deprecated
  66. {
  67. get;
  68. set;
  69. }
  70. }
  71. }