将JsonDictionary属性应用于字典 [英] Applying JsonDictionary attribute to dictionary

查看:54
本文介绍了将JsonDictionary属性应用于字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试将JsonDictionary属性添加到.net Dictionary(Integer,MyClass),则编译器告诉我,该属性无法应用.为什么会这样?

If I try to add the JsonDictionary attribute to a .net 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.

推荐答案

<可以将c0> 应用于类型(类或接口)以强制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对象. (当然,它必须仍然实现IDictionaryIDictionary<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()>似乎没什么用,因为属性,可用于自定义如何字典被序列化,包括:

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:

例如,即使

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 允许自定义字典的序列化方式.

  • 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属性应用于字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