public static void BsonCanSerializeNull()
        {
            // Arrange
            Serialization.Test.SerializingAndDeserializingBehaviorOfNull.NullableObject expected = null;

            void ThrowIfObjectsDiffer(DescribedSerializationBase describedSerialization, Serialization.Test.SerializingAndDeserializingBehaviorOfNull.NullableObject deserialized)
            {
                if (describedSerialization is StringDescribedSerialization stringDescribedSerialization)
                {
                    stringDescribedSerialization.SerializedPayload.Should().Be(ObcBsonSerializer.SerializedRepresentationOfNull);
                }
                else if (describedSerialization is BinaryDescribedSerialization binaryDescribedSerialization)
                {
                    binaryDescribedSerialization.SerializedPayload.Should().BeNull();
                }
                else
                {
                    throw new NotSupportedException("This type of described serialization is not supported: " + describedSerialization.GetType().ToStringReadable());
                }

                deserialized.Should().BeNull();
            }

            void ThrowIfStringsDiffer(DescribedSerializationBase describedSerialization, string deserialized)
            {
                deserialized.Should().Be(ObcBsonSerializer.SerializedRepresentationOfNull);
            }

            // Act, Assert
            expected.RoundtripSerializeViaBsonWithCallbackVerification(ThrowIfObjectsDiffer);
            ObcBsonSerializer.SerializedRepresentationOfNull.RoundtripSerializeViaBsonWithCallbackVerification(ThrowIfStringsDiffer);
        }
Beispiel #2
0
        public static void JsonCanSerializeNull()
        {
            // Arrange
            Serialization.Test.SerializingAndDeserializingBehaviorOfNull.NullableObject expected = null;

            void ThrowIfObjectsDiffer(DescribedSerializationBase describedSerialization, Serialization.Test.SerializingAndDeserializingBehaviorOfNull.NullableObject deserialized)
            {
                if (describedSerialization is StringDescribedSerialization stringDescribedSerialization)
                {
                    stringDescribedSerialization.SerializedPayload.Should().Be(NewtonsoftSerializedNullRepresentation);
                }
                else if (describedSerialization is BinaryDescribedSerialization binaryDescribedSerialization)
                {
                    var expectedPayload = NewtonsoftSerializedNullRepresentation.ToBytes(Encoding.UTF8);

                    binaryDescribedSerialization.SerializedPayload.Should().Equal(expectedPayload);
                }
                else
                {
                    throw new NotSupportedException("This type of described serialization is not supported: " + describedSerialization.GetType().ToStringReadable());
                }

                deserialized.Should().BeNull();
            }

            void ThrowIfStringsDiffer(DescribedSerializationBase describedSerialization, string deserialized)
            {
                deserialized.Should().Be(NewtonsoftSerializedNullRepresentation);
            }

            // Act, Assert
            expected.RoundtripSerializeViaJsonWithCallbackVerification(ThrowIfObjectsDiffer);
            NewtonsoftSerializedNullRepresentation.RoundtripSerializeViaJsonWithCallbackVerification(ThrowIfStringsDiffer);
        }
        public static void PropertyBagCanSerializeNull()
        {
            // Arrange
            Serialization.Test.SerializingAndDeserializingBehaviorOfNull.NullableObject expected = null;

            void ThrowIfObjectsDiffer(DescribedSerializationBase describedSerialization, Serialization.Test.SerializingAndDeserializingBehaviorOfNull.NullableObject deserialized)
            {
                describedSerialization.GetSerializedPayloadAsEncodedString().Should().BeNull();
                describedSerialization.GetSerializedPayloadAsEncodedBytes().Should().BeNull();

                deserialized.Should().BeNull();
            }

            // Act
            expected.RoundtripSerializeViaPropertyBagWithCallbackVerification(ThrowIfObjectsDiffer);
        }