Example #1
0
        public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            object value = null;
            var valueType = actualType.GetConceptValueType();
			if (valueType == typeof(Guid)) {
				var guidBytes = new byte[16];
				BsonBinarySubType subType;
				bsonReader.ReadBinaryData (out guidBytes, out subType);
				value = new Guid (guidBytes);
			} else if (valueType == typeof(double))
				value = bsonReader.ReadDouble ();
			else if (valueType == typeof(float))
				value = (float)bsonReader.ReadDouble ();
			else if (valueType == typeof(Int32))
				value = bsonReader.ReadInt32 ();
			else if (valueType == typeof(Int64))
				value = bsonReader.ReadInt64 ();
			else if (valueType == typeof(bool))
				value = bsonReader.ReadBoolean ();
			else if (valueType == typeof(string))
				value = bsonReader.ReadString ();
			else if (valueType == typeof(decimal))
				value = decimal.Parse (bsonReader.ReadString ());
            
            var concept = ConceptFactory.CreateConceptInstance(actualType, value);
            return concept;
        }
Example #2
0
        /// <summary>
        /// Convert a string into the desired type
        /// </summary>
        /// <param name="input">the string to parse</param>
        /// <param name="type">the desired type</param>
        /// <returns>value as the desired type</returns>
        public static object ParseTo(this string input, Type type)
        {
            if (type == typeof(Guid)) {
                Guid result;
                if (Guid.TryParse(input, out result)) return result;
                return Guid.Empty;
            }

            if (type.IsConcept())
            {
                var primitiveType = type.GetConceptValueType();
                var primitive = ParseTo(input, primitiveType);
                return ConceptFactory.CreateConceptInstance(type, primitive);
            }

            return Convert.ChangeType(input, type, null);           
        }
Example #3
0
        public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            var underlyingValue = value.GetType().GetProperty("Value").GetValue(value, null);
            var underlyingValueType = nominalType.GetConceptValueType();
			if (underlyingValueType == typeof(Guid)) {
				var guid = (Guid)underlyingValue;
				var guidAsBytes = guid.ToByteArray ();
				bsonWriter.WriteBinaryData (guidAsBytes, BsonBinarySubType.UuidLegacy, GuidRepresentation.CSharpLegacy);
			} else if (underlyingValueType == typeof(double))
				bsonWriter.WriteDouble ((double)underlyingValue);
			else if (underlyingValueType == typeof(float))
				bsonWriter.WriteDouble ((double)underlyingValue);
			else if (underlyingValueType == typeof(Int32))
				bsonWriter.WriteInt32 ((Int32)underlyingValue);
			else if (underlyingValueType == typeof(Int64))
				bsonWriter.WriteInt64 ((Int64)underlyingValue);
			else if (underlyingValueType == typeof(bool))
				bsonWriter.WriteBoolean ((bool)underlyingValue);
			else if (underlyingValueType == typeof(string))
				bsonWriter.WriteString ((string)(underlyingValue ?? string.Empty));
			else if (underlyingValueType == typeof(decimal))
				bsonWriter.WriteString (underlyingValue.ToString());
        }
Example #4
0
        /// <summary>
        /// Convert a string into the desired type.
        /// </summary>
        /// <param name="input">The string to parse.</param>
        /// <param name="type">The desired type.</param>
        /// <returns>Value as the desired type.</returns>
        public static object ParseTo(this string input, Type type)
        {
            if (type == typeof(Guid)) {
                Guid result;
                if (Guid.TryParse(input, out result)) return result;
                return Guid.Empty;
            }

            if (type.IsConcept())
            {
                var primitiveType = type.GetConceptValueType();
                var primitive = ParseTo(input, primitiveType);
                return ConceptFactory.CreateConceptInstance(type, primitive);
            }

            try
            {
                return Convert.ChangeType(input, type, null);
            }
            catch (Exception e) when (e is FormatException || e is OverflowException || e is InvalidCastException)
            {
                throw new ConvertException(input, type, e);
            }
        }
Example #5
0
#pragma warning restore 1591

        static IType GuessType(Type type)
        {
            var t = type.IsConcept() ? type.GetConceptValueType() : type;
            return NHibernateUtil.GuessType(t);
        }