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:
- Use the built in DataContractJsonSerializer instead (at the expense of some other conveniences of JSON.NET).
- Use a custom ExpandableObjectConverter (see below).
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
Applying the above converter works flawlessly with JSON.NET and with the property grid control!