ParseShortestXsdDateTime() public static method

public static ParseShortestXsdDateTime ( string dateTimeStr ) : System.DateTime
dateTimeStr string
return System.DateTime
コード例 #1
0
        public static object ParseQuotedPrimitive(string value)
        {
            var fn = JsConfig.ParsePrimitiveFn;

            if (fn != null)
            {
                var result = fn(value);
                if (result != null)
                {
                    return(result);
                }
            }

            if (string.IsNullOrEmpty(value))
            {
                return(null);
            }

            Guid guidValue;

            if (Guid.TryParse(value, out guidValue))
            {
                return(guidValue);
            }

            if (value.StartsWith(DateTimeSerializer.EscapedWcfJsonPrefix, StringComparison.Ordinal) || value.StartsWith(DateTimeSerializer.WcfJsonPrefix, StringComparison.Ordinal))
            {
                return(DateTimeSerializer.ParseWcfJsonDate(value));
            }

            if (JsConfig.DateHandler == DateHandler.ISO8601)
            {
                // check that we have UTC ISO8601 date:
                // YYYY-MM-DDThh:mm:ssZ
                // YYYY-MM-DDThh:mm:ss+02:00
                // YYYY-MM-DDThh:mm:ss-02:00
                if (value.Length > 14 && value[10] == 'T' &&
                    (value.EndsWithInvariant("Z") ||
                     value[value.Length - 6] == '+' ||
                     value[value.Length - 6] == '-'))
                {
                    return(DateTimeSerializer.ParseShortestXsdDateTime(value));
                }
            }

            if (JsConfig.DateHandler == DateHandler.RFC1123)
            {
                // check that we have RFC1123 date:
                // ddd, dd MMM yyyy HH:mm:ss GMT
                if (value.Length == 29 && (value.EndsWithInvariant("GMT")))
                {
                    return(DateTimeSerializer.ParseRFC1123DateTime(value));
                }
            }

            return(Serializer.UnescapeString(value));
        }
コード例 #2
0
        public static object ParseQuotedPrimitive(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(null);
            }

            Guid guidValue;

#if NET4
            if (Guid.TryParse(value, out guidValue))
            {
                return(guidValue);
            }
#else
            try
            {
                guidValue = new Guid(value);


                return(guidValue);
            }

            catch {
            }
#endif

            if (value.StartsWith(DateTimeSerializer.EscapedWcfJsonPrefix, StringComparison.Ordinal) || value.StartsWith(DateTimeSerializer.WcfJsonPrefix, StringComparison.Ordinal))
            {
                return(DateTimeSerializer.ParseWcfJsonDate(value));
            }

            if (JsConfig.DateHandler == JsonDateHandler.ISO8601)
            {
                // check that we have UTC ISO8601 date:
                // YYYY-MM-DDThh:mm:ssZ
                // YYYY-MM-DDThh:mm:ss+02:00
                // YYYY-MM-DDThh:mm:ss-02:00
                if (value.Length > 14 && value[10] == 'T' &&
                    (value.EndsWithInvariant("Z") ||
                     value[value.Length - 6] == '+' ||
                     value[value.Length - 6] == '-'))
                {
                    return(DateTimeSerializer.ParseShortestXsdDateTime(value));
                }
            }

            return(Serializer.UnescapeString(value));
        }
