在我的服务器端 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
不受支持。没什么大不了的,我做了一个函数来转换 ExpandoObject
到 Dictionary<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/