diff --git a/src/EasilyNET.MongoSerializer.AspNetCore/JsonNodeSerializer.cs b/src/EasilyNET.MongoSerializer.AspNetCore/JsonNodeSerializer.cs index ac57840e..40b2795a 100644 --- a/src/EasilyNET.MongoSerializer.AspNetCore/JsonNodeSerializer.cs +++ b/src/EasilyNET.MongoSerializer.AspNetCore/JsonNodeSerializer.cs @@ -1,15 +1,17 @@ -using MongoDB.Bson.Serialization.Serializers; +using System.Text.Json.Nodes; using MongoDB.Bson.Serialization; -using System.Text.Json.Nodes; +using MongoDB.Bson.Serialization.Serializers; + +// ReSharper disable UnusedType.Global namespace EasilyNET.MongoSerializer.AspNetCore; /// /// JsonNode Support /// -public class JsonNodeSerializer : SerializerBase +public sealed class JsonNodeSerializer : SerializerBase { - const string EmptyObject = "{}"; + private const string EmptyObject = "{}"; /// /// if null write {} @@ -17,10 +19,10 @@ public class JsonNodeSerializer : SerializerBase /// /// /// - public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, JsonNode value) + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, JsonNode? value) { var json = value?.ToString() ?? EmptyObject; - context?.Writer?.WriteString(json); + context.Writer.WriteString(json); } /// @@ -31,7 +33,8 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati /// public override JsonNode Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { - string json = context?.Reader?.ReadString() ?? EmptyObject; + var value = context.Reader.ReadString(); + var json = string.IsNullOrWhiteSpace(value) ? EmptyObject : value; return JsonNode.Parse(json)!; } -} +} \ No newline at end of file