将JSON.NET与ExpandableObjectConverter一起使用时出现问题 [英] Problems using JSON.NET with ExpandableObjectConverter

查看:330
本文介绍了将JSON.NET与ExpandableObjectConverter一起使用时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下类:

<TypeConverter(GetType(ExpandableObjectConverter))>
<DataContract()>
Public Class Vector3

   <DataMember()> Public Property X As Double
   <DataMember()> Public Property Y As Double
   <DataMember()> Public Property Z As Double

   Public Overrides Function ToString() As String

      Return String.Format("({0}, {1}, {2})",
                           Format(X, "0.00"),
                           Format(Y, "0.00"),
                           Format(Z, "0.00"))

   End Function

End Class

使用DataContractJsonSerializer,我收到了以下JSON:

Using the DataContractJsonSerializer I receive the following JSON as expected:

{
  "Vector": {
    "X": 1.23,
    "Y": 4.56,
    "Z": 7.89
  }
}

但是,JSON.NET产生:

However, JSON.NET produces:

{
  "Vector": "(1.23, 4.56, 7.89)"
}

如果我从类中删除了ExpandableObjectConverter属性,则JSON.NET会产生预期的结果(与DataContractJsonSerializer相同).

If I remove the ExpandableObjectConverter attribute from the class, JSON.NET produces results as expected (same as DataContractJsonSerializer).

不幸的是,我需要ExpandableObjectConverter,这样该类才能与属性网格一起使用.

Unfortunately I need the ExpandableObjectConverter so that the class works with a property grid.

有什么办法告诉JSON.NET忽略ExpandableObjectConverters?

Is there any way to tell JSON.NET to ignore ExpandableObjectConverters?

我更喜欢使用JSON.NET而不是DataContractJsonSerializer,因为将枚举序列化为其字符串表示形式要容易得多.

I prefer to use JSON.NET instead of DataContractJsonSerializer because it is much easier to serialize enums to their string representations.

推荐答案

尽管我欣赏Rivers的回答,但我确实在寻找一种解决方案,该解决方案会自动忽略所有可扩展的对象转换器(例如DataContractJsonSerializer所做的那样),而不是为该对象构建自定义JsonConverter每个有罪的班级.

Although I appreciate Rivers' answer I am really looking for a solution that ignores all expandable object converters automatically (like the DataContractJsonSerializer does) rather than building a custom JsonConverter for each offending class.

我找到了以下两种解决方案:

I have found the following two solutions:

  1. 改为使用内置的DataContractJsonSerializer(以牺牲JSON.NET的其他便利性为代价).
  2. 使用自定义的ExpandableObjectConverter(请参见下文).

由于默认的ExpandableObjectConverter支持与字符串之间的转换,因此JSON.NET使用字符串对类进行序列化.为了解决这个问题,我创建了自己的可扩展对象转换器,该转换器不允许与字符串之间的转换.

Since the default ExpandableObjectConverter supports converting to/from string, JSON.NET is serializing the class with a string. To counteract this I have created my own expandable object converter which does not allow conversions to/from string.

Imports System.ComponentModel

Public Class SerializableExpandableObjectConverter
   Inherits ExpandableObjectConverter

   Public Overrides Function CanConvertTo(context As System.ComponentModel.ITypeDescriptorContext, destinationType As System.Type) As Boolean

      If destinationType Is GetType(String) Then
         Return False
      Else
         Return MyBase.CanConvertTo(context, destinationType)
      End If

   End Function

   Public Overrides Function CanConvertFrom(context As System.ComponentModel.ITypeDescriptorContext, sourceType As System.Type) As Boolean

      If sourceType Is GetType(String) Then
         Return False
      Else
         Return MyBase.CanConvertFrom(context, sourceType)
      End If

   End Function

End Class

应用上述转换器可完美地与JSON.NET和属性网格控件配合使用!

Applying the above converter works flawlessly with JSON.NET and with the property grid control!

这篇关于将JSON.NET与ExpandableObjectConverter一起使用时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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