[英]How can I create a custom converter similar to Newtonsoft using system.text.json?
I recently spent a lot of time upgrading my old asp.net 4.6 project to .Net 6.我最近花了很多时间将旧的 asp.net 4.6 项目升级到 .Net 6。
I have almost everything working, but I am having trouble using System.Text.Json
in place of Newtonsoft.我几乎一切正常,但我在使用
System.Text.Json
代替 Newtonsoft 时遇到了麻烦。
In my Startup.cs I ham trying to use a Custom Converter like this:在我的 Startup.cs 中,我尝试使用这样的自定义转换器:
services.AddMvc()
.AddJsonOptions(opts => {
//custom converter for PlayerItemDto
opts.JsonSerializerOptions.Converters.Add(new PlayerItemConverter());
});
Here is where I am trying to do the conversion.这是我尝试进行转换的地方。 This is all Newtonsoft now, and I am not sure if this will work in
System.Text.Json
.这就是 Newtonsoft 现在的全部内容,我不确定这是否适用于
System.Text.Json
。
Specifically, I can't find anything in System.Text.Json
that replaces var obj = JObject.Load(reader);
具体来说,我在
System.Text.Json
中找不到任何替换var obj = JObject.Load(reader);
and options.Populate(obj.CreateReader(), retval)
和
options.Populate(obj.CreateReader(), retval)
Does anyone know if it's possible?有谁知道这是否可能?
Here is my converter class:这是我的转换器类:
public class PlayerItemConverter : CustomCreationConverter<PlayerItemDto>
{
public PlayerItemDto Create(String morality)
{
switch (morality) {
case "bad":
return new MonsterTypeDto();
case "good":
return new LeaderTypeDto();
}
}
public override Object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var obj = JObject.Load(reader);
var morality = (String) obj.Property("morality");
var retval = Create(morality);
serializer.Populate(obj.CreateReader(), retval);
return retval;
}
}
Your converter needs to derive from JsonConverter, and the implement CanConvert, Read and Write eg:您的转换器需要从 JsonConverter 派生,并实现 CanConvert、Read 和 Write,例如:
public class PlayerItemConverter : JsonConverter<PlayerItemDto>
{
public override bool CanConvert(Type typeToConvert)
{
return typeof(PlayerItemDto).IsAssignableFrom(typeToConvert);
}
public override PlayerItemDto Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using (JsonDocument jsonDoc = JsonDocument.ParseValue(ref reader))
{
// Convert your jsonDoc into a PlayerItemDto and return it
}
}
public override void Write(Utf8JsonWriter writer, PlayerItemDto value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value, options);
}
}
You can then either register your converter, or declare it using an attribute on the class, eg:然后,您可以注册您的转换器,或使用类上的属性声明它,例如:
[JsonConverter(typeof(PlayerItemConverter))]
public class PlayerItemDto
{
}
UPDATE更新
Considering classes:考虑类:
public class PlayerItemDto
{
public string Name { get; set; }
public int Age { get; set; }
public AddressDto Address { get; set; }
}
public class AddressDto
{
public int HouseNumber { get; set; }
public string Street { get; set; }
}
You can access properties using:您可以使用以下方式访问属性:
using (JsonDocument jsonDoc = JsonDocument.ParseValue(ref reader))
{
string name = jsonDoc.RootElement.GetProperty("Name").GetString();
int age = jsonDoc.RootElement.GetProperty("Age").GetInt32();
int houseNumber = jsonDoc.RootElement.GetProperty("Address").GetProperty("HouseNumber").GetInt32();
string street = jsonDoc.RootElement.GetProperty("Address").GetProperty("Street").GetString();
// Build your PlayerItemDto from the properties
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.