如果我尝试将JsonDictionary属性添加到 Dictionary(Integer,MyClass),则编译器告诉我,该属性无法应用.为什么会这样?
If I try to add the JsonDictionary attribute to a Dictionary(Of Integer, MyClass), the compiler tells me, that the attribute could not be applied. Why is this?
<JsonDictionary()> Public ReadOnly Property monatswerte As Dictionary(Of Integer, MyClass)我基本上找不到如何在线使用JsonDictionary的任何示例.
I basically couldn't find any examples of how to use the JsonDictionary online.
推荐答案应用于类型(类或接口)以强制Json.NET的默认合同解析器(及其子类)以生成该类型的词典合同.它是三个类似属性家族的一部分:
<JsonDictionary()> can be applied to a type (a class or interface) to force Json.NET's default contract resolver (and its subclasses) to generate a dictionary contract for the type. It is part of a family of three similar attributes:
- <JsonObject()> .强制将类型序列化为JSON对象.有助于强制集合属性(而不是此处)所示的项目进行序列化.
- <JsonArray()> .强制将类型序列化为JSON数组.可能对强制将字典序列化为键/值对数组很有用.
- <JsonDictionary()> .强制将类型解释为字典并将其序列化为JSON对象. (当然,它必须仍然实现IDictionary或IDictionary<TKey, TValue>才能起作用.)
- <JsonObject()>. Force a type to be serialized as a JSON object. Useful to force serialization of collection properties instead of items as shown here.
- <JsonArray()>. Force a type to serialized as a JSON array. Possibly useful to, e.g., force a dictionary to be serialized as a key/value pair array.
- <JsonDictionary()>. Force a type to be interpreted as a dictionary and serialized as a JSON object. (It must still implement IDictionary or IDictionary<TKey, TValue> for this to work, of course.)
从表面上看,<JsonDictionary()>似乎没什么用,因为 DefaultContractResolver.CreateContract(Type objectType) 在检查任何其他接口实现之前,先检查传入类型是否实现IDictionary.但是,该属性具有几个属性,可用于自定义如何字典被序列化,包括:
On the face of it <JsonDictionary()> does not seem that useful because DefaultContractResolver.CreateContract(Type objectType) checks that the incoming type implements IDictionary before checking for any other interface implementations. However, the attribute has several properties that could be useful in customizing how a dictionary is serialized, including:
-
NamingStrategyType 和 NamingStrategyParameters 允许控制字典键的大小写和名称映射.
例如,即使 CamelCasePropertyNamesContractResolver 正在使用:
For instance, the following dictionary will always serialize its keys literally, without renaming, even if CamelCasePropertyNamesContractResolver is in use:
<JsonDictionary(NamingStrategyType := GetType(LiteralKeyDictionaryNamingStrategy))> _ Public Class LiteralKeyDictionary(Of TValue) Inherits Dictionary(Of String, TValue) End Class Public Class LiteralKeyDictionaryNamingStrategy Inherits DefaultNamingStrategy Public Sub New() ProcessDictionaryKeys = False End Sub End Class
ItemConverterType , ItemConverterParameters , ItemIsReference , ItemReferenceLoopHandling 和 允许自定义字典值的序列化方式.
ItemConverterType, ItemConverterParameters, ItemIsReference, ItemReferenceLoopHandlingand ItemTypeNameHandling allow customization of how dictionary values are serialized.
例如,在以下字典类型中,值始终与一起存储引用保存已启用:
For instance, in the following dictionary type, the values are always stored with reference preservation enabled:
<JsonDictionary(ItemIsReference := true)> _ Public Class ReferenceObjectDictionary(of TKey, TValue As {Class, New}) Inherits Dictionary(Of TKey, TValue) End Class或者,对于包含enum值的字典类型,您可以应用 StringEnumConverter 作为ItemConverterType,以强制将值序列化为字符串.
Or, for a dictionary type containing enum values, you might apply StringEnumConverter as an ItemConverterType to force the values to be serialized as strings.
更多推荐
将JsonDictionary属性应用于字典
发布评论