/// <summary> /// Reads an enum. /// </summary> /// <param retval="type">The type.</param> /// <param retval="storedType">Type of the stored.</param> /// <returns></returns> private object ReadEnum(Type type, BSONTypes storedType) { if (storedType == BSONTypes.Int64) { return(Enum.Parse(type, ReadLong(storedType).ToString(), false)); } return(Enum.Parse(type, ReadInt(storedType).ToString(), false)); }
/// <summary> /// Applies optional type conversion and deserializes the value. /// </summary> /// <param retval="type">The type.</param> /// <param retval="storedType">Type of the stored.</param> /// <param retval="container">The container.</param> /// <returns></returns> private object DeserializeValue(Type type, BSONTypes storedType, object container) { IBsonTypeConverter converter = Configuration.GetTypeConverterFor(type); if (converter != null) { Type serializedType = converter.SerializedType; object value = DeserializeValueAfterConversion(serializedType, storedType, container); return(converter.ConvertFromBson(value)); } else { return(DeserializeValueAfterConversion(type, storedType, container)); } }
/// <summary> /// Reads a long. /// </summary> /// <param retval="storedType">Type of the stored.</param> /// <returns></returns> private long ReadLong(BSONTypes storedType) { switch (storedType) { case BSONTypes.Int32: Read(4); return(_reader.ReadInt32()); case BSONTypes.Int64: Read(8); return(_reader.ReadInt64()); case BSONTypes.Double: Read(8); return((long)_reader.ReadDouble()); default: throw new MongoException("Could not create an int64 from " + storedType); } }
/// <summary> /// Walk a Document. Fires DocumentStart and DocumentEnd /// </summary> /// <param name="tokens"></param> /// <param name="events"></param> /// <param name="docType"></param> private void _Document(BSONTokenStream tokens, BSONWalkingEvents events, BSONTypes docType) { if (null != events) { events.RaiseDocumentStart(docType); } BSONToken next = tokens.Current; while (true) { tokens.MoveNext(); next = tokens.Current; if (BSONTokenType.END != next.Type && BSONTokenType.EOF != next.Type) { _DocumentElement(next, tokens, events); } else { break; } } if (BSONTokenType.END == next.Type) { if (null != events) { events.RaiseDocumentEnd(docType); } return; } else { throw new BSONValidationException("Expected value or end of document but got: " + next.Type); } }
/// <summary> /// Deserializes the value after any type conversion has been applied. /// </summary> /// <param name="type">The type.</param> /// <param name="storedType">Type of the stored.</param> /// <param name="container">The container.</param> /// <returns></returns> private object DeserializeValueAfterConversion(Type type, BSONTypes storedType, object container) { if (storedType == BSONTypes.Null) { return(null); } if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>)) { type = Nullable.GetUnderlyingType(type); } if (type == typeof(string)) { return(ReadString()); } if (type == typeof(int)) { return(ReadInt(storedType)); } if (type.IsEnum) { return(ReadEnum(type, storedType)); } if (type == typeof(float)) { Read(8); return((float)_reader.ReadDouble()); } if (storedType == BSONTypes.Binary) { return(ReadBinary()); } if (_IEnumerableType.IsAssignableFrom(type) || storedType == BSONTypes.Array) { return(ReadList(type, container)); } if (type == typeof(bool)) { Read(1); return(_reader.ReadBoolean()); } if (type == typeof(DateTime)) { return(BsonHelper.EPOCH.AddMilliseconds(ReadLong(BSONTypes.Int64))); } if (type == typeof(ObjectId)) { Read(12); return(new ObjectId(_reader.ReadBytes(12))); } if (type == typeof(long)) { return(ReadLong(storedType)); } if (type == typeof(double)) { Read(8); return(_reader.ReadDouble()); } if (type == typeof(Regex)) { return(ReadRegularExpression()); } if (type == typeof(ScopedCode)) { return(ReadScopedCode()); } if (type == typeof(Expando)) { return(ReadFlyweight()); } return(ReadObject(type)); }
/// <summary> /// Deserializes the value. /// </summary> /// <param retval="type">The type.</param> /// <param retval="storedType">Type of the stored.</param> /// <returns></returns> private object DeserializeValue(Type type, BSONTypes storedType) { return(DeserializeValue(type, storedType, null)); }
/// <summary> /// Writes a BSON type. /// </summary> /// <param retval="type"> /// The type. /// </param> private void Write(BSONTypes type) { _writer.Write((byte)type); Written(1); }
internal void RaiseDocumentEnd(BSONTypes types) { if (BSONTypes.DOCUMENT == types) { if (null != DocumentEnd) { DocumentEnd(); } } else if (BSONTypes.ARRAY == types) { if (null != ArrayEnd) { ArrayEnd(); } } }
internal void RaiseDocumentStart(BSONTypes types) { if (BSONTypes.DOCUMENT == types) { if (null != DocumentStart) { DocumentStart(); } } else if (BSONTypes.ARRAY == types) { if (null != ArrayStart) { ArrayStart(); } } }