Exemple #1
0
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T?value)
        {
            var bsonWriter = context.Writer;

            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var jsonWriter = new BsonJsonWriter(bsonWriter);

                serializer.Serialize(jsonWriter, value);
            }
        }
        public void Should_serialize_with_reader_and_writer()
        {
            var stream = new MemoryStream();

            using (var writer = new BsonJsonWriter(new BsonBinaryWriter(stream)))
            {
                serializer.Serialize(writer, source);

                writer.Flush();
            }

            stream.Position = 0;

            using (var reader = new BsonJsonReader(new BsonBinaryReader(stream)))
            {
                var target = serializer.Deserialize <TestObject>(reader);

                target.Should().BeEquivalentTo(source);
            }
        }
        private static T SerializeAndDeserialize <T>(T value)
        {
            var serializer = JsonSerializer.CreateDefault();

            var stream = new MemoryStream();

            using (var writer = new BsonJsonWriter(new BsonBinaryWriter(stream)))
            {
                serializer.Serialize(writer, value);

                writer.Flush();
            }

            stream.Position = 0;

            using (var reader = new BsonJsonReader(new BsonBinaryReader(stream)))
            {
                return(serializer.Deserialize <T>(reader) !);
            }
        }
        public void Should_write_problematic_object()
        {
            var buggy = new
            {
                a = new
                {
                    iv = 1
                },
                b = new
                {
                    iv = JObject.FromObject(new
                    {
                        lat = 1.0,
                        lon = 3.0
                    })
                }
            };

            var stream = new MemoryStream();

            using (var writer = new BsonJsonWriter(new BsonBinaryWriter(stream)))
            {
                serializer.Serialize(writer, buggy);

                writer.Flush();
            }

            stream.Position = 0;

            using (var reader = new BsonJsonReader(new BsonBinaryReader(stream)))
            {
                var target = serializer.Deserialize(reader, buggy.GetType());

                target.Should().BeEquivalentTo(buggy);
            }
        }
        protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, T value)
        {
            var jsonWriter = new BsonJsonWriter(context.Writer);

            serializer.Serialize(jsonWriter, value);
        }