How to identify location of invalid JSON in NewtonSoft.JSON

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật