-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/EasilyNET.MongoSerializer.AspNetCore/JsonNodeSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using MongoDB.Bson.Serialization.Serializers; | ||
using MongoDB.Bson.Serialization; | ||
using System.Text.Json.Nodes; | ||
|
||
namespace EasilyNET.MongoSerializer.AspNetCore; | ||
|
||
/// <summary> | ||
/// JsonNode Support | ||
/// </summary> | ||
public class JsonNodeSerializer : SerializerBase<JsonNode> | ||
{ | ||
const string EmptyObject = "{}"; | ||
|
||
/// <summary> | ||
/// if null write {} | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <param name="args"></param> | ||
/// <param name="value"></param> | ||
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, JsonNode value) | ||
{ | ||
var json = value?.ToString() ?? EmptyObject; | ||
context?.Writer?.WriteString(json); | ||
} | ||
|
||
/// <summary> | ||
/// if null return {} | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <param name="args"></param> | ||
/// <returns></returns> | ||
public override JsonNode Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) | ||
{ | ||
string json = context?.Reader?.ReadString() ?? EmptyObject; | ||
return JsonNode.Parse(json)!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters