问题标签 [system.text.json]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
6 回答
9863 浏览

c# - 使用 System.Text.Json 异步反序列化列表

假设我请求一个包含许多对象列表的大型 json 文件。我不希望它们一次全部进入内存,但我宁愿一个一个地阅读和处理它们。所以我需要将异步System.IO.Stream流转换为IAsyncEnumerable<T>. 如何使用新的System.Text.JsonAPI 来执行此操作?

0 投票
2 回答
17926 浏览

json - System.Text.Json - JSON 值无法转换为 System.String

我将以下内容json发送到 api

我想将其映射到以下内容:

当映射Payload属性失败时,它无法映射json到字符串,我只是希望Payload成为一个string版本json(将进入一个jsonb字段postgres

我正在使用 .NET Core 3.0,System.Text.Json如果可能的话,我更喜欢在切换到Newtonsoft.

0 投票
2 回答
893 浏览

c# - 使用 System.Text.Json 时,有没有办法在对象上返回自定义 JSON 结构?

我正在编写一个不公开任何公共属性的类(AttributeBag)。在里面,我有一个通过 Get 和 Set 方法管理的对象(属性)列表。我希望能够在 AttributeBag 类上运行 JsonSerializer.Serialize() 并检索如下 JSON 结构:

我的 Attribute 和 AttributeBag 类如下所示:

我希望能够做这样简单的事情:

我尝试查看文档,但不确定如何继续进行上述操作。我应该为此使用 Utf8JsonWriter 吗?看来,如果我使用 Utf8JsonWriter,我将需要一个实现 JSON 写入的函数,并使用 ab.Serialize() 调用它,并且不需要直接使用 JsonSerializer。这是要走的路还是有办法使用JsonSerializer.Serialize?

任何指针将不胜感激!

0 投票
2 回答
4290 浏览

c# - System.Text.Json 合并两个对象

是否可以将这样的两个 json 对象与System.Text.Json?

对象 1

对象 2

结果对象

我可以像这样使用 newtonsoft.json 来实现它

但是有办法System.Text.Json吗?

0 投票
0 回答
136 浏览

c# - 如何使用 Utf8Json 实现基于属性类型的属性黑名单?

目标

基于 aTypestring分别表示父类和属性,我想在序列化时过滤属性。

我知道 Utf8Json(和 Newtonsoft 就此事而言)支持现场属性,但由于代码库的原因,我想根据存储在字典中的已定义黑名单来执行此操作。

已经使用 Newtonsoft 的示例

目前,Newtonsoft.Json我有类似于以下代码的包(为简洁起见)。

稍后实例化,存储在变量中并调用序列化。

0 投票
1 回答
2894 浏览

c# - System.Text.Json 序列化 Unicode 字符(如表情符号)的问题

我正在将应用程序从 .NET Core 2.2 升级到 .NET Core 3.0,并且新的System.Text.Json序列化程序的行为与 Newtonsoft 在 2.2 中的行为不同。在诸如不间断空格 (\u00A0) 或表情符号字符之类的字符上,Newtonsoft(甚至 Utf8Json)将它们序列化为它们的实际字符,而不是 Unicode 代码。

我创建了一个简单的 .NET Fiddle 来展示这一点。

https://dotnetfiddle.net/erCaZl

是否有JsonSerializerOptions像 Newtonsoft 那样序列化的编码器或属性?

0 投票
1 回答
345 浏览

json - JSON反序列化.net Core 3

这个真的是Basic。使用新的 System.Text.Json 反序列化字符串;

但这样做
我只是得到 null 为 Strings 和 0 为 temp2 中的 Int

这一定很简单,但我不明白

0 投票
2 回答
390 浏览

json - 使用 System.Text.Json 将科学记数法反序列化为 long

我有带有科学计数法数字的 JSON,例如1.83E+2. 用 Json.NET 将它反序列化为 along对我来说效果很好,但是当我用 System.Text.Json 中的新反序列化器替换反序列化器时,它会抛出一个JsonException

System.Text.Json.JsonException:'JSON 值无法转换为 System.Int64。...'

这是一个可重现的示例:

0 投票
1 回答
1827 浏览

c# - Dependency injection with the new .net-core-3 json serializer

I use this contract resolver for dependency-injection with Autofac and Json.NET:

Then, I use it with DI to initialize the JsonSerializer:


What would be the equivalent of this technique with the new System.Text.Json in net-core-3.0 - if there is any already? I wasn't able to figure this out and couldn't find any interfaces that would look similar to this ones.

0 投票
1 回答
1257 浏览

c# - How to deserialize ReadOnlySpan to object using System.Text.Json

I have a case when I get a very big text data & each line contains some metadata + json data string. I need to process the json data on each line.

This is what I have:

#xA;

Where Data is a Dto class which has bunch of (7) different attributes:

#xA;

I'm trying to achieve this with System.Text.Json API. Surprisingly it doesn't have any overload to deserialize from ReadOnlySpan<char>, so I come up with this:

#xA;

While this works, it looks very convoluted.

Is this the way to go or am I missing something more simple ?