Beispiel #1
0
        public void Explicit_decoding_with_standard_representation_should_work_as_expected()
        {
            GuidMode.Set(GuidRepresentationMode.V3);

            var guid       = new Guid("00112233445566778899aabbccddeeff");
            var bytes      = GuidConverter.ToBytes(guid, GuidRepresentation.Standard);
            var binaryData = new BsonBinaryData(bytes, BsonBinarySubType.UuidStandard);

            var exception = Record.Exception(() => binaryData.ToGuid(GuidRepresentation.Unspecified));

            exception.Should().BeOfType <ArgumentException>();

            foreach (var guidRepresentation in new[] { GuidRepresentation.CSharpLegacy, GuidRepresentation.JavaLegacy, GuidRepresentation.PythonLegacy })
            {
                exception = Record.Exception(() => binaryData.ToGuid(guidRepresentation));
                exception.Should().BeOfType <InvalidOperationException>();
            }

            var result = binaryData.ToGuid();

            result.Should().Be(guid);

            result = binaryData.ToGuid(GuidRepresentation.Standard);
            result.Should().Be(guid);
        }
Beispiel #2
0
        /***************************************************/

        public override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            IBsonReader reader          = context.Reader;
            BsonType    currentBsonType = reader.GetCurrentBsonType();

            switch (currentBsonType)
            {
            case BsonType.Array:
                if (context.DynamicArraySerializer != null)
                {
                    return(context.DynamicArraySerializer.Deserialize(context));
                }
                break;

            case BsonType.Binary:
            {
                BsonBinaryData    bsonBinaryData = reader.ReadBinaryData();
                BsonBinarySubType subType        = bsonBinaryData.SubType;
                if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
                {
                    return(bsonBinaryData.ToGuid());
                }
                break;
            }

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

            case BsonType.DateTime:
                return(new BsonDateTime(reader.ReadDateTime()).ToUniversalTime());

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

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

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

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

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

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

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

            case BsonType.String:
                return(reader.ReadString());
            }

            Engine.Reflection.Compute.RecordError($"ObjectSerializer does not support BSON type '{currentBsonType}'.");
            return(null);
        }
Beispiel #3
0
        public void Explicit_decoding_with_python_legacy_representation_should_work_as_expected()
        {
            GuidMode.Set(GuidRepresentationMode.V3);

            var bytes      = BsonUtils.ParseHexString("00112233445566778899aabbccddeeff");
            var binaryData = new BsonBinaryData(bytes, BsonBinarySubType.UuidLegacy);

            var exception = Record.Exception(() => binaryData.ToGuid());

            exception.Should().BeOfType <InvalidOperationException>();

            exception = Record.Exception(() => binaryData.ToGuid(GuidRepresentation.Standard));
            exception.Should().BeOfType <InvalidOperationException>();

            exception = Record.Exception(() => binaryData.ToGuid(GuidRepresentation.Unspecified));
            exception.Should().BeOfType <ArgumentException>();

            var result = binaryData.ToGuid(GuidRepresentation.PythonLegacy);

            result.Should().Be(new Guid("00112233445566778899aabbccddeeff"));
        }
Beispiel #4
0
        private static BsonBinaryData ConvertBinaryData(
            BsonBinaryData binary
            )
        {
            if (binary.SubType == BsonBinarySubType.Uuid || binary.SubType == BsonBinarySubType.UuidLegacy)
            {
                if (binary.GuidRepresentation != toRepresentation)
                {
                    var guid = binary.ToGuid();
                    return(new BsonBinaryData(guid, toRepresentation));
                }
            }

            return(binary);
        }
        private static BsonBinaryData ConvertBinaryData(
            BsonBinaryData binary
        ) {
            if (binary.SubType == BsonBinarySubType.Uuid || binary.SubType == BsonBinarySubType.UuidLegacy) {
                if (binary.GuidRepresentation != toRepresentation) {
                    var guid = binary.ToGuid();
                    return new BsonBinaryData(guid, toRepresentation);
                }
            }

            return binary;
        }