将JsonDictionary属性应用于字典

编程入门 行业动态 更新时间:2024-10-27 12:35:27
本文介绍了将JsonDictionary属性应用于字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我尝试将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属性应用于字典

    本文发布于:2023-11-12 09:30:24,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1581115.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:应用于   字典   属性   JsonDictionary

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!