我尝试从 Newtonsoft.Json 迁移到 System.Text.Json。我想反序列化抽象类。Newtonsoft.Json 为此具有 TypeNameHandling。有没有办法通过.net core 3.0 上的 System.Text.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)
这确实有效 - 但有更好的方法吗?
取而代之的是:
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的核心。还有这个。 …
我正在准备从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
在 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
我正在使用 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) 使用.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 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) 我有以下简单的课程:
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) 我正在为 .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)
新命名空间中是否有等效的方法?
system.text.json ×10
c# ×9
.net-core ×4
json ×4
asp.net-core ×2
.net ×1
.net-6.0 ×1
json.net ×1
razor-pages ×1