Exemplo n.º 1
0
        static bool GetParameterSerializer(TypeInfo type, out IParameterSerializer serializer)
        {
            if (typeof(ITestParameter).GetTypeInfo().IsAssignableFrom(type))
            {
                serializer = null;
                return(true);
            }

            if (type.Equals(typeof(bool)))
            {
                serializer = new BooleanSerializer();
                return(true);
            }
            else if (type.Equals(typeof(int)))
            {
                serializer = new IntegerSerializer();
                return(true);
            }
            else if (type.Equals(typeof(string)))
            {
                serializer = new StringSerializer();
                return(true);
            }
            else if (type.IsEnum)
            {
                var serializerType = typeof(EnumSerializer <>).MakeGenericType(type.AsType());
                serializer = (IParameterSerializer)Activator.CreateInstance(serializerType);
                return(true);
            }

            serializer = null;
            return(false);
        }
Exemplo n.º 2
0
 static SerializerFactory()
 {
     _autoSerializer             = new AutoSerializer();
     _booleanSerializer          = new BooleanSerializer();
     _enumNameSerializer         = new EnumNameSerializer();
     _enumValueSerializer        = new EnumValueSerializer();
     _jsonSerializableSerializer = new JsonSerializableSerializer();
     _numericSerializer          = new NumericSerializer();
     _registeredObjectSerializer = new RegisteredObjectSerializer();
     _stringSerializer           = new StringSerializer();
     _schemaSerializer           = new SchemaSerializer();
     _uriSerializer = new UriSerializer();
     _library       = new Dictionary <Type, ISerializer>
     {
         { typeof(sbyte), _numericSerializer },
         { typeof(byte), _numericSerializer },
         { typeof(char), _numericSerializer },
         { typeof(short), _numericSerializer },
         { typeof(ushort), _numericSerializer },
         { typeof(int), _numericSerializer },
         { typeof(uint), _numericSerializer },
         { typeof(long), _numericSerializer },
         { typeof(ulong), _numericSerializer },
         { typeof(float), _numericSerializer },
         { typeof(double), _numericSerializer },
         { typeof(decimal), _numericSerializer },
         { typeof(bool), _booleanSerializer },
         { typeof(string), _stringSerializer },
         { typeof(Uri), _uriSerializer }
     };
 }
Exemplo n.º 3
0
        public void SerializeAndDeserializeToTheSameObject(bool originalValue)
        {
            Serializer <bool> serializer = new BooleanSerializer();

            byte[] serializedValue   = serializer.Serialize(originalValue);
            bool   deserializedValue = serializer.Deserialize(serializedValue);

            Assert.Equal(originalValue, deserializedValue);
        }
        public static AggregationExpression Translate(TranslationContext context, MemberExpression expression)
        {
            if (expression.Member is PropertyInfo propertyInfo && propertyInfo.Is(NullableProperty.HasValue))
            {
                var containerExpression  = expression.Expression;
                var containerTranslation = ExpressionToAggregationExpressionTranslator.Translate(context, containerExpression);
                var ast        = AstExpression.Ne(containerTranslation.Ast, BsonNull.Value);
                var serializer = new BooleanSerializer();
                return(new AggregationExpression(expression, ast, serializer));
            }

            throw new ExpressionNotSupportedException(expression);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
 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);
 }
Exemplo n.º 7
0
        public void ThrowIfDeserializingLessThanOneByte()
        {
            Serializer <bool> serializer = new BooleanSerializer();

            Assert.Throws <ArgumentOutOfRangeException>(() => serializer.Deserialize(new byte[] { }));
        }
Exemplo n.º 8
0
        public void ThrowIfDeserializingNull()
        {
            Serializer <bool> serializer = new BooleanSerializer();

            Assert.Throws <ArgumentNullException>(() => serializer.Deserialize(null));
        }