I’m using json.net to perform handle my json deserialization in a IIS hosted restful service.
First, here are the objects I’m trying to work with.
[DataContract]
public class CreateSamplesRequest
{
[DataMember] public Guid SessionId { get; set; }
[DataMember] public SampleTemplateDTO Template { get; set; }
}
[DataContract]
public class SampleTemplateDTO
{
[DataMember] public int NumberOfSamples { get; set; }
[DataMember] public int CompanyId { get; set; }
[DataMember] public int SampleTypeId { get; set; }
[DataMember] public HmisDTO Hmis { get; set; }
[DataMember] public List<AttributeValueDTO> AttributeValues { get; set; }
}
[DataContract]
public class AttributeValueDTO
{
[DataMember] public int AttributeId { get; set; }
[DataMember] public string AttributeName { get; set; }
[DataMember] public string Value { get; set; }
}
[DataContract]
public class HmisDTO
{
[DataMember] public string Health { get; set; }
[DataMember] public string Flammability { get; set; }
[DataMember] public string Reactivity { get; set; }
[DataMember] public string Equipment { get; set; }
}
The help page asks for json in this format for the CreateSamplesRequest
{
"SessionId":"1627aea5-8e0a-4371-9022-9b504344e724",
"Template":{
"NumberOfSamples":2147483647,
"CompanyId":2147483647,
"SampleTypeId":2147483647,
"Hmis":{
"Health":"String content",
"Flammability":"String content",
"Reactivity":"String content",
"Equipment":"String content"
},
"AttributeValues":[{
"AttributeId":2147483647,
"AttributeName":"String content",
"Value":"String content"
}]
}
}
And this is what I’m actually sending:
{
"SessionId":"17aaec11-be28-4536-b5df-d98fbda91e91",
"Template":{
"NumberOfSamples":1,
"CompanyId":1,
"SampleTypeId":9,
"Hmis":{
"Health":"2",
"Flammability":"0",
"Reactivity":"0",
"Equipment":"E",
},
"AttributeValues":[
{"AttributeId":1,"AttributeName":"Item No.","Value":"MN0002NG"},
{"AttributeId":2,"AttributeName":"Lot No.","Value":"C4526"}
]
}
}
The problem I’m having is that the AttributeValues property of the SampleTemplateDTO object always ends up being ignored. With the above code, it will be null. If I instanciate it to a empty List<AttributeValueDTO> it will be an empty list. I’ve been banging my head against this for a few hours.
I’ve tried creating a service that just takes a List<AttributeValueDTO> and it works fine. I’ve tried creating a wrapper class for the AttributeValues and it still ends up as null. I’m completely stumped. Any ideas?
MOTHER OF GOD, I JUST WASTED 5 FREAKING HOURS ON A FREAKING COMMA. The trailing comma in the HMIS section was apparently telling json.net to stop parsing at that point.
When I submit this json:
Everything works great.