标签: system.text.json

System.Text.Json 中是否可以进行多态反序列化?

我尝试从 Newtonsoft.Json 迁移到 System.Text.Json。我想反序列化抽象类。Newtonsoft.Json 为此具有 TypeNameHandling。有没有办法通过.net core 3.0 上的 System.Text.Json 反序列化抽象类?

c# json .net-core-3.0 system.text.json

60
推荐指数
6
解决办法
3万
查看次数

你如何使用 System.Text.Json 从一些 json 中读取一个简单的值?

我有这个 json

{"id":"48e86841-f62c-42c9-ae20-b54ba8c35d6d"}
Run Code Online (Sandbox Code Playgroud)

我如何48e86841-f62c-42c9-ae20-b54ba8c35d6d摆脱它?我能找到的所有例子都显示做类似的事情

var o = System.Text.Json.JsonSerializer.Deserialize<some-type>(json);
o.id // <- here's the ID!
Run Code Online (Sandbox Code Playgroud)

但是我没有符合这个定义的类型,我不想创建一个。我试过反序列化为动态,但我无法让它工作。

var result = System.Text.Json.JsonSerializer.Deserialize<dynamic>(json);
result.id // <-- An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Linq.Expressions.dll but was not handled in user code: ''System.Text.Json.JsonElement' does not contain a definition for 'id''
Run Code Online (Sandbox Code Playgroud)

任何人都可以提供任何建议吗?


编辑:

我刚刚发现我可以这样做:

Guid id = System.Text.Json.JsonDocument.Parse(json).RootElement.GetProperty("id").GetGuid();
Run Code Online (Sandbox Code Playgroud)

这确实有效 - 但有更好的方法吗?

c# .net-core system.text.json

39
推荐指数
4
解决办法
3万
查看次数

如何全局设置 System.Text.Json.JsonSerializer 的默认选项?

取而代之的是:

JsonSerializerOptions options = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
    // etc.
};
var so = JsonSerializer.Deserialize<SomeObject>(someJsonString, options);
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情:

// This property is a pleasant fiction
JsonSerializer.DefaultSettings = new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
    // etc.
};

// This uses my options
var soA = JsonSerializer.Deserialize<SomeObject>(someJsonString); 

// And somewhere else in the same codebase...
// This also uses my options
var soB = JsonSerializer.Deserialize<SomeOtherObject>(someOtherJsonString); 

Run Code Online (Sandbox Code Playgroud)

希望不必JsonSerializerOptions为我们最常见的情况传递一个实例,并覆盖异常而不是规则。

问答中所示,这是 Json.Net 的一个有用功能。我看着在文档System.Text.Json以及这个GitHub库为.NET的核心。还有这个。 …

c# serialization json .net-core system.text.json

37
推荐指数
5
解决办法
2万
查看次数

.Net Core 3.0 JsonSerializer填充现有对象

我正在准备从ASP.NET Core 2.2迁移到3.0。

As I don't use any more advanced JSON features (but maybe one as described below), and 3.0 now comes with a built-in namespace/classes for JSON, System.Text.Json, I decided to see if I could drop the previous default Newtonsoft.Json. Do note, I'm aware that System.Text.Json will not completely replace Newtonsoft.Json.

I managed to do that everywhere, e.g.

var obj = JsonSerializer.Parse<T>(jsonstring);

var jsonstring = JsonSerializer.ToString(obj);
Run Code Online (Sandbox Code Playgroud)

but in one place, where I populate an existing object. …

c# asp.net-core razor-pages asp.net-core-3.0 system.text.json

36
推荐指数
3
解决办法
8311
查看次数

使用新的 Net Core 3.0 Json 时忽略属性

在 ASP.Net Core 2.2 中使用 JSON.Net 时,当序列化为 JSON 时,当其值为 null 时,我能够忽略属性:

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public DateTime? Created { get; set; }
Run Code Online (Sandbox Code Playgroud)

