fsprojects / FSharp.Json

F# JSON Reflection based serialization library
Apache License 2.0
226 stars 26 forks source link

'Non option field is missing' error when deserializing #67

Closed cmdoffing closed 1 year ago

cmdoffing commented 1 year ago

I'm getting a 'Non option field is missing' error when deserializing. My JSON looks like this:

{ "csvFilePath": "C:\Users\Mike\source\repos\Validator2\data\cars.csv", "delimiter": ",", "maxErrors": 2, "numHeaderLines": 0, "validationList": [ { "name": "Year", "desc": "The year the car was made.", "fieldType": "integer", "minLen": 4, "maxLen": 4, "minValue": 1930, "maxValue": 2023 }, { "name": "Make", "desc": "The make of the car.", "fieldType": "string", "minLen": 0, "maxLen": 35, "minValue": "A", "maxValue": "zzzz" }, { "name": "Model", "desc": "The make of the car.", "fieldType": "string", "minLen": 0, "maxLen": 35, "minValue": "A", "maxValue": "zzzz" }, { "name": "Description", "desc": "The make of the car.", "fieldType": "string", "minLen": 0, "maxLen": 35, "minValue": "A", "maxValue": "zzzz" }, { "name": "price", "desc": "how much it costs", "fieldType": "decimal", "minLen": 0, "maxLen": 6, "minValue": 100, "maxValue": 250000 } ] }

I'm deserializing into RawParamsRecord:

type RawValidationParams = { name : string desc : string option fieldType: string option //fieldCode: FieldCode option minLen : int option maxLen : int option minValue : decimal option maxValue : decimal option }

type RawParamsRecord = { csvFilePath : string delimiter : string maxErrors : int numHeaderLines: int validationList_: RawValidationParams list }

FSharp.Json.JsonDeserializationError: 'JSON Path: validationList_. Non option field is missing'

Originally some of the fields in the JSON file list (e.g., maxValue) were missing., so I filled in all of the values in the array of records. Since most of these are options I thought that should be OK. I'm at a complete loss as to what could be going on.

vsapronov commented 1 year ago

Hi Mike @cmdoffing. You have the field "validationList" in your record. Pay attention to "" in the end. But your JSON has the field named "validationList". Hence the error message: the validationList_ in no an option but it's missing in JSON...