1 | #region License
|
---|
2 | /*---------------------------------------------------------------------------------*\
|
---|
3 |
|
---|
4 | Distributed under the terms of an MIT-style license:
|
---|
5 |
|
---|
6 | The MIT License
|
---|
7 |
|
---|
8 | Copyright (c) 2006-2009 Stephen M. McKamey
|
---|
9 |
|
---|
10 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
11 | of this software and associated documentation files (the "Software"), to deal
|
---|
12 | in the Software without restriction, including without limitation the rights
|
---|
13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | copies of the Software, and to permit persons to whom the Software is
|
---|
15 | furnished to do so, subject to the following conditions:
|
---|
16 |
|
---|
17 | The above copyright notice and this permission notice shall be included in
|
---|
18 | all copies or substantial portions of the Software.
|
---|
19 |
|
---|
20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
26 | THE SOFTWARE.
|
---|
27 |
|
---|
28 | \*---------------------------------------------------------------------------------*/
|
---|
29 | #endregion License
|
---|
30 |
|
---|
31 | using System;
|
---|
32 | using System.Reflection;
|
---|
33 | using System.Xml.Serialization;
|
---|
34 |
|
---|
35 | namespace JsonFx.Json
|
---|
36 | {
|
---|
37 | /// <summary>
|
---|
38 | /// Designates a property or field to not be serialized.
|
---|
39 | /// </summary>
|
---|
40 | [AttributeUsage(AttributeTargets.All, AllowMultiple=false)]
|
---|
41 | public sealed class JsonIgnoreAttribute : Attribute
|
---|
42 | {
|
---|
43 | #region Methods
|
---|
44 |
|
---|
45 | /// <summary>
|
---|
46 | /// Gets a value which indicates if should be ignored in Json serialization.
|
---|
47 | /// </summary>
|
---|
48 | /// <param name="value"></param>
|
---|
49 | /// <returns></returns>
|
---|
50 | public static bool IsJsonIgnore(object value)
|
---|
51 | {
|
---|
52 | if (value == null)
|
---|
53 | {
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 |
|
---|
57 | Type type = value.GetType();
|
---|
58 |
|
---|
59 | ICustomAttributeProvider provider = null;
|
---|
60 | if (type.IsEnum)
|
---|
61 | {
|
---|
62 | provider = type.GetField(Enum.GetName(type, value));
|
---|
63 | }
|
---|
64 | else
|
---|
65 | {
|
---|
66 | provider = value as ICustomAttributeProvider;
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (provider == null)
|
---|
70 | {
|
---|
71 | throw new ArgumentException();
|
---|
72 | }
|
---|
73 |
|
---|
74 | return provider.IsDefined(typeof(JsonIgnoreAttribute), true);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /// <summary>
|
---|
78 | /// Gets a value which indicates if should be ignored in Json serialization.
|
---|
79 | /// </summary>
|
---|
80 | /// <param name="value"></param>
|
---|
81 | /// <returns></returns>
|
---|
82 | public static bool IsXmlIgnore(object value)
|
---|
83 | {
|
---|
84 | if (value == null)
|
---|
85 | {
|
---|
86 | return false;
|
---|
87 | }
|
---|
88 |
|
---|
89 | Type type = value.GetType();
|
---|
90 |
|
---|
91 | ICustomAttributeProvider provider = null;
|
---|
92 | if (type.IsEnum)
|
---|
93 | {
|
---|
94 | provider = type.GetField(Enum.GetName(type, value));
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | provider = value as ICustomAttributeProvider;
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (provider == null)
|
---|
102 | {
|
---|
103 | throw new ArgumentException();
|
---|
104 | }
|
---|
105 |
|
---|
106 | return provider.IsDefined(typeof(XmlIgnoreAttribute), true);
|
---|
107 | }
|
---|
108 |
|
---|
109 | #endregion Methods
|
---|
110 | }
|
---|
111 | }
|
---|