MongoDB.Bson.Serialization.Serializers.GuidSerializer.Deserialize C# (CSharp) Метод

Deserialize() публичный Метод

Deserializes an object from a BsonReader.
public Deserialize ( MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options ) : object
bsonReader MongoDB.Bson.IO.BsonReader The BsonReader.
nominalType System.Type The nominal type of the object.
actualType System.Type The actual type of the object.
options IBsonSerializationOptions The serialization options.
Результат object
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(Guid));

            var bsonType = bsonReader.GetCurrentBsonType();
            string message;
            switch (bsonType)
            {
                case BsonType.Binary:
                    var binaryData = bsonReader.ReadBinaryData();
                    var bytes = binaryData.Bytes;
                    var subType = binaryData.SubType;
                    var guidRepresentation = binaryData.GuidRepresentation;
                    if (bytes.Length != 16)
                    {
                        message = string.Format("Expected length to be 16, not {0}.", bytes.Length);
                        throw new Exception(message);
                    }
                    if (subType != BsonBinarySubType.UuidStandard && subType != BsonBinarySubType.UuidLegacy)
                    {
                        message = string.Format("Expected binary sub type to be UuidStandard or UuidLegacy, not {0}.", subType);
                        throw new Exception(message);
                    }
                    if (guidRepresentation == GuidRepresentation.Unspecified)
                    {
                        throw new BsonSerializationException("GuidSerializer cannot deserialize a Guid when GuidRepresentation is Unspecified.");
                    }
                    return GuidConverter.FromBytes(bytes, guidRepresentation);
                case BsonType.String:
                    return new Guid(bsonReader.ReadString());
                default:
                    message = string.Format("Cannot deserialize Guid from BsonType {0}.", bsonType);
                    throw new Exception(message);
            }
        }

Usage Example

Пример #1
0
        // public methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <param name="args">The deserialization args.</param>
        /// <returns>A deserialized value.</returns>
        public override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var bsonReader = context.Reader;

            var bsonType = bsonReader.GetCurrentBsonType();

            switch (bsonType)
            {
            case BsonType.Array:
                if (context.DynamicArraySerializer != null)
                {
                    return(context.DynamicArraySerializer.Deserialize(context));
                }
                goto default;

            case BsonType.Binary:
#pragma warning disable 618
                if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && _guidRepresentation == GuidRepresentation.Unspecified)
                {
                    var binaryData = bsonReader.ReadBinaryData();
                    var subType    = binaryData.SubType;
                    if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
                    {
                        return(binaryData.ToGuid());
                    }
                }
                else
                {
                    var binaryDataBookmark = bsonReader.GetBookmark();
                    var binaryData         = bsonReader.ReadBinaryDataWithGuidRepresentationUnspecified();
                    var subType            = binaryData.SubType;
                    if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
                    {
                        bsonReader.ReturnToBookmark(binaryDataBookmark);
                        return(_guidSerializer.Deserialize(context));
                    }
                }
#pragma warning restore 618
                goto default;

            case BsonType.Boolean:
                return(bsonReader.ReadBoolean());

            case BsonType.DateTime:
                var millisecondsSinceEpoch = bsonReader.ReadDateTime();
                var bsonDateTime           = new BsonDateTime(millisecondsSinceEpoch);
                return(bsonDateTime.ToUniversalTime());

            case BsonType.Decimal128:
                return(bsonReader.ReadDecimal128());

            case BsonType.Document:
                return(DeserializeDiscriminatedValue(context, args));

            case BsonType.Double:
                return(bsonReader.ReadDouble());

            case BsonType.Int32:
                return(bsonReader.ReadInt32());

            case BsonType.Int64:
                return(bsonReader.ReadInt64());

            case BsonType.Null:
                bsonReader.ReadNull();
                return(null);

            case BsonType.ObjectId:
                return(bsonReader.ReadObjectId());

            case BsonType.String:
                return(bsonReader.ReadString());

            default:
                var message = string.Format("ObjectSerializer does not support BSON type '{0}'.", bsonType);
                throw new FormatException(message);
            }
        }