未调用自定义通用 json 转换器
Custom generic json converter not called
我有一个带有抽象键和值的字典的自定义 JsonConverter:
public class DictionaryJsonConverter<TKey, TValue> : JsonConverter<Dictionary<TKey, TValue>>
我是这样使用的:
[JsonConverter(typeof(DictionaryJsonConverter<ServerDevice, long>))]
public static Dictionary<ServerDevice, long> KnownServers { get; set; }
但是read和write方法都没有被调用。我错过了什么吗?
错误再现示例:
https://dotnetfiddle.net/2VakI3(不在 DotNetFiddle 中编译)
我不保证读取或写入的方法是正确的,因为我没有看到结果。
我认为问题在于您不能只执行 JsonConvert.SerializeObject(SomeDictionaryThatHappensToBeInAProperty)
,因为那样它就看不到 JsonConverterAttribute
。它所知道的是它从某处获得了一本字典。因此,它将使用字典中的任何 JsonConverter
。
因此,您需要明确地告诉 Json.NET 使用哪个转换器。从您的评论中,您似乎找到了一种方法来告诉 Json.NET 使用哪个转换器(通过将转换器存储在 JsonSerializerSettings
实例的 Converters
属性 中并将其作为JsonConvert.SerializeObject
).
的参数
但是,如果您的 属性 在 class 中,并且您序列化了那个 class 的一个实例,它会像您拥有的那样正常工作,因为它会看到属性 上的属性并知道要使用哪个 JsonConverter
。例如:
public class Test {
[JsonConverter(typeof(DictionaryJsonConverter<ServerDevice, long>))]
public Dictionary<ServerDevice, long> KnownServers { get; set; }
}
...
static void Main(string[] args) {
Test test = new Test() {
KnownServers = new Dictionary<ServerDevice, long>() {
{ new ServerDevice() { ID = "a", Name = "A", IPAddress = IPAddress.Any }, 1 },
{ new ServerDevice() { ID = "b", Name = "B", IPAddress = IPAddress.Any }, 2 },
{ new ServerDevice() { ID = "c", Name = "C", IPAddress = IPAddress.Any }, 3 },
}
};
// This correctly uses DictionaryJsonConverter for KnownServers
Console.WriteLine(JsonConvert.SerializeObject(test));
}
这个方法可能不是你想要的,因为它显然也会序列化 属性“周围”的 Test
对象。
我有一个带有抽象键和值的字典的自定义 JsonConverter:
public class DictionaryJsonConverter<TKey, TValue> : JsonConverter<Dictionary<TKey, TValue>>
我是这样使用的:
[JsonConverter(typeof(DictionaryJsonConverter<ServerDevice, long>))]
public static Dictionary<ServerDevice, long> KnownServers { get; set; }
但是read和write方法都没有被调用。我错过了什么吗?
错误再现示例:
https://dotnetfiddle.net/2VakI3(不在 DotNetFiddle 中编译)
我不保证读取或写入的方法是正确的,因为我没有看到结果。
我认为问题在于您不能只执行 JsonConvert.SerializeObject(SomeDictionaryThatHappensToBeInAProperty)
,因为那样它就看不到 JsonConverterAttribute
。它所知道的是它从某处获得了一本字典。因此,它将使用字典中的任何 JsonConverter
。
因此,您需要明确地告诉 Json.NET 使用哪个转换器。从您的评论中,您似乎找到了一种方法来告诉 Json.NET 使用哪个转换器(通过将转换器存储在 JsonSerializerSettings
实例的 Converters
属性 中并将其作为JsonConvert.SerializeObject
).
但是,如果您的 属性 在 class 中,并且您序列化了那个 class 的一个实例,它会像您拥有的那样正常工作,因为它会看到属性 上的属性并知道要使用哪个 JsonConverter
。例如:
public class Test {
[JsonConverter(typeof(DictionaryJsonConverter<ServerDevice, long>))]
public Dictionary<ServerDevice, long> KnownServers { get; set; }
}
...
static void Main(string[] args) {
Test test = new Test() {
KnownServers = new Dictionary<ServerDevice, long>() {
{ new ServerDevice() { ID = "a", Name = "A", IPAddress = IPAddress.Any }, 1 },
{ new ServerDevice() { ID = "b", Name = "B", IPAddress = IPAddress.Any }, 2 },
{ new ServerDevice() { ID = "c", Name = "C", IPAddress = IPAddress.Any }, 3 },
}
};
// This correctly uses DictionaryJsonConverter for KnownServers
Console.WriteLine(JsonConvert.SerializeObject(test));
}
这个方法可能不是你想要的,因为它显然也会序列化 属性“周围”的 Test
对象。