0

This is my first REST service, basically it is a self hosted service that is listening on a port for a PUT call which will have a payload of JSON data. I want to convert that to a native object, I thought the serializer could do it automatically but it's not working.

This SO does work for me: WCF REST Service JSON Post data

I can get the JSON data as a stream and output it to a log just fine, but now I am wondering if WCF can automagically put the JSON data to my object.

The JSON data is this.

{
  "ID": 639073,
  "PatientFirst": "John",
  "PatientLast": "Smith",
  "Birthdate": "1/1/2000",
  "PtEmail":"rdejournett@medfusion.com",
  "Sex": "F",
  "mrn": ""
}

I have an ApptData class which is this.

[DataContract]
class ApptData {
    [DataMember]
    internal string ID = "";

    [DataMember]
    internal string PatientFirst = "";
    [DataMember]
    internal string PatientLast = "";
    [DataMember]
    internal string Birthdate = "";
    [DataMember]
    internal string PtEmail = "";
    [DataMember]
    internal string Sex = "";
    [DataMember]
    internal string mrn = "";

    public ApptData() { }
}

And the iContract is this. I want to use the stuff that is commented out.

[ServiceContract]
interface iContract
{
    [OperationContract]


    [WebInvoke(Method = "PUT", UriTemplate = "CheckinPUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    [OperationContract]
    //Stream PatientCheckinWithDemos(ApptData data);
    Stream PatientCheckinWithDemos(Stream streamdata);

}
Community
  • 1
  • 1
Rob
  • 2,363
  • 7
  • 36
  • 54
  • Can you post the code for deserializing the JSON? Even if it's not working? And out of curiousity, why are you using fields instead of properties? – Tim Dec 10 '14 at 19:36
  • That's basically my question, do I need to create code to deserialize it or is there a way to do it somewhat easily? – Rob Dec 10 '14 at 20:10
  • `do I need to create code to deserialize it or is there a way to do it somewhat easily?` No, Your commented interface is the right way to go and it should work. If I remember correctly, you should use `WebHttpBinding`. – L.B Dec 10 '14 at 20:48

1 Answers1

0

I have a solution wherein I do the deserializing. It's not too hard.

Here is the class that impliments iContract.

    public Stream PatientCheckinWithDemos(Stream streamdata) {
        try {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ApptData));
            ApptData data = (ApptData)ser.ReadObject(streamdata);
            c.WriteToLog("Appt id is " + data.ID);
            c.WriteToLog("PatientFirst is " + data.PatientFirst);
            c.WriteToLog("PatientLast is " + data.PatientLast);
            c.WriteToLog("PtEmail is " + data.PtEmail);
            c.WriteToLog("Sex is " + data.Sex);
            c.WriteToLog("Birthdate is " + data.Birthdate);

            return GetStream("something");
        } catch (Exception e) {
            c.WriteToLog("Failure during Database Call to PatientCheckinWithDemos " + e.Message);
            return GetStream("");

        }
    }
Rob
  • 2,363
  • 7
  • 36
  • 54