I believe the issue is that you can't just do JsonConvert.SerializeObject(SomeDictionaryThatHappensToBeInAProperty)
, because then it doesn't see the JsonConverterAttribute
. All it knows is that it has been given a Dictionary from somewhere. As such it will use whichever JsonConverter
it has for dictionaries.
Therefore, you need to tell Json.NET which converter to use explicitly. From your comments you appear to have found one way to tell Json.NET which converter to use (by storing the converter in the Converters
property of a JsonSerializerSettings
instance and giving it as argument to JsonConvert.SerializeObject
).
If, however, your property was in a class, and you serialized an instance of that class, it would work fine as you had it, because it will see the attribute on the property and know which JsonConverter
to use. For example:
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));
}
This method may not be what you want though because it will obviously serialize the Test
object "around" the property as well.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…