From 3375922b02cdfb6e87a9c146ecfd6fc0da8f1c57 Mon Sep 17 00:00:00 2001 From: Joes Date: Sat, 14 Sep 2024 11:15:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=8C=E5=96=84JsonNodeSerializer?= =?UTF-8?q?=E5=8F=AF=E7=A9=BA=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JsonNodeSerializer.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) 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