但是,当使用内置于 JSON (System.Text.Json) 的新 ASP.Net Core 3.0 时,如果某个属性的值为 null,我找不到可以忽略该属性的等效属性。

我只能找到 JsonIgnore。

我错过了什么吗?

json json.net asp.net-core-3.0 .net-core-3.0 system.text.json

29
推荐指数
5
解决办法
2万
查看次数

是否可以使用 System.Text.Json 将 json 字符串反序列化为动态对象?

我正在使用 System.Text.Json 包来使用序列化和反序列化。

当明确指定类型时,我可以将 json 字符串反序列化为对象,如下所示。

var data = JsonSerializer.Deserialize<PersonType>(jsonString);
Run Code Online (Sandbox Code Playgroud)

但动态类型不行。是否可以在不指定类型的情况下反序列化?谢谢你!

var data = JsonSerializer.Deserialize<dynamic>(jsonString);
Run Code Online (Sandbox Code Playgroud)

c# system.text.json

24
推荐指数
1
解决办法
3万
查看次数

System.Text.Json:如何为枚举值指定自定义名称?

使用.NET Core 中的System.Text.Json序列化器功能,如何为枚举值指定自定义值,类似于JsonPropertyName? 例如:

public enum Example {
  Trick, 
  Treat, 
  [JsonPropertyName("Trick-Or-Treat")] // Error: Attribute 'JsonPropertyName' is not valid on this declaration type. It is only valid on 'property, indexer' declarations.
   TrickOrTreat
}
Run Code Online (Sandbox Code Playgroud)

.net c# .net-core .net-core-3.0 system.text.json

22
推荐指数
1
解决办法
2万
查看次数

当我不使用“System.IntPtr”时,为什么 System.Text.Json 会为“System.IntPtr”抛出“NotSupportedException”?

我有一个 .NET Framework 4.8 / AngularJS 应用程序,我正在尝试升级到 .NET 6,以便我可以开始尝试更新的前端框架(如 Blazor)。升级进展顺利,但当从 Angular JS 代码调用我的 API 方法之一时,我总是看到此错误:

System.NotSupportedException: Serialization and deserialization of 'System.IntPtr' instances are not supported. Path: $.TargetSite.MethodHandle.Value.
 ---> System.NotSupportedException: Serialization and deserialization of 'System.IntPtr' instances are not supported.
   at System.Text.Json.Serialization.Converters.UnsupportedTypeConverter`1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core system.text.json .net-6.0

21
推荐指数
1
解决办法
5万
查看次数

类型的反序列化构造函数中的每个参数必须绑定到反序列化时的对象属性或字段

我有以下简单的课程:

public abstract class GitObject
{
    public Repository Repository { get; set; }
    public abstract string Serialize();
    public abstract void Deserialize(string data);

    public class Blob : GitObject
    {
        public string Data { get; set; }

        public Blob(Repository repository, string data = null)
        {
            if (data != null) Data = File.ReadAllText(data);
            Repository = repository;
        }
        public override string Serialize()
        {
            return JsonSerializer.Serialize(this);
        }
        public override void Deserialize(string data)
        {
            Blob blobData = JsonSerializer.Deserialize<Blob>(data);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道可能还有很大的改进空间(我很高兴听到这一点)。但是,该方法Deserialize给了我错误

Each parameter …
Run Code Online (Sandbox Code Playgroud)

c# jsonserializer system.text.json

18
推荐指数
1
解决办法
2万
查看次数

使用 System.Text.Json 反序列化匿名类型

我正在为 .NET Core 3.x 更新一些应用程序,作为其中的一部分,我正在尝试Json.NET从新System.Text.Json类迁移。使用 Json.NET,我可以反序列化一个匿名类型,如下所示:

var token = JsonConvert.DeserializeAnonymousType(jsonStr, new { token = "" }).token;
Run Code Online (Sandbox Code Playgroud)

新命名空间中是否有等效的方法?

c# json .net-core .net-core-3.1 system.text.json

17
推荐指数
1
解决办法
5365
查看次数