You have voted for fewer posts like these
AllSoftwareFileSieve

FileSieve and Newtonsoft JSON.NET

After the last Wake On LAN Ex release, FileSieve needs to be sorted out and released itself. This particular 4.20 release contains significant bug fixes that really needs to be put in the hands of FS users.

But when - just as before - I was just about to get the remaining issues fixed, I ran into another issue. A gigantic, showstopping issue that I'm absolutely confounded by.

Any time FileSieve does any (de)serialisation, JSON.NET throws exceptions; specifically, the .NET runtime throws exceptions. FileSieve worked absolutely fine forever - until this started happening yesterday. All of my other software that also (de)serialise are doing the same thing.

The code in-question (VB.NET):

Return JsonConvert.DeserializeObject(Of T)(tr.ReadToEnd, jss)

The exception is all of the following:

  • System.NotSupportedException: 'TypeConverter cannot convert from (null).'
  • System.NotSupportedException: 'TypeConverter cannot convert from System.String.'
  • System.NotSupportedException: 'CollectionConverter cannot convert from (null).'
  • System.NotSupportedException: 'CollectionConverter cannot convert from System.String.'

The error isn't technically being thrown by JSON.NET itself, but by .NET and so consequently JSON.NET isn't providing me with any useful information on what the underlying cause is.

I'd post an example class that's being exceptioned on, but they're a bit too involved. But don't forget: they were working completely fine previously.

I didn't make any changes to any of the classes being serialised, so why has this started happening? Who the hell knows. What I do know is that all development has halted.

This is with Newtonsoft JSON.NET 10.0.3.

Edit: It appears I've found one of the causes. It's when using the property attribute DefaultValue. In this case, it's when setting the default value for SystemColors. Here's the code below, in this one particular case.

    Private _customColor As Color = SystemColors.Info

    <Category(""), Description(""), Browsable(True), DefaultValue(GetType(SystemColors), "Info")>
    Public Property CustomColor() As Color
        Get
            Return Me._customColor
        End Get
        Set(ByVal value As Color)
            Me._customColor = value
        End Set
    End Property

Edit 2: Managed to fix all of the FileSieve issues; this means I have a ton of other projects I need to similarly fix. I hope it's just a bug introduced into JSON.NET that stops DefaultValue() from being used. Each complex property now needs a corresponding method that determines if it should be serialised or not.

To fix the above color property, remove the DefaultValue attribute from the property and use the following method within the same class as the property itself:

    ''' <summary>Determines if CustomColor should be serialised depending on if it's the same colour as its default value.</summary>
    ''' <returns><c>True</c> if CustomColor is different to the default and should be serialised, otherwise <c>False</c>.</returns>
    <EditorBrowsable(EditorBrowsableState.Never), Browsable(False)>
    Public Function ShouldSerializeCustomColor() As Boolean
        Return (Not Me._customColor.ToArgb.Equals(SystemColors.Info.ToArgb))
    End Function

The reason for the ToArgb() is as follows:

To compare colors based solely on their ARGB values, you should use the ToArgb method. This is because the Equals and Equality members determine equivalency using more than just the ARGB value of the colors. For example, Black and FromArgb(0,0,0) are not considered equal, since Black is a named color and FromArgb(0,0,0) is not.