1

I am using a payment module API to generate a token. I make a HttpWebRequest and I need to deserialize response as per my [DataContract]. The issue I am facing is the response contains a padding, "OnCallback".

`OnCallback(
           {"status":201, 
               "results{
                   "correlation_id":"124.1493307935056",
                   "status":"success",
                   "type":"FDToken",
                   "token":{
                       "type":"visa",
                       "cardholder_name":"JohnSmith",
                       "exp_date":"1030",
                       "value":"2305691117118291"}}})`

This is what I do to deserialize JSON without padding.

internal static T DeserializeJSon<T>(string jsonString)
{
    T                          obj;
    DataContractJsonSerializer serializer;

    serializer = new DataContractJsonSerializer(typeof(T));

    using (MemoryStream stream = 
            newMemoryStream(Encoding.UTF8.GetBytes(jsonString)))
            {
                obj = (T)serializer.ReadObject(stream);
                stream.Close();
            }

    return obj;
}

So when I do

response     = request.GetResponse();
dataStream   = response.GetResponseStream();
reader       = new StreamReader(dataStream);
responseText = reader.ReadToEnd();

try
{
    myTokenResponse = 
    ConsumerFirstData.DeserializeJSon<TokenResponse>(responseText);
}
catch (Exception ex)
{
    throw new Exception(ex.Message);
}

I get exception: ex.Message = "There was an error deserializing the object of type Demo.Json.TokenResponse. Encountered unexpected character 'O'."

Here is my contract:

public class TokenResponse
{
    [DataMember]
    public string status { get; set; }

    [DataMember]
    public Results results { get; set; }
}

public class Results
{
    [DataMember]
    public string correlation_id { get; set; }

    [DataMember]
    public string status { get; set; }

    [DataMember]
    public string type { get; set; }

    [DataMember]
    public Token token { get; set; }
}

What can I do to unwrap JSONP into JSON? Or how to directly deserialize JSONP?

  • 3
    I would just remove the JS method before deserializing it. Find the index of the first ( and remove everything to that point and before. Then do .TrimEnd().TrimEnd(')') and it should get you what you need. – Daniel Lorenz Apr 28 '17 at 12:38
  • Thank you Daniel for your response. That is indeed a solution, but that is a worst-case solution for me. I am looking for something more formal, without modifying my response string. Something defined in the System.Runtime.Serialization.Json? Or internal c# functionality to handle JSONP. – Marij Siddiqui Apr 28 '17 at 12:47
  • There really isn't any other way that I know of. JSONP has no standard, so the method can be whatever they want. Thus, you can really just do it this way and/or regex. – Daniel Lorenz Apr 28 '17 at 13:21
  • Thank you Daniel for helping out. Your response is much appreciated. – Marij Siddiqui Apr 28 '17 at 13:26

0 Answers0