Skip to content

Commit

Permalink
fix: 调整JsonNode的BSON序列化器支持可空类型. (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
joesdu authored Sep 14, 2024
2 parents 535d2ee + d2f4cee commit 9a4d0a3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
45 changes: 45 additions & 0 deletions sample/WebApi.Test.Unit/Controllers/MongoTestController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Text.Json.Nodes;
using EasilyNET.Core.Enums;
using EasilyNET.WebCore.Swagger.Attributes;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -17,6 +18,28 @@ public class MongoTestController(DbContext db) : ControllerBase
{
private readonly FilterDefinitionBuilder<MongoTest> bf = Builders<MongoTest>.Filter;

/// <summary>
/// 测试JsonNode
/// </summary>
/// <returns></returns>
[HttpGet("JsonNodeTest")]
public async Task<JsonNodeTest> JsonNodeTest()
{
var coll = db.GetCollection<JsonNodeTest>("test.json.node");
var id = ObjectId.GenerateNewId();
var obj = new JsonNodeTest
{
Id = id.ToString(),
JsonNode = JsonNode.Parse("""
{
"name": "test"
}
""")!
};
await coll.InsertOneAsync(obj);
return await coll.Find(c => c.Id == obj.Id).FirstOrDefaultAsync();
}

/// <summary>
/// 添加一个动态数据,可便于快速测试一些代码.
/// </summary>
Expand Down Expand Up @@ -141,5 +164,27 @@ file sealed class MultiEnum
/// IEnumerable类型的枚举需要添加该特性,才能实现每一个元素都转成字符串,可以参考: https://github.com/joesdu/EasilyNET/issues/482
/// </summary>
[BsonRepresentation(BsonType.String)]
// ReSharper disable once UnusedMember.Local
public EZodiac[] Zodiac { get; set; } = [EZodiac., EZodiac., EZodiac.];
}

/// <summary>
/// 测试JsonNode的序列化
/// </summary>
public sealed class JsonNodeTest
{
/// <summary>
/// ID
/// </summary>
public required string Id { get; set; }

/// <summary>
/// 常规的JsonNode
/// </summary>
public required JsonNode JsonNode { get; set; }

/// <summary>
/// 可空的JsonNode
/// </summary>
public JsonNode? JsonNodeNullAble { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public sealed class DateOnlySerializerAsString(string format = "yyyy-MM-dd") : S
/// <inheritdoc />
public override DateOnly Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var str = context.Reader.ReadString();
var str = InnerSerializer.Deserialize(context, args);
var success = DateOnly.TryParseExact(str, format, out var result);
return success ? result : throw new("unsupported data formats.");
}
Expand Down
14 changes: 6 additions & 8 deletions src/EasilyNET.MongoSerializer.AspNetCore/JsonNodeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace EasilyNET.MongoSerializer.AspNetCore;
/// <summary>
/// JsonNode Support
/// </summary>
public sealed class JsonNodeSerializer : SerializerBase<JsonNode>
public sealed class JsonNodeSerializer : SerializerBase<JsonNode?>
{
private const string EmptyObject = "{}";
private readonly StringSerializer InnerSerializer = new();

/// <summary>
/// if null write {}
Expand All @@ -21,8 +21,7 @@ public sealed class JsonNodeSerializer : SerializerBase<JsonNode>
/// <param name="value"></param>
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, JsonNode? value)
{
var json = value?.ToString() ?? EmptyObject;
context.Writer.WriteString(json);
InnerSerializer.Serialize(context, args, string.IsNullOrWhiteSpace(value?.ToString()) ? null : value.ToString());
}

/// <summary>
Expand All @@ -31,10 +30,9 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati
/// <param name="context"></param>
/// <param name="args"></param>
/// <returns></returns>
public override JsonNode Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
public override JsonNode? Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var value = context.Reader.ReadString();
var json = string.IsNullOrWhiteSpace(value) ? EmptyObject : value;
return JsonNode.Parse(json)!;
var value = InnerSerializer.Deserialize(context, args);
return string.IsNullOrWhiteSpace(value) ? null : JsonNode.Parse(value);
}
}

0 comments on commit 9a4d0a3

Please sign in to comment.