public void SerializeAndDeserializeToTheSameObject(double originalValue) { Serializer <double> serializer = new DoubleSerializer(); byte[] serializedValue = serializer.Serialize(originalValue); double deserializedValue = serializer.Deserialize(serializedValue); Assert.Equal(originalValue, deserializedValue); }
public void IsBigEndian() { var buffer = new byte[] { 23, 0, 0, 0, 0, 0, 0, 0 }; var value = BitConverter.ToDouble(buffer, 0); var data = new DoubleSerializer().Serialize(null, value); Assert.Equal(23, data[7]); Assert.Equal(0, data[0]); }
private Dictionary <Type, ISerializer> GetDefaultSerializers() { Dictionary <Type, ISerializer> tmp = new Dictionary <Type, ISerializer>(); tmp[typeof(byte)] = new ByteSerializer(); tmp[typeof(sbyte)] = new SByteSerializer(); tmp[typeof(bool)] = new BooleanSerializer(); tmp[typeof(short)] = new ShortSerializer(); tmp[typeof(ushort)] = new UShortSerializer(); tmp[typeof(int)] = new IntSerializer(); tmp[typeof(uint)] = new UIntSerializer(); tmp[typeof(long)] = new LongSerializer(); tmp[typeof(ulong)] = new ULongSerializer(); tmp[typeof(float)] = new FloatSerializer(); tmp[typeof(double)] = new DoubleSerializer(); tmp[typeof(string)] = new StringSerializer(this); tmp[typeof(Array)] = new ArraySerializer(this); return(tmp); }
public WKTSerializers(ISerializerStorage serializerStorage, IValueTypeObjectsDictionary objectCache) { Boolean = new BooleanSerializer(serializerStorage); Byte = new ByteSerializer(serializerStorage); Char = new CharSerializer(serializerStorage); DateTime = new DateTimeSerializer(serializerStorage); DateTimeOffset = new DateTimeOffsetSerializer(serializerStorage); Decimal = new DecimalSerializer(serializerStorage); Double = new DoubleSerializer(serializerStorage); Guid = new GuidSerializer(serializerStorage); Int16 = new Int16Serializer(serializerStorage); Int32 = new Int32Serializer(serializerStorage); Int64 = new Int64Serializer(serializerStorage); Object = new ObjectSerializer(serializerStorage); SByte = new SByteSerializer(serializerStorage); Single = new SingleSerializer(serializerStorage); String = new StringSerializer(serializerStorage, objectCache); TimeSpan = new TimeSpanSerializer(serializerStorage); UInt16 = new UInt16Serializer(serializerStorage); UInt32 = new UInt32Serializer(serializerStorage); }
private ClassInfo <T> RegisterInternal <T>() { if (ClassInfo <T> .Instance != null) { return(ClassInfo <T> .Instance); } var t = typeof(T); var props = t.GetProperties( BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty); var serializers = new List <FastCall <T> >(); for (var i = 0; i < props.Length; i++) { var property = props[i]; var propertyType = property.PropertyType; var elementType = propertyType.IsArray ? propertyType.GetElementType() : propertyType; var callType = propertyType.IsArray ? CallType.Array : CallType.Basic; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List <>)) { elementType = propertyType.GetGenericArguments()[0]; callType = CallType.List; } var getMethod = property.GetGetMethod(); var setMethod = property.GetSetMethod(); if (getMethod == null || setMethod == null) { continue; } FastCall <T> serialzer = null; if (propertyType.IsEnum) { var underlyingType = Enum.GetUnderlyingType(propertyType); if (underlyingType == typeof(byte)) { serialzer = new EnumByteSerializer <T>(property, propertyType); } else if (underlyingType == typeof(int)) { serialzer = new EnumIntSerializer <T>(property, propertyType); } else { throw new InvalidTypeException("Not supported enum underlying type: " + underlyingType.Name); } } else if (elementType == typeof(string)) { serialzer = new StringSerializer <T>(_maxStringLength); } else if (elementType == typeof(bool)) { serialzer = new BoolSerializer <T>(); } else if (elementType == typeof(byte)) { serialzer = new ByteSerializer <T>(); } else if (elementType == typeof(sbyte)) { serialzer = new SByteSerializer <T>(); } else if (elementType == typeof(short)) { serialzer = new ShortSerializer <T>(); } else if (elementType == typeof(ushort)) { serialzer = new UShortSerializer <T>(); } else if (elementType == typeof(int)) { serialzer = new IntSerializer <T>(); } else if (elementType == typeof(uint)) { serialzer = new UIntSerializer <T>(); } else if (elementType == typeof(long)) { serialzer = new LongSerializer <T>(); } else if (elementType == typeof(ulong)) { serialzer = new ULongSerializer <T>(); } else if (elementType == typeof(float)) { serialzer = new FloatSerializer <T>(); } else if (elementType == typeof(double)) { serialzer = new DoubleSerializer <T>(); } else if (elementType == typeof(char)) { serialzer = new CharSerializer <T>(); } else if (elementType == typeof(IPEndPoint)) { serialzer = new IPEndPointSerializer <T>(); } else { CustomType customType; _registeredTypes.TryGetValue(elementType, out customType); if (customType != null) { serialzer = customType.Get <T>(); } } if (serialzer != null) { serialzer.Init(getMethod, setMethod, callType); serializers.Add(serialzer); } else { throw new InvalidTypeException("Unknown property type: " + propertyType.FullName); } } ClassInfo <T> .Instance = new ClassInfo <T>(serializers); return(ClassInfo <T> .Instance); }
/// <summary> /// Initializes a new instance of the <see cref="IntSchema"/> class. /// </summary> public DoubleSchema() { MinValue = double.MinValue; MaxValue = double.MaxValue; Serializer = new DoubleSerializer(); }
private ClassInfo <T> RegisterInternal <T>() { if (ClassInfo <T> .Instance != null) { return(ClassInfo <T> .Instance); } Type t = typeof(T); PropertyInfo[] props = t.GetProperties( BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty); List <FastCall <T> > serializers = new List <FastCall <T> >(); for (int i = 0; i < props.Length; i++) { PropertyInfo property = props[i]; Type propertyType = property.PropertyType; Type elementType = propertyType.IsArray ? propertyType.GetElementType() : propertyType; CallType callType = propertyType.IsArray ? CallType.Array : CallType.Basic; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List <>)) { elementType = propertyType.GetGenericArguments()[0]; callType = CallType.List; } // Note from Cod: Required to get it to build // TODO: Fix this /*if (Attribute.IsDefined(property, typeof(IgnoreDataMemberAttribute))) * continue;*/ MethodInfo getMethod = property.GetGetMethod(); MethodInfo setMethod = property.GetSetMethod(); if (getMethod == null || setMethod == null) { continue; } FastCall <T> serialzer = null; if (propertyType.IsEnum) { Type underlyingType = Enum.GetUnderlyingType(propertyType); if (underlyingType == typeof(byte)) { serialzer = new EnumByteSerializer <T>(property, propertyType); } else if (underlyingType == typeof(int)) { serialzer = new EnumIntSerializer <T>(property, propertyType); } else { throw new InvalidTypeException("Not supported enum underlying type: " + underlyingType.Name); } } else if (elementType == typeof(string)) { serialzer = new StringSerializer <T>(_maxStringLength); } else if (elementType == typeof(bool)) { serialzer = new BoolSerializer <T>(); } else if (elementType == typeof(byte)) { serialzer = new ByteSerializer <T>(); } else if (elementType == typeof(sbyte)) { serialzer = new SByteSerializer <T>(); } else if (elementType == typeof(short)) { serialzer = new ShortSerializer <T>(); } else if (elementType == typeof(ushort)) { serialzer = new UShortSerializer <T>(); } else if (elementType == typeof(int)) { serialzer = new IntSerializer <T>(); } else if (elementType == typeof(uint)) { serialzer = new UIntSerializer <T>(); } else if (elementType == typeof(long)) { serialzer = new LongSerializer <T>(); } else if (elementType == typeof(ulong)) { serialzer = new ULongSerializer <T>(); } else if (elementType == typeof(float)) { serialzer = new FloatSerializer <T>(); } else if (elementType == typeof(double)) { serialzer = new DoubleSerializer <T>(); } else if (elementType == typeof(char)) { serialzer = new CharSerializer <T>(); } else if (elementType == typeof(IPEndPoint)) { serialzer = new IPEndPointSerializer <T>(); } else { _registeredTypes.TryGetValue(elementType, out CustomType customType); if (customType != null) { serialzer = customType.Get <T>(); } } if (serialzer != null) { serialzer.Init(getMethod, setMethod, callType); serializers.Add(serialzer); } else { throw new InvalidTypeException("Unknown property type: " + propertyType.FullName); } } ClassInfo <T> .Instance = new ClassInfo <T>(serializers); return(ClassInfo <T> .Instance); }
public void ShouldThrowIfDeserializingLessThanEightBytes(byte[] data) { Serializer <double> serializer = new DoubleSerializer(); Assert.Throws <ArgumentOutOfRangeException>(() => serializer.Deserialize(data)); }
public void ShouldThrowIfDeserializingNull() { Serializer <double> serializer = new DoubleSerializer(); Assert.Throws <ArgumentNullException>(() => serializer.Deserialize(null)); }