[7849] | 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 | /// Specifies the naming to use for a property or field when serializing
|
---|
| 39 | /// </summary>
|
---|
| 40 | [AttributeUsage(AttributeTargets.All, AllowMultiple=false)]
|
---|
| 41 | public class JsonNameAttribute : Attribute
|
---|
| 42 | {
|
---|
| 43 | #region Fields
|
---|
| 44 |
|
---|
| 45 | private string jsonName = null;
|
---|
| 46 |
|
---|
| 47 | #endregion Fields
|
---|
| 48 |
|
---|
| 49 | #region Init
|
---|
| 50 |
|
---|
| 51 | /// <summary>
|
---|
| 52 | /// Ctor
|
---|
| 53 | /// </summary>
|
---|
| 54 | public JsonNameAttribute()
|
---|
| 55 | {
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | /// <summary>
|
---|
| 59 | /// Ctor
|
---|
| 60 | /// </summary>
|
---|
| 61 | /// <param name="jsonName"></param>
|
---|
| 62 | public JsonNameAttribute(string jsonName)
|
---|
| 63 | {
|
---|
| 64 | this.jsonName = JsonWriter.EnsureValidIdentifier(jsonName, false);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | #endregion Init
|
---|
| 68 |
|
---|
| 69 | #region Properties
|
---|
| 70 |
|
---|
| 71 | /// <summary>
|
---|
| 72 | /// Gets and sets the name to be used in JSON
|
---|
| 73 | /// </summary>
|
---|
| 74 | public string Name
|
---|
| 75 | {
|
---|
| 76 | get { return this.jsonName; }
|
---|
| 77 | set { this.jsonName = JsonWriter.EnsureValidIdentifier(value, false); }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | #endregion Properties
|
---|
| 81 |
|
---|
| 82 | #region Methods
|
---|
| 83 |
|
---|
| 84 | /// <summary>
|
---|
| 85 | /// Gets the name specified for use in Json serialization.
|
---|
| 86 | /// </summary>
|
---|
| 87 | /// <param name="value"></param>
|
---|
| 88 | /// <returns></returns>
|
---|
| 89 | public static string GetJsonName(object value)
|
---|
| 90 | {
|
---|
| 91 | if (value == null)
|
---|
| 92 | {
|
---|
| 93 | return null;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | Type type = value.GetType();
|
---|
| 97 | MemberInfo memberInfo = null;
|
---|
| 98 |
|
---|
| 99 | if (type.IsEnum)
|
---|
| 100 | {
|
---|
| 101 | string name = Enum.GetName(type, value);
|
---|
| 102 | if (String.IsNullOrEmpty(name))
|
---|
| 103 | {
|
---|
| 104 | return null;
|
---|
| 105 | }
|
---|
| 106 | memberInfo = type.GetField(name);
|
---|
| 107 | }
|
---|
| 108 | else
|
---|
| 109 | {
|
---|
| 110 | memberInfo = value as MemberInfo;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | if (memberInfo == null)
|
---|
| 114 | {
|
---|
| 115 | throw new ArgumentException();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | if (!Attribute.IsDefined(memberInfo, typeof(JsonNameAttribute)))
|
---|
| 119 | {
|
---|
| 120 | return null;
|
---|
| 121 | }
|
---|
| 122 | JsonNameAttribute attribute = (JsonNameAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(JsonNameAttribute));
|
---|
| 123 |
|
---|
| 124 | return attribute.Name;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | ///// <summary>
|
---|
| 128 | ///// Gets the name specified for use in Json serialization.
|
---|
| 129 | ///// </summary>
|
---|
| 130 | ///// <param name="value"></param>
|
---|
| 131 | ///// <returns></returns>
|
---|
| 132 | //public static string GetXmlName(object value)
|
---|
| 133 | //{
|
---|
| 134 | // if (value == null)
|
---|
| 135 | // {
|
---|
| 136 | // return null;
|
---|
| 137 | // }
|
---|
| 138 |
|
---|
| 139 | // Type type = value.GetType();
|
---|
| 140 | // ICustomAttributeProvider memberInfo = null;
|
---|
| 141 |
|
---|
| 142 | // if (type.IsEnum)
|
---|
| 143 | // {
|
---|
| 144 | // string name = Enum.GetName(type, value);
|
---|
| 145 | // if (String.IsNullOrEmpty(name))
|
---|
| 146 | // {
|
---|
| 147 | // return null;
|
---|
| 148 | // }
|
---|
| 149 | // memberInfo = type.GetField(name);
|
---|
| 150 | // }
|
---|
| 151 | // else
|
---|
| 152 | // {
|
---|
| 153 | // memberInfo = value as ICustomAttributeProvider;
|
---|
| 154 | // }
|
---|
| 155 |
|
---|
| 156 | // if (memberInfo == null)
|
---|
| 157 | // {
|
---|
| 158 | // throw new ArgumentException();
|
---|
| 159 | // }
|
---|
| 160 |
|
---|
| 161 | // XmlAttributes xmlAttributes = new XmlAttributes(memberInfo);
|
---|
| 162 | // if (xmlAttributes.XmlElements.Count == 1 &&
|
---|
| 163 | // !String.IsNullOrEmpty(xmlAttributes.XmlElements[0].ElementName))
|
---|
| 164 | // {
|
---|
| 165 | // return xmlAttributes.XmlElements[0].ElementName;
|
---|
| 166 | // }
|
---|
| 167 | // if (xmlAttributes.XmlAttribute != null &&
|
---|
| 168 | // !String.IsNullOrEmpty(xmlAttributes.XmlAttribute.AttributeName))
|
---|
| 169 | // {
|
---|
| 170 | // return xmlAttributes.XmlAttribute.AttributeName;
|
---|
| 171 | // }
|
---|
| 172 |
|
---|
| 173 | // return null;
|
---|
| 174 | //}
|
---|
| 175 |
|
---|
| 176 | #endregion Methods
|
---|
| 177 | }
|
---|
| 178 | }
|
---|