コード例 #3
0
        private static ParseStringDelegate GetParseFn()
        {
            var nullableType = Nullable.GetUnderlyingType(typeof(T));

            if (nullableType == null)
            {
#if NETFX_CORE
                var typeCode = ReflectionExtensions.GetTypeCode(typeof(T));
#else
                var typeCode = Type.GetTypeCode(typeof(T));
#endif
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    //Lots of kids like to use '1', HTML checkboxes use 'on' as a soft convention
                    return(value =>
                           value.Length == 1 ?
                           value == "1"
                            : value.Length == 2 ?
                           value == "on" :
                           bool.Parse(value));

                case TypeCode.Byte:
                    return(value => byte.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.SByte:
                    return(value => sbyte.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Int16:
                    return(value => short.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.UInt16:
                    return(value => ushort.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Int32:
                    return(value => int.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.UInt32:
                    return(value => uint.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Int64:
                    return(value => long.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.UInt64:
                    return(value => ulong.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Single:
                    return(value => float.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Double:
                    return(value => double.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Decimal:
                    return(value => decimal.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.DateTime:
                    return(value => DateTimeSerializer.ParseShortestXsdDateTime(value));

                case TypeCode.Char:
                    char cValue;
                    return(value => char.TryParse(value, out cValue) ? cValue : '\0');
                }

                if (typeof(T) == typeof(Guid))
                {
                    return(value => new Guid(value));
                }
                if (typeof(T) == typeof(DateTimeOffset))
                {
                    return(value => DateTimeSerializer.ParseDateTimeOffset(value));
                }
                if (typeof(T) == typeof(TimeSpan))
                {
                    return(value => DateTimeSerializer.ParseTimeSpan(value));
                }
#if !MONOTOUCH && !SILVERLIGHT && !XBOX && !ANDROID
                if (typeof(T) == typeof(System.Data.Linq.Binary))
                {
                    return(value => new System.Data.Linq.Binary(Convert.FromBase64String(value)));
                }
#endif
            }
            else
            {
#if NETFX_CORE
                var typeCode = ReflectionExtensions.GetTypeCode(nullableType);
#else
                var typeCode = Type.GetTypeCode(nullableType);
#endif
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    return(value => string.IsNullOrEmpty(value) ?
                           (bool?)null
                            : value.Length == 1 ?
                           value == "1"
                            : value.Length == 2 ?
                           value == "on" :
                           bool.Parse(value));

                case TypeCode.Byte:
                    return(value => string.IsNullOrEmpty(value) ? (byte?)null : byte.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.SByte:
                    return(value => string.IsNullOrEmpty(value) ? (sbyte?)null : sbyte.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Int16:
                    return(value => string.IsNullOrEmpty(value) ? (short?)null : short.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.UInt16:
                    return(value => string.IsNullOrEmpty(value) ? (ushort?)null : ushort.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Int32:
                    return(value => string.IsNullOrEmpty(value) ? (int?)null : int.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.UInt32:
                    return(value => string.IsNullOrEmpty(value) ? (uint?)null : uint.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Int64:
                    return(value => string.IsNullOrEmpty(value) ? (long?)null : long.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.UInt64:
                    return(value => string.IsNullOrEmpty(value) ? (ulong?)null : ulong.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Single:
                    return(value => string.IsNullOrEmpty(value) ? (float?)null : float.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Double:
                    return(value => string.IsNullOrEmpty(value) ? (double?)null : double.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Decimal:
                    return(value => string.IsNullOrEmpty(value) ? (decimal?)null : decimal.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.DateTime:
                    return(value => DateTimeSerializer.ParseShortestNullableXsdDateTime(value));

                case TypeCode.Char:
                    char cValue;
                    return(value => string.IsNullOrEmpty(value) ? (char?)null : char.TryParse(value, out cValue) ? cValue : '\0');
                }

                if (typeof(T) == typeof(TimeSpan?))
                {
                    return(value => DateTimeSerializer.ParseNullableTimeSpan(value));
                }
                if (typeof(T) == typeof(Guid?))
                {
                    return(value => string.IsNullOrEmpty(value) ? (Guid?)null : new Guid(value));
                }
                if (typeof(T) == typeof(DateTimeOffset?))
                {
                    return(value => DateTimeSerializer.ParseNullableDateTimeOffset(value));
                }
            }

            return(null);
        }
コード例 #4
0
        private static ParseStringDelegate GetParseFn()
        {
            //Note the generic typeof(T) is faster than using var type = typeof(T)
            if (typeof(T) == typeof(bool) || typeof(T) == typeof(bool?))
            {
                return(value => bool.Parse(value));
            }
            if (typeof(T) == typeof(byte) || typeof(T) == typeof(byte?))
            {
                return(value => byte.Parse(value));
            }
            if (typeof(T) == typeof(sbyte) || typeof(T) == typeof(sbyte?))
            {
                return(value => sbyte.Parse(value));
            }
            if (typeof(T) == typeof(short) || typeof(T) == typeof(short?))
            {
                return(value => short.Parse(value));
            }
            if (typeof(T) == typeof(int) || typeof(T) == typeof(int?))
            {
                return(value => int.Parse(value));
            }
            if (typeof(T) == typeof(long) || typeof(T) == typeof(long?))
            {
                return(value => long.Parse(value));
            }
            if (typeof(T) == typeof(float) || typeof(T) == typeof(float?))
            {
                return(value => float.Parse(value, CultureInfo.InvariantCulture));
            }
            if (typeof(T) == typeof(double) || typeof(T) == typeof(double?))
            {
                return(value => double.Parse(value, CultureInfo.InvariantCulture));
            }
            if (typeof(T) == typeof(decimal) || typeof(T) == typeof(decimal?))
            {
                return(value => decimal.Parse(value, CultureInfo.InvariantCulture));
            }

            if (typeof(T) == typeof(Guid) || typeof(T) == typeof(Guid?))
            {
                return(value => new Guid(value));
            }
            if (typeof(T) == typeof(DateTime) || typeof(T) == typeof(DateTime?))
            {
                return(value => DateTimeSerializer.ParseShortestXsdDateTime(value));
            }
            if (typeof(T) == typeof(TimeSpan) || typeof(T) == typeof(TimeSpan?))
            {
                return(value => TimeSpan.Parse(value));
            }

            if (typeof(T) == typeof(char) || typeof(T) == typeof(char?))
            {
                char cValue;
                return(value => char.TryParse(value, out cValue) ? cValue : '\0');
            }
            if (typeof(T) == typeof(ushort) || typeof(T) == typeof(ushort?))
            {
                return(value => ushort.Parse(value));
            }
            if (typeof(T) == typeof(uint) || typeof(T) == typeof(uint?))
            {
                return(value => uint.Parse(value));
            }
            if (typeof(T) == typeof(ulong) || typeof(T) == typeof(ulong?))
            {
                return(value => ulong.Parse(value));
            }

            return(null);
        }
コード例 #5
0
        private static ParseStringSegmentDelegate GetParseStringSegmentFn()
        {
            var nullableType = Nullable.GetUnderlyingType(typeof(T));

            if (nullableType == null)
            {
                var typeCode = typeof(T).GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    //Lots of kids like to use '1', HTML checkboxes use 'on' as a soft convention
                    return(value =>
                           value.Length == 1 ?
                           value.Equals("1")
                            : value.Length == 2 ?
                           value.Equals("on") :
                           value.ParseBoolean());

                case TypeCode.Byte:
                    return(value => value.ParseByte());

                case TypeCode.SByte:
                    return(value => value.ParseSByte());

                case TypeCode.Int16:
                    return(value => value.ParseInt16());

                case TypeCode.UInt16:
                    return(value => value.ParseUInt16());

                case TypeCode.Int32:
                    return(value => value.ParseInt32());

                case TypeCode.UInt32:
                    return(value => value.ParseUInt32());

                case TypeCode.Int64:
                    return(value => value.ParseInt64());

                case TypeCode.UInt64:
                    return(value => value.ParseUInt64());

                case TypeCode.Single:
                    return(value => float.Parse(value.Value, CultureInfo.InvariantCulture));

                case TypeCode.Double:
                    return(value => double.Parse(value.Value, CultureInfo.InvariantCulture));

                case TypeCode.Decimal:
                    return(value => value.ParseDecimal(allowThousands: true));

                case TypeCode.DateTime:
                    return(value => DateTimeSerializer.ParseShortestXsdDateTime(value.Value));

                case TypeCode.Char:
                    return(value =>
                    {
                        char cValue;
                        return char.TryParse(value.Value, out cValue) ? cValue : '\0';
                    });
                }

                if (typeof(T) == typeof(Guid))
                {
                    return(value => value.ParseGuid());
                }
                if (typeof(T) == typeof(DateTimeOffset))
                {
                    return(value => DateTimeSerializer.ParseDateTimeOffset(value.Value));
                }
                if (typeof(T) == typeof(TimeSpan))
                {
                    return(value => DateTimeSerializer.ParseTimeSpan(value.Value));
                }
            }
            else
            {
                var typeCode = nullableType.GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    return(value => value.IsNullOrEmpty() ?
                           (bool?)null
                            : value.Length == 1 ?
                           value.Equals("1")
                            : value.Length == 2 ?
                           value.Equals("on") :
                           value.ParseBoolean());

                case TypeCode.Byte:
                    return(value => value.IsNullOrEmpty() ? (byte?)null : value.ParseByte());

                case TypeCode.SByte:
                    return(value => value.IsNullOrEmpty() ? (sbyte?)null : value.ParseSByte());

                case TypeCode.Int16:
                    return(value => value.IsNullOrEmpty() ? (short?)null : value.ParseInt16());

                case TypeCode.UInt16:
                    return(value => value.IsNullOrEmpty() ? (ushort?)null : value.ParseUInt16());

                case TypeCode.Int32:
                    return(value => value.IsNullOrEmpty() ? (int?)null : value.ParseInt32());

                case TypeCode.UInt32:
                    return(value => value.IsNullOrEmpty() ? (uint?)null : value.ParseUInt32());

                case TypeCode.Int64:
                    return(value => value.IsNullOrEmpty() ? (long?)null : value.ParseInt64());

                case TypeCode.UInt64:
                    return(value => value.IsNullOrEmpty() ? (ulong?)null : value.ParseUInt64());

                case TypeCode.Single:
                    return(value => value.IsNullOrEmpty() ? (float?)null : float.Parse(value.Value, CultureInfo.InvariantCulture));

                case TypeCode.Double:
                    return(value => value.IsNullOrEmpty() ? (double?)null : double.Parse(value.Value, CultureInfo.InvariantCulture));

                case TypeCode.Decimal:
                    return(value => value.IsNullOrEmpty() ? (decimal?)null : value.ParseDecimal(allowThousands: true));

                case TypeCode.DateTime:
                    return(value => DateTimeSerializer.ParseShortestNullableXsdDateTime(value.Value));

                case TypeCode.Char:
                    return(value =>
                    {
                        char cValue;
                        return value.IsNullOrEmpty() ? (char?)null : char.TryParse(value.Value, out cValue) ? cValue : '\0';
                    });
                }

                if (typeof(T) == typeof(TimeSpan?))
                {
                    return(value => DateTimeSerializer.ParseNullableTimeSpan(value.Value));
                }
                if (typeof(T) == typeof(Guid?))
                {
                    return(value => value.IsNullOrEmpty() ? (Guid?)null : value.ParseGuid());
                }
                if (typeof(T) == typeof(DateTimeOffset?))
                {
                    return(value => DateTimeSerializer.ParseNullableDateTimeOffset(value.Value));
                }
            }

            return(null);
        }
コード例 #6
0
        private static ParseStringDelegate GetParseFn()
        {
            //Note the generic typeof(T) is faster than using var type = typeof(T)
            if (typeof(T) == typeof(bool))
            {
                return(value => bool.Parse(value));
            }
            if (typeof(T) == typeof(byte))
            {
                return(value => byte.Parse(value));
            }
            if (typeof(T) == typeof(sbyte))
            {
                return(value => sbyte.Parse(value));
            }
            if (typeof(T) == typeof(short))
            {
                return(value => short.Parse(value));
            }
            if (typeof(T) == typeof(int))
            {
                return(value => int.Parse(value));
            }
            if (typeof(T) == typeof(long))
            {
                return(value => long.Parse(value));
            }
            if (typeof(T) == typeof(float))
            {
                return(value => float.Parse(value, CultureInfo.InvariantCulture));
            }
            if (typeof(T) == typeof(double))
            {
                return(value => double.Parse(value, CultureInfo.InvariantCulture));
            }
            if (typeof(T) == typeof(decimal))
            {
                return(value => decimal.Parse(value, CultureInfo.InvariantCulture));
            }

            if (typeof(T) == typeof(Guid))
            {
                return(value => new Guid(value));
            }
            if (typeof(T) == typeof(DateTime?))
            {
                return(value => DateTimeSerializer.ParseShortestNullableXsdDateTime(value));
            }
            if (typeof(T) == typeof(DateTime) || typeof(T) == typeof(DateTime?))
            {
                return(value => DateTimeSerializer.ParseShortestXsdDateTime(value));
            }
            if (typeof(T) == typeof(DateTimeOffset) || typeof(T) == typeof(DateTimeOffset?))
            {
                return(value => DateTimeSerializer.ParseDateTimeOffset(value));
            }
            if (typeof(T) == typeof(TimeSpan))
            {
                return(value => DateTimeSerializer.ParseTimeSpan(value));
            }
            if (typeof(T) == typeof(TimeSpan?))
            {
                return(value => DateTimeSerializer.ParseNullableTimeSpan(value));
            }
#if !MONOTOUCH && !SILVERLIGHT && !XBOX && !ANDROID
            if (typeof(T) == typeof(System.Data.Linq.Binary))
            {
                return(value => new System.Data.Linq.Binary(Convert.FromBase64String(value)));
            }
#endif
            if (typeof(T) == typeof(char))
            {
                char cValue;
                return(value => char.TryParse(value, out cValue) ? cValue : '\0');
            }
            if (typeof(T) == typeof(ushort))
            {
                return(value => ushort.Parse(value));
            }
            if (typeof(T) == typeof(uint))
            {
                return(value => uint.Parse(value));
            }
            if (typeof(T) == typeof(ulong))
            {
                return(value => ulong.Parse(value));
            }

            if (typeof(T) == typeof(bool?))
            {
                return(value => value == null ? (bool?)null : bool.Parse(value));
            }
            if (typeof(T) == typeof(byte?))
            {
                return(value => value == null ? (byte?)null : byte.Parse(value));
            }
            if (typeof(T) == typeof(sbyte?))
            {
                return(value => value == null ? (sbyte?)null : sbyte.Parse(value));
            }
            if (typeof(T) == typeof(short?))
            {
                return(value => value == null ? (short?)null : short.Parse(value));
            }
            if (typeof(T) == typeof(int?))
            {
                return(value => value == null ? (int?)null : int.Parse(value));
            }
            if (typeof(T) == typeof(long?))
            {
                return(value => value == null ? (long?)null : long.Parse(value));
            }
            if (typeof(T) == typeof(float?))
            {
                return(value => value == null ? (float?)null : float.Parse(value, CultureInfo.InvariantCulture));
            }
            if (typeof(T) == typeof(double?))
            {
                return(value => value == null ? (double?)null : double.Parse(value, CultureInfo.InvariantCulture));
            }
            if (typeof(T) == typeof(decimal?))
            {
                return(value => value == null ? (decimal?)null : decimal.Parse(value, CultureInfo.InvariantCulture));
            }

            if (typeof(T) == typeof(TimeSpan?))
            {
                return(value => value == null ? (TimeSpan?)null : TimeSpan.Parse(value));
            }
            if (typeof(T) == typeof(Guid?))
            {
                return(value => value == null ? (Guid?)null : new Guid(value));
            }
            if (typeof(T) == typeof(ushort?))
            {
                return(value => value == null ? (ushort?)null : ushort.Parse(value));
            }
            if (typeof(T) == typeof(uint?))
            {
                return(value => value == null ? (uint?)null : uint.Parse(value));
            }
            if (typeof(T) == typeof(ulong?))
            {
                return(value => value == null ? (ulong?)null : ulong.Parse(value));
            }

            if (typeof(T) == typeof(char?))
            {
                char cValue;
                return(value => value == null ? (char?)null : char.TryParse(value, out cValue) ? cValue : '\0');
            }

            return(null);
        }
コード例 #7
0
        private static ParseStringDelegate GetParseFn()
        {
            var nullableType = Nullable.GetUnderlyingType(typeof(T));

            if (nullableType == null)
            {
                var typeCode = typeof(T).GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    //Lots of kids like to use '1', HTML checkboxes use 'on' as a soft convention
                    return(value =>
                           value.Length == 1 ?
                           value == "1"
                            : value.Length == 2 ?
                           value == "on" :
                           bool.Parse(value));

                case TypeCode.Byte:
                    return(value => byte.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.SByte:
                    return(value => sbyte.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Int16:
                    return(value => short.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.UInt16:
                    return(value => ushort.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Int32:
                    return(value => int.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.UInt32:
                    return(value => uint.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Int64:
                    return(value => long.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.UInt64:
                    return(value => ulong.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Single:
                    return(value => float.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Double:
                    return(value => double.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Decimal:
                    return(value => decimal.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.DateTime:
                    return(value => DateTimeSerializer.ParseShortestXsdDateTime(value));

                case TypeCode.Char:
                    char cValue;
                    return(value => char.TryParse(value, out cValue) ? cValue : '\0');
                }

                if (typeof(T) == typeof(Guid))
                {
                    return(value => new Guid(value));
                }
                if (typeof(T) == typeof(DateTimeOffset))
                {
                    return(value => DateTimeSerializer.ParseDateTimeOffset(value));
                }
                if (typeof(T) == typeof(TimeSpan))
                {
                    return(value => DateTimeSerializer.ParseTimeSpan(value));
                }
            }
            else
            {
                var typeCode = nullableType.GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    return(value => string.IsNullOrEmpty(value) ?
                           (bool?)null
                            : value.Length == 1 ?
                           value == "1"
                            : value.Length == 2 ?
                           value == "on" :
                           bool.Parse(value));

                case TypeCode.Byte:
                    return(value => string.IsNullOrEmpty(value) ? (byte?)null : byte.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.SByte:
                    return(value => string.IsNullOrEmpty(value) ? (sbyte?)null : sbyte.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Int16:
                    return(value => string.IsNullOrEmpty(value) ? (short?)null : short.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.UInt16:
                    return(value => string.IsNullOrEmpty(value) ? (ushort?)null : ushort.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Int32:
                    return(value => string.IsNullOrEmpty(value) ? (int?)null : int.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.UInt32:
                    return(value => string.IsNullOrEmpty(value) ? (uint?)null : uint.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Int64:
                    return(value => string.IsNullOrEmpty(value) ? (long?)null : long.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.UInt64:
                    return(value => string.IsNullOrEmpty(value) ? (ulong?)null : ulong.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Single:
                    return(value => string.IsNullOrEmpty(value) ? (float?)null : float.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Double:
                    return(value => string.IsNullOrEmpty(value) ? (double?)null : double.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.Decimal:
                    return(value => string.IsNullOrEmpty(value) ? (decimal?)null : decimal.Parse(value, CultureInfo.InvariantCulture));

                case TypeCode.DateTime:
                    return(value => DateTimeSerializer.ParseShortestNullableXsdDateTime(value));

                case TypeCode.Char:
                    char cValue;
                    return(value => string.IsNullOrEmpty(value) ? (char?)null : char.TryParse(value, out cValue) ? cValue : '\0');
                }

                if (typeof(T) == typeof(TimeSpan?))
                {
                    return(value => DateTimeSerializer.ParseNullableTimeSpan(value));
                }
                if (typeof(T) == typeof(Guid?))
                {
                    return(value => string.IsNullOrEmpty(value) ? (Guid?)null : new Guid(value));
                }
                if (typeof(T) == typeof(DateTimeOffset?))
                {
                    return(value => DateTimeSerializer.ParseNullableDateTimeOffset(value));
                }
            }

            return(null);
        }
コード例 #8
0
        private static ParseStringSpanDelegate GetParseStringSpanFn()
        {
            var nullableType = Nullable.GetUnderlyingType(typeof(T));

            if (nullableType == null)
            {
                var typeCode = typeof(T).GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    return(value => value.ParseBoolean());

                case TypeCode.SByte:
                    return(SignedInteger <sbyte> .ParseObject);

                case TypeCode.Byte:
                    return(UnsignedInteger <byte> .ParseObject);

                case TypeCode.Int16:
                    return(SignedInteger <short> .ParseObject);

                case TypeCode.UInt16:
                    return(UnsignedInteger <ushort> .ParseObject);

                case TypeCode.Int32:
                    return(SignedInteger <int> .ParseObject);

                case TypeCode.UInt32:
                    return(UnsignedInteger <uint> .ParseObject);

                case TypeCode.Int64:
                    return(SignedInteger <long> .ParseObject);

                case TypeCode.UInt64:
                    return(UnsignedInteger <ulong> .ParseObject);

                case TypeCode.Single:
                    return(value => MemoryProvider.Instance.ParseFloat(value));

                case TypeCode.Double:
                    return(value => MemoryProvider.Instance.ParseDouble(value));

                case TypeCode.Decimal:
                    return(value => MemoryProvider.Instance.ParseDecimal(value));

                case TypeCode.DateTime:
                    return(value => DateTimeSerializer.ParseShortestXsdDateTime(value.ToString()));

                case TypeCode.Char:
                    return(value => value.Length == 0 ? (char)0 : value.Length == 1 ? value[0] : JsonTypeSerializer.Unescape(value)[0]);
                }

                if (typeof(T) == typeof(Guid))
                {
                    return(value => value.ParseGuid());
                }
                if (typeof(T) == typeof(DateTimeOffset))
                {
                    return(value => DateTimeSerializer.ParseDateTimeOffset(value.ToString()));
                }
                if (typeof(T) == typeof(TimeSpan))
                {
                    return(value => DateTimeSerializer.ParseTimeSpan(value.ToString()));
                }
#if NET6_0
                if (typeof(T) == typeof(DateOnly))
                {
                    return(value => DateOnly.FromDateTime(DateTimeSerializer.ParseShortestXsdDateTime(value.ToString())));
                }
                if (typeof(T) == typeof(TimeOnly))
                {
                    return(value => TimeOnly.FromTimeSpan(DateTimeSerializer.ParseTimeSpan(value.ToString())));
                }
#endif
            }
            else
            {
                var typeCode = nullableType.GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    return(value => value.IsNullOrEmpty()
                            ? (bool?)null
                            : value.ParseBoolean());

                case TypeCode.SByte:
                    return(SignedInteger <sbyte> .ParseNullableObject);

                case TypeCode.Byte:
                    return(UnsignedInteger <byte> .ParseNullableObject);

                case TypeCode.Int16:
                    return(SignedInteger <short> .ParseNullableObject);

                case TypeCode.UInt16:
                    return(UnsignedInteger <ushort> .ParseNullableObject);

                case TypeCode.Int32:
                    return(SignedInteger <int> .ParseNullableObject);

                case TypeCode.UInt32:
                    return(UnsignedInteger <uint> .ParseNullableObject);

                case TypeCode.Int64:
                    return(SignedInteger <long> .ParseNullableObject);

                case TypeCode.UInt64:
                    return(UnsignedInteger <ulong> .ParseNullableObject);

                case TypeCode.Single:
                    return(value => value.IsNullOrEmpty() ? (float?)null : value.ParseFloat());

                case TypeCode.Double:
                    return(value => value.IsNullOrEmpty() ? (double?)null : value.ParseDouble());

                case TypeCode.Decimal:
                    return(value => value.IsNullOrEmpty() ? (decimal?)null : value.ParseDecimal());

                case TypeCode.DateTime:
                    return(value => DateTimeSerializer.ParseShortestNullableXsdDateTime(value.ToString()));

                case TypeCode.Char:
                    return(value => value.IsEmpty ? (char?)null : value.Length == 1 ? value[0] : JsonTypeSerializer.Unescape(value)[0]);
                }

                if (typeof(T) == typeof(TimeSpan?))
                {
                    return(value => DateTimeSerializer.ParseNullableTimeSpan(value.ToString()));
                }
                if (typeof(T) == typeof(Guid?))
                {
                    return(value => value.IsNullOrEmpty() ? (Guid?)null : value.ParseGuid());
                }
                if (typeof(T) == typeof(DateTimeOffset?))
                {
                    return(value => DateTimeSerializer.ParseNullableDateTimeOffset(value.ToString()));
                }
#if NET6_0
                if (typeof(T) == typeof(DateOnly?))
                {
                    return(value => value.IsNullOrEmpty() ? default : DateOnly.FromDateTime(DateTimeSerializer.ParseShortestXsdDateTime(value.ToString())));
                }
                if (typeof(T) == typeof(TimeOnly?))
                {
                    return(value => value.IsNullOrEmpty() ? default : TimeOnly.FromTimeSpan(DateTimeSerializer.ParseTimeSpan(value.ToString())));
                }
#endif
            }

            return(null);
        }
コード例 #9
0
        private static ParseStringSpanDelegate GetParseStringSpanFn()
        {
            var nullableType = Nullable.GetUnderlyingType(typeof(T));

            if (nullableType == null)
            {
                var typeCode = typeof(T).GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    //Lots of kids like to use '1', HTML checkboxes use 'on' as a soft convention
                    return(value =>
                           value.Length == 1 ?
                           value[0] == '1'
                            : value.Length == 2 ?
                           value[0] == 'o' && value[1] == 'n' :
                           value.ParseBoolean());

                case TypeCode.SByte:
                    return(SignedInteger <sbyte> .ParseObject);

                case TypeCode.Byte:
                    return(UnsignedInteger <byte> .ParseObject);

                case TypeCode.Int16:
                    return(SignedInteger <short> .ParseObject);

                case TypeCode.UInt16:
                    return(UnsignedInteger <ushort> .ParseObject);

                case TypeCode.Int32:
                    return(SignedInteger <int> .ParseObject);

                case TypeCode.UInt32:
                    return(UnsignedInteger <uint> .ParseObject);

                case TypeCode.Int64:
                    return(SignedInteger <long> .ParseObject);

                case TypeCode.UInt64:
                    return(UnsignedInteger <ulong> .ParseObject);

                case TypeCode.Single:
                    return(value => MemoryProvider.Instance.ParseFloat(value));

                case TypeCode.Double:
                    return(value => MemoryProvider.Instance.ParseDouble(value));

                case TypeCode.Decimal:
                    return(value => MemoryProvider.Instance.ParseDecimal(value));

                case TypeCode.DateTime:
                    return(value => DateTimeSerializer.ParseShortestXsdDateTime(value.ToString()));

                case TypeCode.Char:
                    return(value => value[0]);
                }

                if (typeof(T) == typeof(Guid))
                {
                    return(value => value.ParseGuid());
                }
                if (typeof(T) == typeof(DateTimeOffset))
                {
                    return(value => DateTimeSerializer.ParseDateTimeOffset(value.ToString()));
                }
                if (typeof(T) == typeof(TimeSpan))
                {
                    return(value => DateTimeSerializer.ParseTimeSpan(value.ToString()));
                }
            }
            else
            {
                var typeCode = nullableType.GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    return(value => value.IsNullOrEmpty() ?
                           (bool?)null
                            : value.Length == 1 ?
                           value[0] == '1'
                            : value.Length == 2 ?
                           value[0] == 'o' && value[1] == 'n' :
                           value.ParseBoolean());

                case TypeCode.SByte:
                    return(SignedInteger <sbyte> .ParseNullableObject);

                case TypeCode.Byte:
                    return(UnsignedInteger <byte> .ParseNullableObject);

                case TypeCode.Int16:
                    return(SignedInteger <short> .ParseNullableObject);

                case TypeCode.UInt16:
                    return(UnsignedInteger <ushort> .ParseNullableObject);

                case TypeCode.Int32:
                    return(SignedInteger <int> .ParseNullableObject);

                case TypeCode.UInt32:
                    return(UnsignedInteger <uint> .ParseNullableObject);

                case TypeCode.Int64:
                    return(SignedInteger <long> .ParseNullableObject);

                case TypeCode.UInt64:
                    return(UnsignedInteger <ulong> .ParseNullableObject);

                case TypeCode.Single:
                    return(value => value.IsNullOrEmpty() ? (float?)null : value.ParseFloat());

                case TypeCode.Double:
                    return(value => value.IsNullOrEmpty() ? (double?)null : value.ParseDouble());

                case TypeCode.Decimal:
                    return(value => value.IsNullOrEmpty() ? (decimal?)null : value.ParseDecimal());

                case TypeCode.DateTime:
                    return(value => DateTimeSerializer.ParseShortestNullableXsdDateTime(value.ToString()));

                case TypeCode.Char:
                    return(value => value.IsEmpty ? (char?)null : value[0]);
                }

                if (typeof(T) == typeof(TimeSpan?))
                {
                    return(value => DateTimeSerializer.ParseNullableTimeSpan(value.ToString()));
                }
                if (typeof(T) == typeof(Guid?))
                {
                    return(value => value.IsNullOrEmpty() ? (Guid?)null : value.ParseGuid());
                }
                if (typeof(T) == typeof(DateTimeOffset?))
                {
                    return(value => DateTimeSerializer.ParseNullableDateTimeOffset(value.ToString()));
                }
            }

            return(null);
        }