A static class containing BSON utility methods.
 /// <summary>
 /// Returns a string representation of the value.
 /// </summary>
 /// <returns>A string representation of the value.</returns>
 public override string ToString()
 {
     return(BsonUtils.ToHexString(Pack(_timestamp, _machine, _pid, _increment)));
 }
Example #2
0
        /// <summary>
        /// Maps a BsonValue to a .NET value.
        /// </summary>
        /// <param name="bsonValue">The BsonValue.</param>
        /// <param name="options">The BsonTypeMapperOptions.</param>
        /// <returns>The mapped .NET value.</returns>
        public static object MapToDotNetValue(BsonValue bsonValue, BsonTypeMapperOptions options)
        {
            switch (bsonValue.BsonType)
            {
            case BsonType.Array:
                var bsonArray = (BsonArray)bsonValue;
                if (options.MapBsonArrayTo == typeof(BsonArray))
                {
                    return(bsonArray);
                }
                else if (options.MapBsonArrayTo == typeof(object[]))
                {
                    var array = new object[bsonArray.Count];
                    for (int i = 0; i < bsonArray.Count; i++)
                    {
                        array[i] = MapToDotNetValue(bsonArray[i], options);
                    }
                    return(array);
                }
                else if (typeof(IList <object>).IsAssignableFrom(options.MapBsonArrayTo))
                {
                    var list = (IList <object>)Activator.CreateInstance(options.MapBsonArrayTo);
                    for (int i = 0; i < bsonArray.Count; i++)
                    {
                        list.Add(MapToDotNetValue(bsonArray[i], options));
                    }
                    return(list);
                }
                else if (typeof(IList).IsAssignableFrom(options.MapBsonArrayTo))
                {
                    var list = (IList)Activator.CreateInstance(options.MapBsonArrayTo);
                    for (int i = 0; i < bsonArray.Count; i++)
                    {
                        list.Add(MapToDotNetValue(bsonArray[i], options));
                    }
                    return(list);
                }
                else
                {
                    var message = string.Format("A BsonArray can't be mapped to a {0}.", BsonUtils.GetFriendlyTypeName(options.MapBsonArrayTo));
                    throw new NotSupportedException(message);
                }

            case BsonType.Binary:
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
                var bsonBinaryData = (BsonBinaryData)bsonValue;
                if (bsonBinaryData.SubType == BsonBinarySubType.Binary ||
                    bsonBinaryData.SubType == BsonBinarySubType.OldBinary && options.MapOldBinaryToByteArray)
                {
                    return(bsonBinaryData.Bytes);
                }
                else if (bsonBinaryData.SubType == BsonBinarySubType.UuidLegacy || bsonBinaryData.SubType == BsonBinarySubType.UuidStandard)
                {
                    return(bsonBinaryData.ToGuid());
                }
                else
                {
                    return(bsonBinaryData);    // unmapped
                }

#pragma warning restore 618
            case BsonType.Boolean:
                return(bsonValue.AsBoolean);

            case BsonType.DateTime:
                return(bsonValue.ToUniversalTime());

            case BsonType.Document:
                var bsonDocument = (BsonDocument)bsonValue;
                if (options.MapBsonDocumentTo == typeof(BsonDocument))
                {
                    return(bsonDocument);
                }
                else if (typeof(IDictionary <string, object>).IsAssignableFrom(options.MapBsonDocumentTo))
                {
                    var dictionary = (IDictionary <string, object>)Activator.CreateInstance(options.MapBsonDocumentTo);
                    foreach (var element in bsonDocument.Elements)
                    {
                        var mappedValue = MapToDotNetValue(element.Value, options);
                        if (dictionary.ContainsKey(element.Name))
                        {
                            switch (options.DuplicateNameHandling)
                            {
                            case DuplicateNameHandling.Ignore:
                                break;

                            case DuplicateNameHandling.Overwrite:
                            default:
                                dictionary[element.Name] = mappedValue;
                                break;

                            case DuplicateNameHandling.ThrowException:
                                var message = string.Format("Duplicate element name '{0}'.", element.Name);
                                throw new ArgumentOutOfRangeException("bsonValue", message);
                            }
                        }
                        else
                        {
                            dictionary.Add(element.Name, mappedValue);
                        }
                    }
                    return(dictionary);
                }
                else if (typeof(IDictionary).IsAssignableFrom(options.MapBsonDocumentTo))
                {
                    var dictionary = (IDictionary)Activator.CreateInstance(options.MapBsonDocumentTo);
                    foreach (var element in bsonDocument.Elements)
                    {
                        var mappedValue = MapToDotNetValue(element.Value, options);
                        if (dictionary.Contains(element.Name))
                        {
                            switch (options.DuplicateNameHandling)
                            {
                            case DuplicateNameHandling.Ignore:
                                break;

                            case DuplicateNameHandling.Overwrite:
                            default:
                                dictionary[element.Name] = mappedValue;
                                break;

                            case DuplicateNameHandling.ThrowException:
                                var message = string.Format("Duplicate element name '{0}'.", element.Name);
                                throw new ArgumentOutOfRangeException("bsonValue", message);
                            }
                        }
                        else
                        {
                            dictionary.Add(element.Name, mappedValue);
                        }
                    }
                    return(dictionary);
                }
                else
                {
                    var message = string.Format("A BsonDocument can't be mapped to a {0}.", BsonUtils.GetFriendlyTypeName(options.MapBsonArrayTo));
                    throw new NotSupportedException(message);
                }

            case BsonType.Double:
                return(bsonValue.AsDouble);

            case BsonType.Int32:
                return(bsonValue.AsInt32);

            case BsonType.Int64:
                return(bsonValue.AsInt64);

            case BsonType.Null:
                return(null);    // BsonValue.Null maps to C# null

            case BsonType.ObjectId:
                return(bsonValue.AsObjectId);

            case BsonType.String:
                return(bsonValue.AsString);

            case BsonType.JavaScript:
            case BsonType.JavaScriptWithScope:
            case BsonType.MaxKey:
            case BsonType.MinKey:
            case BsonType.RegularExpression:
            case BsonType.Symbol:
            case BsonType.Timestamp:
            case BsonType.Undefined:
            default:
                return(bsonValue);    // unmapped
            }
        }
 private static int GetTimestampFromDateTime(DateTime timestamp)
 {
     return((int)Math.Floor((BsonUtils.ToUniversalTime(timestamp) - BsonConstants.UnixEpoch).TotalSeconds));
 }
 /// <summary>
 /// Converts the BsonDateTime value to a .NET DateTime value in UTC.
 /// </summary>
 /// <returns>A DateTime in UTC.</returns>
 public DateTime ToUniversalTime()
 {
     return(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(_millisecondsSinceEpoch));
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the BsonDateTime class.
 /// </summary>
 /// <param name="value">A DateTime.</param>
 public BsonDateTime(DateTime value)
     : base(BsonType.DateTime)
 {
     _millisecondsSinceEpoch = BsonUtils.ToMillisecondsSinceEpoch(value);
 }
        /// <summary>
        /// Converts the BsonDateTime value to a .NET DateTime value in the local timezone.
        /// </summary>
        /// <returns>A DateTime in the local timezone.</returns>
        public DateTime ToLocalTime()
        {
            var utcDateTime = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(_millisecondsSinceEpoch);

            return(BsonUtils.ToLocalTime(utcDateTime));
        }
Example #7
0
 /// <summary>
 /// Returns a string representation of the value.
 /// </summary>
 /// <returns>A string representation of the value.</returns>
 public override string ToString()
 {
     return(BsonUtils.ToHexString(ToByteArray()));
 }
Example #8
0
 /// <summary>
 /// Converts this BsonValue to a DateTime in local time.
 /// </summary>
 /// <returns>A DateTime.</returns>
 public override DateTime ToLocalTime()
 {
     return(BsonUtils.ToLocalTime(ToUniversalTime()));
 }
 /// <summary>
 /// Returns a string representation of the binary data.
 /// </summary>
 /// <returns>A string representation of the binary data.</returns>
 public override string ToString()
 {
     return(string.Format("{0}:0x{1}", _subType, BsonUtils.ToHexString(_bytes)));
 }
Example #10
0
 /// <summary>
 /// Initializes a new instance of the ObjectId class.
 /// </summary>
 /// <param name="value">The value.</param>
 public ObjectId(string value)
 {
     Unpack(BsonUtils.ParseHexString(value), out _timestamp, out _machine, out _pid, out _increment);
 }