用Manatee.Json替换System.Text.Json以在.NET Core 3中进行序列化 [英] Replacing System.Text.Json with Manatee.Json for serialization in .NET Core 3

查看:58
本文介绍了用Manatee.Json替换System.Text.Json以在.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的 System.Text.Json 对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 无法正确访问内部get方法,例如异常消息:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