コード例 #1
0
        public static string Serialize(object value)
        {
            if (value == null)
            {
                return(null);
            }

            var serializable = value as IXunitSerializable;

            if (serializable != null)
            {
                var info = new XunitSerializationInfo();
                serializable.Serialize(info);
                return(info.ToSerializedString());
            }

            var charData = value as char?;

            if (charData != null)
            {
                return(((ushort)charData.GetValueOrDefault()).ToString(CultureInfo.InvariantCulture));
            }

            var stringData = value as string;

            if (stringData != null)
            {
                return(ToBase64(stringData));
            }

            var byteData = value as byte?;

            if (byteData != null)
            {
                return(byteData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture));
            }

            var sbyteData = value as sbyte?;

            if (sbyteData != null)
            {
                return(sbyteData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture));
            }

            var ushortData = value as ushort?;

            if (ushortData != null)
            {
                return(ushortData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture));
            }

            var shortData = value as short?;

            if (shortData != null)
            {
                return(shortData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture));
            }

            var uintData = value as uint?;

            if (uintData != null)
            {
                return(uintData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture));
            }

            var intData = value as int?;

            if (intData != null)
            {
                return(intData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture));
            }

            var ulongData = value as ulong?;

            if (ulongData != null)
            {
                return(ulongData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture));
            }

            var longData = value as long?;

            if (longData != null)
            {
                return(longData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture));
            }

            var floatData = value as float?;

            if (floatData != null)
            {
                var info     = new XunitSerializationInfo();
                var arraySer = new ArraySerializer(BitConverter.GetBytes(floatData.GetValueOrDefault()));
                arraySer.Serialize(info);
                return(info.ToSerializedString());
            }

            var doubleData = value as double?;

            if (doubleData != null)
            {
                var info     = new XunitSerializationInfo();
                var arraySer = new ArraySerializer(BitConverter.GetBytes(doubleData.GetValueOrDefault()));
                arraySer.Serialize(info);
                return(info.ToSerializedString());
            }

            var decimalData = value as decimal?;

            if (decimalData != null)
            {
                return(decimalData.GetValueOrDefault().ToString(CultureInfo.InvariantCulture));
            }

            var booleanData = value as bool?;

            if (booleanData != null)
            {
                return(booleanData.GetValueOrDefault().ToString());
            }

            var datetimeData = value as DateTime?;

            if (datetimeData != null)
            {
                return(datetimeData.GetValueOrDefault().ToString("o", CultureInfo.InvariantCulture));  // Round-trippable format
            }
            var datetimeoffsetData = value as DateTimeOffset?;

            if (datetimeoffsetData != null)
            {
                return(datetimeoffsetData.GetValueOrDefault().ToString("o", CultureInfo.InvariantCulture));  // Round-trippable format
            }
            var typeData = value as Type;

            if (typeData != null)
            {
                return(SerializationHelper.GetTypeNameForSerialization(typeData));
            }

            var valueType = value.GetType();

            if (valueType.IsEnum())
            {
                if (!valueType.IsFromLocalAssembly())
                {
                    throw new ArgumentException($"We cannot serialize enum {valueType.FullName}.{value} because it lives in the GAC", nameof(value));
                }
                return(value.ToString());
            }

            var arrayData = value as Array;

            if (arrayData != null)
            {
                var info     = new XunitSerializationInfo();
                var arraySer = new ArraySerializer(arrayData);
                arraySer.Serialize(info);
                return(info.ToSerializedString());
            }

            throw new ArgumentException($"We don't know how to serialize type {valueType.FullName}", nameof(value));
        }
コード例 #2
0
        public static object Deserialize(Type type, string serializedValue)
        {
            if (serializedValue == null)
            {
                return(null);
            }

            if (typeof(IXunitSerializable).IsAssignableFrom(type))
            {
                return(DeserializeSerializable(type, serializedValue));
            }

            if (type == typeof(char?) || type == typeof(char))
            {
                return((char)ushort.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(string))
            {
                return(FromBase64(serializedValue));
            }

            if (type == typeof(byte?) || type == typeof(byte))
            {
                return(byte.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(sbyte?) || type == typeof(sbyte))
            {
                return(sbyte.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(short?) || type == typeof(short))
            {
                return(short.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(ushort?) || type == typeof(ushort))
            {
                return(ushort.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(int?) || type == typeof(int))
            {
                return(int.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(uint?) || type == typeof(uint))
            {
                return(uint.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(long?) || type == typeof(long))
            {
                return(long.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(ulong?) || type == typeof(ulong))
            {
                return(ulong.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(float?) || type == typeof(float))
            {
                var    arrSer = (ArraySerializer)DeserializeSerializable(typeof(ArraySerializer), serializedValue);
                byte[] bytes  = (byte[])arrSer.ArrayData;
                return(BitConverter.ToSingle(bytes, 0));
            }

            if (type == typeof(double?) || type == typeof(double))
            {
                var    arrSer = (ArraySerializer)DeserializeSerializable(typeof(ArraySerializer), serializedValue);
                byte[] bytes  = (byte[])arrSer.ArrayData;
                return(BitConverter.ToDouble(bytes, 0));
            }

            if (type == typeof(decimal?) || type == typeof(decimal))
            {
                return(decimal.Parse(serializedValue, CultureInfo.InvariantCulture));
            }

            if (type == typeof(bool?) || type == typeof(bool))
            {
                return(bool.Parse(serializedValue));
            }

            if (type == typeof(DateTime?) || type == typeof(DateTime))
            {
                var styles = serializedValue.EndsWith("Z", StringComparison.Ordinal) ? DateTimeStyles.AdjustToUniversal : DateTimeStyles.None;
                return(DateTime.Parse(serializedValue, CultureInfo.InvariantCulture, styles));
            }

            if (type == typeof(DateTimeOffset?) || type == typeof(DateTimeOffset))
            {
                var styles = serializedValue.EndsWith("Z", StringComparison.Ordinal) ? DateTimeStyles.AdjustToUniversal : DateTimeStyles.None;
                return(DateTimeOffset.Parse(serializedValue, CultureInfo.InvariantCulture, styles));
            }

            if (type == typeof(Type))
            {
                return(SerializationHelper.GetType(serializedValue));
            }

            if (type.IsEnum() || type.IsNullableEnum())
            {
                return(Enum.Parse(type.UnwrapNullable(), serializedValue));
            }

            if (type.IsArray)
            {
                var arrSer = (ArraySerializer)DeserializeSerializable(typeof(ArraySerializer), serializedValue);
                return(arrSer.ArrayData);
            }

            throw new ArgumentException("We don't know how to de-serialize type " + type.FullName, nameof(serializedValue));
        }