wcf json array with variable name not deserializing

87 Views Asked by At

I have created a WCF service that accepts, for example, this json:

{
  "type": "despatch",
  "client_ref": "ORD00001",
  "date_despatched": "2015-01-01T17:27:38+00:00",
  "postage_method": "Royal Mail 1st Class Packet",
  "tracking_number": "GB1010101010A",
  "items": [
    {
      "client_ref": "ABC123",
      "quantity": 1,
      "serial_numbers": {
        "ABC123": [
          "SERIAL0001"
        ]
      }
    }
  ]
}

An issue arises when trying to deserialize the "serial_numbers" element as the array element name is unknown - it is a variable that changes with each entry in "items".

The service works fine except the "serial_numbers" data is blank after deserialization.

Here is the operation contract definition

        [OperationContract(Name = "JAJTracking")]
        [WebInvoke(Method = "POST", UriTemplate = "JAJTracking?W={W}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        JANDJResponse ProcessTracking(string W, JAndJDespatch jjdesp);

I've tried using a "Dictionary" to store the serial_numbers data as from searching this appeared to be the only way to go.

    public class JAndJDespatch
    {
        [DataMember]
        public string type { get; set; }
        [DataMember]
        public string client_ref { get; set; }
        [DataMember]
        public string date_despatched { get; set; }
        [DataMember]
        public string postage_method { get; set; }
        [DataMember]
        public string tracking_number { get; set; }
        [DataMember]
        public List<JAndJDespatchItems> items { get; set; }
    }

    public class JAndJDespatchItems
    {
        [DataMember]
        public object UsedForKnownTypeSerializationObject;
        [DataMember]
        public string client_ref { get; set; }
        [DataMember]
        public Int32 quantity { get; set; }
        [JsonExtensionData]
        public Dictionary<string, object> serial_numbers { get; set; }
    }

If I hard-code the json message as a string in the 'ProcessTracking' method & run it the deserialization process works perfectly & I can access the serial_number data.

However when the json is passed through the wcf service the rest of the data is deserialized but the "serial_numbers" data is missing.

Is there something obvious I am doing wrong or is it not possible to deserialize this type json data using a WCF service?

If not possible to do this via wcf how can I do it?

0

There are 0 best solutions below