JsonAttribute.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // Used to decorate fields and properties that should be serialized
  18. //
  19. // - [Json] on class or struct causes all public fields and properties to be serialized
  20. // - [Json] on a public or non-public field or property causes that member to be serialized
  21. // - [JsonExclude] on a field or property causes that field to be not serialized
  22. // - A class or struct with no [Json] attribute has all public fields/properties serialized
  23. // - A class or struct with no [Json] attribute but a [Json] attribute on one or more members only serializes those members
  24. //
  25. // Use [Json("keyname")] to explicitly specify the key to be used
  26. // [Json] without the keyname will be serialized using the name of the member with the first letter lowercased.
  27. //
  28. // [Json(KeepInstance=true)] causes container/subobject types to be serialized into the existing member instance (if not null)
  29. //
  30. // You can also use the system supplied DataContract and DataMember attributes. They'll only be used if there
  31. // are no JsonKit attributes on the class or it's members. You must specify DataContract on the type and
  32. // DataMember on any fields/properties that require serialization. There's no need for exclude attribute.
  33. // When using DataMember, the name of the field or property is used as is - the first letter is left in upper case
  34. //
  35. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field)]
  36. public class JsonAttribute : Attribute
  37. {
  38. public JsonAttribute()
  39. {
  40. _key = null;
  41. }
  42. public JsonAttribute(string key)
  43. {
  44. _key = key;
  45. }
  46. // Key used to save this field/property
  47. string _key;
  48. public string Key
  49. {
  50. get { return _key; }
  51. }
  52. // If true uses ParseInto to parse into the existing object instance
  53. // If false, creates a new instance as assigns it to the property
  54. public bool KeepInstance
  55. {
  56. get;
  57. set;
  58. }
  59. // If true, the property will be loaded, but not saved
  60. // Use to upgrade deprecated persisted settings, but not
  61. // write them back out again
  62. public bool Deprecated
  63. {
  64. get;
  65. set;
  66. }
  67. }
  68. }