From 3c8b7a082f418f036a81c0e29375af25c2fe0f36 Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Mon, 12 Feb 2018 12:16:16 +0100 Subject: [PATCH] Serialization fixed. --- .../EventSourcing/MongoEvent.cs | 2 +- .../MongoDb/BsonJsonSerializer.cs | 35 +++++++++++++++---- .../MongoDb/JTokenSerializer.cs | 33 +++++++++++++---- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/Squidex.Infrastructure.MongoDb/EventSourcing/MongoEvent.cs b/src/Squidex.Infrastructure.MongoDb/EventSourcing/MongoEvent.cs index 62d15ca20..eb1f97f86 100644 --- a/src/Squidex.Infrastructure.MongoDb/EventSourcing/MongoEvent.cs +++ b/src/Squidex.Infrastructure.MongoDb/EventSourcing/MongoEvent.cs @@ -26,7 +26,7 @@ namespace Squidex.Infrastructure.EventSourcing public static MongoEvent FromEventData(EventData data) { - return new MongoEvent { Type = data.Type, Metadata = data.Metadata, Payload = data.ToString() }; + return new MongoEvent { Type = data.Type, Metadata = data.Metadata, Payload = data.Payload.ToString() }; } public EventData ToEventData() diff --git a/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonJsonSerializer.cs b/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonJsonSerializer.cs index 21c05cf38..d06df02c9 100644 --- a/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonJsonSerializer.cs +++ b/src/Squidex.Infrastructure.MongoDb/MongoDb/BsonJsonSerializer.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using Newtonsoft.Json; @@ -22,18 +23,38 @@ namespace Squidex.Infrastructure.MongoDb this.serializer = serializer; } - protected override T DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) + public override T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { - var jsonReader = new BsonJsonReader(context.Reader); + var bsonReader = context.Reader; - return serializer.Deserialize(jsonReader); + if (bsonReader.GetCurrentBsonType() == BsonType.Null) + { + bsonReader.ReadNull(); + + return null; + } + else + { + var jsonReader = new BsonJsonReader(bsonReader); + + return serializer.Deserialize(jsonReader); + } } - protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, T value) + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T value) { - var jsonWriter = new BsonJsonWriter(context.Writer); - - serializer.Serialize(jsonWriter, value); + var bsonWriter = context.Writer; + + if (value == null) + { + bsonWriter.WriteNull(); + } + else + { + var jsonWriter = new BsonJsonWriter(bsonWriter); + + serializer.Serialize(jsonWriter, value); + } } } } diff --git a/src/Squidex.Infrastructure.MongoDb/MongoDb/JTokenSerializer.cs b/src/Squidex.Infrastructure.MongoDb/MongoDb/JTokenSerializer.cs index fbb1039e0..755a4ae25 100644 --- a/src/Squidex.Infrastructure.MongoDb/MongoDb/JTokenSerializer.cs +++ b/src/Squidex.Infrastructure.MongoDb/MongoDb/JTokenSerializer.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using Newtonsoft.Json.Linq; @@ -15,18 +16,38 @@ namespace Squidex.Infrastructure.MongoDb { public static readonly JTokenSerializer Instance = new JTokenSerializer(); - protected override T DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) + public override T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { - var jsonReader = new BsonJsonReader(context.Reader); + var bsonReader = context.Reader; - return (T)JToken.ReadFrom(jsonReader); + if (bsonReader.GetCurrentBsonType() == BsonType.Null) + { + bsonReader.ReadNull(); + + return null; + } + else + { + var jsonReader = new BsonJsonReader(bsonReader); + + return (T)JToken.ReadFrom(jsonReader); + } } - protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, T value) + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T value) { - var jsonWriter = new BsonJsonWriter(context.Writer); + var bsonWriter = context.Writer; + + if (value == null) + { + bsonWriter.WriteNull(); + } + else + { + var jsonWriter = new BsonJsonWriter(bsonWriter); - value.WriteTo(jsonWriter); + value.WriteTo(jsonWriter); + } } } }