I have a json document that when serializing from the database throws an exception. I have POCO classes that define a visit, chiefcomplaint and hpi with identical names to that of the json doc.
The json document to serialize:
[
{
"Id": 10032,
"DateOfService": "2020-11-20T00:00:00",
"ChiefComplaints": [
{
"Id": 2,
"Description": "cc1: description",
"Hpi": {
"Id": 2,
"AssociatedSignsAndSymptoms": "hpi1: associated signs and symptoms",
"Context": "hpi1: context",
"Duration": "hpi1: duration",
"Location": "hpi1: location",
"ModifyingFactors": "hpi1: modifying factors",
"Quality": "hpi1: quality",
"Severity": "hpi1: severity",
"Timing": "hpi1: timing"
}
}
]
},
{
"Id": 10034,
"DateOfService": "2020-10-21T23:09:02",
"ChiefComplaints": [
{
"Id": 5,
"Description": "cc2: description",
"Hpi": {
"Id": 5,
"AssociatedSignsAndSymptoms": "hpi2: associated signs and symptoms",
"Context": "hpi2: context",
"Duration": "hpi2: duration",
"Location": "hpi2: location",
"ModifyingFactors": "hpi2: modifying factors",
"Quality": "hpi2: quality",
"Severity": "hpi2: severity",
"Timing": "hpi2: timing"
}
},
{
"Id": 6,
"Description": "cc3: description",
"Hpi": {
"Id": 6,
"AssociatedSignsAndSymptoms": "hpi3: associated signs and symptoms",
"Context": "hpi3: context",
"Duration": "hpi3: duration",
"Location": "hpi3: location",
"ModifyingFactors": "hpi3: modifying factors",
"Quality": "hpi3: quality",
"Severity": "hpi3: severity",
"Timing": "hpi3: timing"
}
}
]
}
]
The POCO classes:
public class Visit : BaseEntity {
public DateTime DateOfService { get; private set; }
private readonly List<ChiefComplaint> _chiefComplaints = new List<ChiefComplaint>();
public IReadOnlyList<ChiefComplaint> ChiefComplaints => _chiefComplaints.ToList();
public Visit(long id, DateTime dateOfService, IEnumerable<ChiefComplaint> chiefComplaints) : base(id) {
this.DateOfService = dateOfService;
_chiefComplaints.AddRange(chiefComplaints);
}
}
public class ChiefComplaint : BaseEntity {
public string Description { get; private set; }
public HistoryOfPresentIllness Hpi { get; private set; }
public ChiefComplaint(long id, string description, HistoryOfPresentIllness hpi) : base(id) {
this.Description = description;
this.Hpi = hpi;
}
}
public class HistoryOfPresentIllness : BaseEntity {
public string Location { get; private set; }
public string Quality { get; private set; }
public string Severity { get; private set; }
public string Duration { get; private set; }
public string Timing { get; private set; }
public string Context { get; private set; }
public string ModifyingFactors { get; private set; }
public string AssociatedSignsAndSymptoms { get; private set; }
public HistoryOfPresentIllness(
long id,
string location,
string quality,
string severity,
string duration,
string timing,
string context,
string modifyingFactors,
string associatedSignsAndSymptoms) : base(id) {
this.Location = location;
this.Quality = quality;
this.Severity = severity;
this.Duration = duration;
this.Timing = timing;
this.Context = context;
this.ModifyingFactors = modifyingFactors;
this.AssociatedSignsAndSymptoms = associatedSignsAndSymptoms;
}
}
The exception occurs when the following code is executed:
JsonConvert.DeserializeObject<Visit>(value.ToString());
The exception that is generated:
Newtonsoft.Json.JsonSerializationException
HResult=0x80131500
Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Domain.Core.Entities.Visit' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
Source=Newtonsoft.Json
StackTrace:
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
at Data.Core.Repositories.VisitTypeHandler.Parse(Object value) in C:\Users\denniscpolley\source\repos\Data.Core\Repositories\VisitRepository.cs:line 15
at Dapper.SqlMapper.TypeHandler`1.Dapper.SqlMapper.ITypeHandler.Parse(Type destinationType, Object value) in /_/Dapper/SqlMapper.TypeHandler.cs:line 42
at Dapper.SqlMapper.<>c__DisplayClass169_0.<GetHandlerDeserializer>b__0(IDataReader reader) in /_/Dapper/SqlMapper.cs:line 1812
at Dapper.SqlMapper.<QueryAsync>d__33`1.MoveNext() in /_/Dapper/SqlMapper.Async.cs:line 438
This exception was originally thrown at this call stack:
[External Code]
Data.Core.Repositories.VisitTypeHandler.Parse(object) in VisitRepository.cs
[External Code]
User contributions licensed under CC BY-SA 3.0