用 Manatee.Json 替换 System.Text.Json 以在 .NET Core 3 中进行序列化 [英] Replacing System.Text.Json with Manatee.Json for serialization 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
.
问题
有谁知道将 .NET Core 3 中的默认序列化从使用 System.Text.Json
更改为使用 Manatee.Json
的方法吗?
Question
Does anyone know a way to change default serialization in .NET Core 3 from using System.Text.Json
to using Manatee.Json
?
侧标
另一种方法是启用 System.Text.Json
以序列化 Manatee.Json
结构(查看 这个问题).
Sidemark
Another way forward is to enable System.Text.Json
to serialize Manatee.Json
structures (take a look at this question).
推荐答案
你很幸运!我只是将架构后继者发布到 Manatee.Json:JsonSchema.Net!它完全支持草案 6 到 2019-09,100% 基于 System.Text.Json
,而且速度快得离谱!
You're in luck! I just release the schema-successor to Manatee.Json: JsonSchema.Net! It has full support of drafts 6 through 2019-09, is based 100% in System.Text.Json
, and it's ridiculously fast!
希望这会有所帮助.
(不知道为什么我没有收到关于 Manatee.Json
标签的电子邮件.抱歉花了这么长时间才找到这个.)
(Don't know why I didn't get emailed for the Manatee.Json
tag. Sorry it took so long to find this.)
这篇关于用 Manatee.Json 替换 System.Text.Json 以在 .NET Core 3 中进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!