Ejemplo n.º 1
0
        public static string GetSerializationName(this MemberInfo member, SerializationNameFormat serializationNameFormat)
        {
            var jilDirectiveAttr = member.GetCustomAttribute <JilDirectiveAttribute>();

            if (jilDirectiveAttr != null && jilDirectiveAttr.Name != null)
            {
                return(jilDirectiveAttr.Name);
            }

            var dataMemberAttr = member.GetCustomAttribute <System.Runtime.Serialization.DataMemberAttribute>();

            if (dataMemberAttr != null && dataMemberAttr.Name != null)
            {
                return(dataMemberAttr.Name);
            }

            switch (serializationNameFormat)
            {
            case SerializationNameFormat.CamelCase:
                return(member.Name.ToCamelCase());

            default:
                return(member.Name);
            }
        }
        internal static Type GetGenericTypeArgument(this SerializationNameFormat serializationNameFormat)
        {
            switch (serializationNameFormat)
            {
            case SerializationNameFormat.Verbatim:
                return(typeof(SerializationNameFormatVerbatim));

            case SerializationNameFormat.CamelCase:
                return(typeof(SerializationNameFormatCamelCase));

            default:
                throw new ArgumentOutOfRangeException("serializationNameFormat", serializationNameFormat, null);
            }
        }
Ejemplo n.º 3
0
        public static string GetSerializationName(this MemberInfo member, SerializationNameFormat serializationNameFormat)
        {
            var jilDirectiveAttr = member.GetCustomAttribute <JilDirectiveAttribute>();

            if (jilDirectiveAttr != null && !string.IsNullOrEmpty(jilDirectiveAttr.Name))
            {
                return(jilDirectiveAttr.Name);
            }

            var dataMemberAttr = member.GetCustomAttribute <System.Runtime.Serialization.DataMemberAttribute>();

            if (dataMemberAttr != null && !string.IsNullOrEmpty(dataMemberAttr.Name))
            {
                return(dataMemberAttr.Name);
            }

            switch (serializationNameFormat)
            {
            case SerializationNameFormat.CamelCase:
                var s = member.Name;
                if (string.IsNullOrEmpty(s) || !char.IsUpper(s[0]))
                {
                    return(s);
                }

                var cArr = s.ToCharArray();
                for (var i = 0; i < cArr.Length; i++)
                {
                    if (i > 0 && i + 1 < cArr.Length && !char.IsUpper(cArr[i + 1]))
                    {
                        break;
                    }
                    cArr[i] = char.ToLowerInvariant(cArr[i]);
                }
                return(new string(cArr));

            default:
                return(member.Name);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Configuration for Jil serialization options.
        /// 
        /// Available options:
        ///   prettyPrint - whether or not to include whitespace and newlines for ease of reading
        ///   excludeNulls - whether or not to write object members whose value is null
        ///   jsonp - whether or not the serialized json should be valid for use with JSONP
        ///   dateFormat - the style in which to serialize DateTimes and TimeSpans
        ///   includeInherited - whether or not to serialize members declared by an objects base types
        ///   unspecifiedDateTimeKindBehavior - how to treat DateTime's with a DateTimeKind of Unspecified (Jil converts all DateTimes to Utc for serialization, use DateTimeOffset to preserve time zone information)
        ///   serializationNameFormat - how to serialize names of properties/objects unless specified explicitly by an attribute
        /// </summary>
        public Options(bool prettyPrint = false, bool excludeNulls = false, bool jsonp = false, DateTimeFormat dateFormat = DateTimeFormat.MicrosoftStyleMillisecondsSinceUnixEpoch, bool includeInherited = false, UnspecifiedDateTimeKindBehavior unspecifiedDateTimeKindBehavior = UnspecifiedDateTimeKindBehavior.IsLocal, SerializationNameFormat serializationNameFormat = SerializationNameFormat.Verbatim)
        {
            ShouldPrettyPrint = prettyPrint;
            ShouldExcludeNulls = excludeNulls;
            IsJSONP = jsonp;

#pragma warning disable 618
            // upgrade from the obsolete DateTimeFormat enumeration; warning disabled to allow it, but all other references
            //  should be expunged
            if (dateFormat == DateTimeFormat.NewtonsoftStyleMillisecondsSinceUnixEpoch)
            {
                dateFormat = DateTimeFormat.MicrosoftStyleMillisecondsSinceUnixEpoch;
            }
#pragma warning restore 618

            UseDateTimeFormat = dateFormat;
            ShouldIncludeInherited = includeInherited;
            UseUnspecifiedDateTimeKindBehavior = unspecifiedDateTimeKindBehavior;
            SerializationNameFormat = serializationNameFormat;
        }