c# - IJSRuntime 忽略服务器端 blazor 项目中的自定义 json 序列化程序

标签 c# json asp.net-core serialization json.net

在我的服务器端 blazor 项目(核心 3,预览版 6)中,我试图用我的类的实例调用 javascript。您可以通过注入(inject) IJSRuntime 来做到这一点并调用 InvokeAsync在上面(参见 here )。

因为我需要摆脱所有空属性(应该处理我的对象 (chartJs) 的 js 库不能处理空值)并且因为我有一些自定义序列化(对于可以具有不同数据类型的枚举),我黑了它与 ExpandoObject 一起工作(这不是我的主意,但它有效)。

我首先用 json.net 序列化了我的对象,这样我就可以定义 NullValueHandling = NullValueHandling.Ignore所以所有的自定义转换器都被使用了。

然后我再次使用 json.net 将其解析为 ExpandoObject .我当然这样做了,因为否则不在 json 中的值在 c# 实例中将为空,但这样它们根本不存在。

ExpandoObject它没有空值并且具有正确的枚举值,然后用于调用 javascript。这样当它到达 javascript 引擎时就没有空值。

Since in preview6 json.net isn't used internally anymore并且新的 json 序列化程序(来自 System.Text.Json )无法处理 ExpandoObject s,当我从preview5更新到preview6时出现运行时错误,说ExpandoObject不受支持。没什么大不了的,我做了一个函数来转换 ExpandoObjectDictionary<string, object>可以毫无问题地序列化。

阅读this question了解更多关于我是如何做到的,并查看一些示例。如果您不太了解我所做的,这可能是个好主意。它还包含我将在此处添加的 json 以及 c# 模型和其他解释。

这行得通,但我想也许如果我能够回到 json.net,我就可以完全删除 ExpandoObject因为首先它会使用我为我的特殊枚举编写的自定义转换器,其次我可以指定 NullValueHandling = NullValueHandling.Ignore全局范围内(附注:这会有副作用吗?)。

我关注了the ms docs关于如何再次使用 json.net 并将其添加到 ConfigureServies方法(services.AddRazorPages() 已经存在):

services.AddRazorPages()
    .AddNewtonsoftJson(o =>
    {
        o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
        o.SerializerSettings.ContractResolver = new DefaultContractResolver
        {
            NamingStrategy = new CamelCaseNamingStrategy(true, false)
        };
    });

遗憾的是,它没有工作,直接用对象调用 js 导致与我添加它之前完全相同的 json(见下文)。

然后我尝试使用 ExpandoObject直接是因为我知道来自 .net 的新 json 序列化程序无法处理。正如预期的那样,它抛出了一个异常,并且堆栈跟踪没有显示 json.net 的迹象。这证实它实际上并没有像我告诉它的那样使用 json.net。

at System.Text.Json.Serialization.JsonClassInfo.GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo) at System.Text.Json.Serialization.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonClassInfo..ctor(Type type, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonSerializerOptions.GetOrAddClass(Type classType) at System.Text.Json.Serialization.JsonSerializer.GetRuntimeClassInfo(Object value, JsonClassInfo& jsonClassInfo, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonSerializer.HandleEnumerable(JsonClassInfo elementClassInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state) at System.Text.Json.Serialization.JsonSerializer.Write(Utf8JsonWriter writer, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonSerializer.WriteCore(PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonSerializer.WriteCoreString(Object value, Type type, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonSerializer.ToString[TValue](TValue value, JsonSerializerOptions options) at Microsoft.JSInterop.JSRuntimeBase.InvokeAsync[T](String identifier, Object[] args) at ChartJs.Blazor.ChartJS.ChartJsInterop.GetJsonRep(IJSRuntime jSRuntime, Object obj)

然后我还尝试仅更改普通序列化程序的选项,看看是否会改变任何内容。我用这个代替 AddNewtonsoftJson :

services.AddRazorPages()
    .AddJsonOptions(o => o.JsonSerializerOptions.IgnoreNullValues = true);

有趣的是,这仍然给了我相同的结果。

我将添加生成的 json 以及应该生成的内容。您也可以在我之前提到的 CodeReview 问题中找到这一点。

下面的json是我得到的:

  • 我没有在 ConfigureServices 中指定任何内容方法。
  • 我指定使用 Newtonsoft.Json通过AddNewtonsoftJson .
  • 我指定了 JsonOptions从 .net 到 AddJsonOptions .
{
    "options": {
        "someInt": 2,
        "someString": null,
        "axes": [
            {
                "someString": null
            },
            {
                "someString": "axisString"
            }
        ]
    },
    "data": {
        "data": [
            1,
            2,
            3,
            4,
            5
        ],
        "someString": "asdf",
        "someStringEnum": {}   <-- this is one of those special enums with a custom converter
    }
}

这是我应该得到的。请注意,所有空值都已删除,并且使用了自定义转换器。我只能通过使用 ExpandoObject 的 hacky 解决方案来获得此信息.

{
    "options": {
        "someInt": 2,
        "axes": [
            {},
            {
                "someString": "axisString"
            }
        ]
    },
    "data": {
        "data": [
            1,
            2,
            3,
            4,
            5
        ],
        "someString": "asdf",
        "someStringEnum": "someTestThing"   <-- this is one of those special enums with a custom converter
    }
}

那么为什么它还在使用 System.Text.Json而不是 Newtonsoft.Json即使我指示它这样做?我还需要添加什么才能使其使用 json.net?

最佳答案

正如@dbc 在评论中正确提到的,IJSRuntime 始终使用 System.Text.Json。我已通过询问 asp.net 团队确认了这一点(请参阅 my github issue)。

我提交了一个 github feature request这可能会在未来的 .NET 版本中实现这一点。

关于c# - IJSRuntime 忽略服务器端 blazor 项目中的自定义 json 序列化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56915186/

相关文章:

php - C3 : reading JSON generated from PHP

c# - 从 API 返回结果列表时出现异常错误

c# - ASP.NET Core MVC 应用程序发布时的奇怪行为

c# - 在 .NET 4.0 中,如何 'sandbox' 一个内存程序集并执行一个方法?

c# - 在 C# 单元测试中使用 HttpContext.GetTokenAsync

python - 为什么我的 `else` block 的 `try` 部分的代码没有运行?

c# - 如何在 Blazor 服务器应用程序中正确计算 SignalR 消息?

c# - ASP.NET MVC : Align checkbox and label on same line

c# - VB.Net WriteOnly 属性到 C#

php - 从两个表中获取记录并转换为json