I struggled in debug, for longer than I would care to admit, over an invalid enumeration value in a JSON string when deserializing an object.
I wonder if there is a method or switch for exposing the location in the JSON string (or JObject) of an invalid element.
I am using NewtonSoft JSON .NET library. NewtonSoft did not throw an exception when deserializing, but the resultant object was null (Nothing in VB.NET).
Here is the code:
Public Enum enumParameterSelectionType
None
Range
List
fromINIfile
CharacteristicField
End Enum
Public Class TestStepMetaData
Public Name As String
Public Caption As String
Public StepFunction As String
Public EnabledAlways As Boolean
Public ParameterCaption As String
Public ParameterSelectionType As enumParameterSelectionType
Public ParameterSelectionSource As String
<Newtonsoft.Json.JsonIgnore>
Public ParameterSelectionList As List(Of String)
Public StopOnFail As Boolean
Public QuantityFormat As String
Public LimitsRange As String
Public LoLimitEnabled As Boolean
Public HiLimitEnabled As Boolean
End Class
Public ReadOnly Property Comment As String = "Data about recipes and recipe steps"
'<Newtonsoft.Json.JsonIgnore>
Public Property Version As String = "2022-12-31"
Public Property TimestampFormat As String = "yyyy/MM/dd HH:mm:ss"
Public Property FileStampFormat As String = "yyyy-MM-dd_HHmmss"
Public Property FileDateFormat As String = "yyyy-MM-dd"
Public Property TestSteps As List(Of TestStepMetaData)
Private mvFilePath As String = ""
Public Shared Function LoadFromFile(LoadPath As String) As clsRecipeMetaData
Dim Result As clsRecipeMetaData = Nothing
Try
Dim SAndD As New AccutrolLibrary.jsonSandD(Of clsRecipeMetaData)
Result = SAndD.LoadFromFile(LoadPath)
Result.mvFilePath = LoadPath
Catch ex As Exception
AccutrolLibrary.SWLogging.LogException(ex)
End Try
Return Result
End Function
'And from jsonSandD.vb ...
Public Class jsonSandD(Of T)
Public Function LoadFromFile(LoadPath As String) As T
Dim NewObject As T = Nothing
Dim JSONobject As Newtonsoft.Json.Linq.JObject
Try
If IO.File.Exists(LoadPath) Then
Using infile As System.IO.StreamReader = System.IO.File.OpenText(LoadPath)
Using FileReader As Newtonsoft.Json.JsonTextReader = New Newtonsoft.Json.JsonTextReader(infile)
JSONobject = Newtonsoft.Json.Linq.JToken.ReadFrom(FileReader)
NewObject = JSONobject.ToObject(Of T)()
FileReader.Close()
End Using
End Using
End If
Catch ex As Exception
AccutrolLibrary.SWLogging.LogEvent(ex.Message, ex.StackTrace)
End Try
Return NewObject
End Function
End Class
And here are the contents of the JSON file I am trying to deserialize:
{
"Comment": "Definition of recipe steps for CHAMP LED fixtures",
"Version": "2022-12-31",
"TimestampFormat": "yyyy/MM/dd HH:mm:ss",
"FileStampFormat": "yyyy-MM-dd_HHmmss",
"FileDateFormat": "yyyy-MM-dd",
"TestSteps": [
{
"Name": "Test Volts",
"Caption": "Test Volts",
"StepFunction": "Test Volts",
"EnabledAlways": true,
"ParameterCaption": "Nominal Volts",
"ParameterSelectionType": "List",
"ParameterSelectionSource": "AC120 | AC208 | AC220 | AC230 | AC240 | AC277 |AC347 | AC380 | AC415 | AC480 | AC600 | DC12 | DC24 | DC125 | DC250",
"StopOnFail": true,
"QuantityFormat": "0.0",
"LimitsRange": "0.0:999.9",
"LoLimitEnabled": true,
"HiLimitEnabled": true
},
{
"Name": "Short Circuit",
"Caption": "Short Circuit",
"StepFunction": "Short Circuit",
"EnabledAlways": false,
"ParameterCaption": "",
"ParameterSelectionType": "",
"ParameterSelectionSource": "",
"StopOnFail": true,
"QuantityFormat": "0",
"LimitsRange": "0.0:0.0",
"LoLimitEnabled": false,
"HiLimitEnabled": false
}
]
}
In the JSON file, "ParameterSelectionType": "",
does not contain a valid value for the enumerated JSON element. Replacing the value as so, "ParameterSelectionType": "None",
solves the issue.
Similarly, changing the spelling of one of the element names in the JSON file, for instance changing “ParameterSelectionSource” to “Parameter5electionSource” processes, but leaves the value of the .NET variable, ParameterSelectionSource, null. No information is given that something is amiss.
I know that there are tools for determining if the JSON is valid JSON.
I would like to know if there is a method or tool for determining if the JSON is valid for the .NET object to which it is intended to be deserialized to.
I understand that there are validators that use JSON schema, but I am not familiar with those. I don’t know if they are what I am looking for. If they are, how can I convert the .NET class to a JSON schema, and how can I use one of these tools to validate the JSON for the .NET class?