在 .NET Core 3 中序列化 Manatee.Json [英] Serializing Manatee.Json in .NET Core 3
问题描述
背景
我想从我的 .NET Core 3 应用程序中提供一些 JsonSchema,以及序列化为 JSON 的其他对象.由于Manatee.Json 经常更新并且对JsonSchema 提供了很好的支持,所以它们是我的首选.同时,.NET Core 3 为返回对象提供了出色的支持,可以将它们转换为 JSON 文档.
Background
I want to provide some JsonSchema from my .NET Core 3 application, as well as other objects serialized into JSON. Since Manatee.Json is frequently updated and provides good support for JsonSchema, they are my preferred choice. At the same time, .NET Core 3 provides excellent support for returning objects with magic that transform them into JSON documents.
示例:
public ActionResult<MyFancyClass> MyFancyAction()
{
return new MyFancyClass {
Property1 = "property 1 content",
Property2 = "property 2 content",
};
}
输出:
{
"Property1": "property 1 content",
"Property2": "property 2 content"
}
问题
.NET Core 3 内部支持 JSON,其 System.Text.Json
在前面的示例中使用.如果我尝试序列化 Manatee.Json.Schema.JsonSchema
,它的内部结构被序列化,而不是 json 模式本身.
Problem
.NET Core 3 has internal support for JSON with its System.Text.Json
which is used in the previous example. If I try to serialize Manatee.Json.Schema.JsonSchema
, its internal structure are serialized, not the json schema itself.
示例:
public ActionResult<MyFancyClass2> MyFancyAction2()
{
return new MyFancyClass2 {
Property1 = "property 1 content",
Property1Schema = new JsonSchema()
.Type(JsonSchemaType.String)
};
}
输出:
{
"Property1": "property 1 content",
"Property1Schema": [{
"name":"type",
"supportedVersions":15,
"validationSequence":1,
"vocabulary": {
"id":"https://json-schema.org/draft/2019-09/vocab/validation",
"metaSchemaId":"https://json-schema.org/draft/2019-09/meta/validation"
}
}]
}
我希望:
{
"Property1": "property 1 content",
"Property1Schema": {
"type": "string",
}
}
Manatee.Json.JsonValue
也有内部结构冲突,其中 System.Text.Json.JsonSerializer
无法正确访问内部获取方法,例如我得到这个异常消息:
Manatee.Json.JsonValue
also have a conflicting inner structure, where System.Text.Json.JsonSerializer
fails to access internal get methods correctly and I get for instance this exception message:
Cannot access value of type Object as type Boolean.
发现Manatee.Json.Schema.JsonSchema
有一个 .ToJson()
方法,可用于获取正确的 json 模式作为 JsonValue
,但是然后我得到了我刚刚提到的序列化 Manatee.Json.JsonValue
的问题.
Discovery
Manatee.Json.Schema.JsonSchema
has a .ToJson()
method that can be used to get the correct json schema as a JsonValue
, but then I get the problem I just mentioned with serializing Manatee.Json.JsonValue
.
问题
有谁知道让 System.Text.Json
序列化 Manatee.Json
结构的方法?
Question
Does anyone know a way to enable System.Text.Json
to serialize Manatee.Json
structures?
侧标
另一种方法是完全替换System.Text.Json
(看看这个问题).
推荐答案
.NET Core 3 json 序列化带有很多配置选项.其中之一是添加转换器来指定不同类型的序列化方式.一种方法是为 JsonSchema
创建一个 JsonConverter
,为 JsonValue
创建另一个.
.NET Core 3 json serialization comes with a lot of configuration options. One of them is to add converters that specify how different types should be serialized. One way forward is to create a JsonConverter
for JsonSchema
and another for JsonValue
.
对于 JsonSchema
,我们可以实现一个 JsonSchemaConverter
,在序列化/写入时,将 json 模式提取为 JsonValue 并要求序列化器序列化该 JsonValue.像这样:
For JsonSchema
we can implement a JsonSchemaConverter
that when serializing/writing, extracts the json schema as a JsonValue and ask the serializer to serialize that JsonValue instead. Like this:
public class JsonSchemaConverter : JsonConverter<JsonSchema>
{
public JsonSchemaConverter()
{
_manateeSerializer = new ManateeSerializer();
}
private ManateeSerializer _manateeSerializer { get; set; }
public override JsonSchema Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jsonText = reader.GetString();
var jsonValue = JsonValue.Parse(jsonText);
return _manateeSerializer.Deserialize<JsonSchema>(jsonValue);
}
public override void Write(Utf8JsonWriter writer, JsonSchema value, JsonSerializerOptions options)
{
var schemaAsJson = value.ToJson(_manateeSerializer);
try
{
System.Text.Json.JsonSerializer.Serialize<JsonValue>(writer, schemaAsJson, options);
}
catch (Exception e)
{
Log.Information($"Failed to serialize JsonSchema ({e.Message});");
writer.WriteNullValue();
}
}
}
对于 JsonValue
我们可以把它改成 System.Text.Json
可以理解的东西,因为它毕竟是 json.一种不幸的方法是将 JsonValue
序列化为 string
,例如用 JsonDocument.Parse(string)
解析它并序列化它的 RootElement
属性.感觉没有必要通过 JsonDocument
去,所以如果有人找到更好的解决方案那就太好了!一个可能的实现如下所示:
For JsonValue
we can change it into something System.Text.Json
understands, since it is json after all. One unfortunate approach is to serialize the JsonValue
to a string
, parsing it with for instance JsonDocument.Parse(string)
and serialize its RootElement
property. It feels so unnecessary to go via JsonDocument
, so if anyone finds a better solution that would be great!
A possible implementation can look like this:
public class JsonValueConverter : JsonConverter<JsonValue>
{
public override JsonValue Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var json = reader.GetString();
return JsonValue.Parse(json);
}
public override void Write(Utf8JsonWriter writer, JsonValue value, JsonSerializerOptions options)
{
string content = value.ToString();
try
{
var jsonDocument = JsonDocument.Parse(content);
JsonSerializer.Serialize<JsonElement>(writer, jsonDocument.RootElement, options);
}
catch (Exception e)
{
Log.Warning($"JsonDocument.Parse(JsonValue) failed in JsonValueConverter.Write(,,).
{e.Message}");
writer.WriteNullValue();
}
}
}
它们必须像这样在 Startup.cs
注册:
They must be registered at Startup.cs
like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonValueConverter());
options.JsonSerializerOptions.Converters.Add(new JsonSchemaConverter());
});
}
这篇关于在 .NET Core 3 中序列化 Manatee.Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!