From f96529dcf03fd3239c59bb8e93bf5d3981731563 Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Sun, 26 Nov 2017 16:35:02 +0100 Subject: [PATCH] Json Writer fixed --- .../MongoDb/BsonJsonWriter.cs | 5 +++ .../MongoDb/BsonConverterTests.cs | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonJsonWriter.cs b/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonJsonWriter.cs index b4e923381..7a39550e7 100644 --- a/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonJsonWriter.cs +++ b/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonJsonWriter.cs @@ -23,6 +23,11 @@ namespace Squidex.Infrastructure.MongoDb this.bsonWriter = bsonWriter; } + public override void WritePropertyName(string name) + { + bsonWriter.WriteName(name.EscapeJson()); + } + public override void WritePropertyName(string name, bool escape) { bsonWriter.WriteName(name.EscapeJson()); diff --git a/tests/Squidex.Infrastructure.Tests/MongoDb/BsonConverterTests.cs b/tests/Squidex.Infrastructure.Tests/MongoDb/BsonConverterTests.cs index b9732cceb..9dcce8c5a 100644 --- a/tests/Squidex.Infrastructure.Tests/MongoDb/BsonConverterTests.cs +++ b/tests/Squidex.Infrastructure.Tests/MongoDb/BsonConverterTests.cs @@ -126,6 +126,44 @@ namespace Squidex.Infrastructure.MongoDb target.ShouldBeEquivalentTo(source); } + [Fact] + 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.ShouldBeEquivalentTo(buggy); + } + } + [Fact] public void Should_serialize_with_reader_and_writer() {