Problem reading JSON index into C#

Problem reading JSON index into C#

Postby glugglug » Wed Oct 21, 2015 8:48 pm

When reading the JSON into an object, arrays can be read into an array or List type, maps can be read into a dictionary or a custom class type with named fields.

But a class, (or a field within the class) must be one or the other, not both.

The "data" element of each image index response is an array on success, but a map on error.

Example snippet from a response I just received:

Code: Select all
{"programID:"EP00521262","data":[{"width":"135","height":"180","uri":"assets\/p3605402_b_v2_ae.jpg","size":"Sm","aspect":"3x4","category":"Banner-L2","text":"yes","tier":"Sport"....},{"width":"360","height":"270","uri":"assets\/o62_l_h3_aa.png","size":"Md","aspect":"4x3","category":"Logo","text":"no","primary":"true","tier":"Sport"},...]}{"programID:"EP02233233","data":{"errorCode":1013,"errorMessage":"invalid_tms_id"}},
{"programID:"EP00007921","data":[{"width":"270","height":"360","uri":"assets\/p684607_b_v3_aa.jpg","size":"Md","aspect":"3x4","category":"Banner-L2","text":"yes","primary":"true","tier":"Sport"},...]}


My code is expecting a List<SDProgramImageResponse> where SDProgramImageResponse looks like this:

Code: Select all
        public class SDProgramImageResponse
        {

            [DataMember(Name ="programID", IsRequired = true)]
            public string programID { get; set; }
            [DataMember(Name = "code")]
            public int code { get; set; }
            [DataMember(Name ="message")]
            public string message { get; set; }
            [DataMember(Name ="data", IsRequired =true)]
            public SDProgramImageData[] data { get; set; }
        }


The data element fails to parse so an exception is thrown for parsing the whole batch of 500 responses because the error is not in an array like the rest of the "data" fields.

Possible solution: Could the error be it's own field, and data empty or missing for programIDs with failures?
glugglug
 
Posts: 16
Joined: Wed Oct 14, 2015 8:52 pm

Re: Problem reading JSON index into C#

Postby rkulagow » Wed Oct 21, 2015 9:08 pm

Yes, possibly. But I need to check my code to determine where the 1013 error is coming from. Did you use the hasImages boolean to request images and get the 1013, or did you just make the request without checking the boolean first? That's two different code paths so I just need to make sure I'm chasing down the right path.
rkulagow
SD Staff
 
Posts: 911
Joined: Tue Aug 14, 2007 3:15 pm

Re: Problem reading JSON index into C#

Postby glugglug » Wed Oct 21, 2015 9:14 pm

The corresponding program response has "hasImageArtwork":true.

In my first attempt I did make the request without checking, after getting the error I added a filter to only include IDs where hasImageArtwork=true and still got the error.

Is that the hasImages boolean or is there some other?

Edit: found something interesting querying the program info I cached in a local DB.

There are 6 episodes in this series with program info downloaded:

EP022332330045 and EP022332330046 have hasImageArtwork=true
EP022332330048, EP022332330018, EP022332330040, and EP022332330047 do not.
glugglug
 
Posts: 16
Joined: Wed Oct 14, 2015 8:52 pm

Re: Problem reading JSON index into C#

Postby rkulagow » Wed Oct 21, 2015 9:35 pm

Thanks for the details. Let me dig deeper into the issue and I'll let you know what I find.
rkulagow
SD Staff
 
Posts: 911
Joined: Tue Aug 14, 2007 3:15 pm

Re: Problem reading JSON index into C#

Postby glugglug » Wed Oct 21, 2015 10:11 pm

I got my code to work around the format by reading "data" into a generic object and parsing each element separately.

Hacky, but it works:

Code: Select all
        [DataContract]
        public class SDProgramImageResponse
        {

            [DataMember(Name = "programID", IsRequired = true)]
            public string programID { get; set; }
            [DataMember(Name = "code")]
            public int code { get; set; }
            [DataMember(Name = "message")]
            public string message { get; set; }
            [IgnoreDataMember]
            public List<SDProgramImageData> imageData;
            [IgnoreDataMember]
            private object data_;
            [DataMember(Name = "data", IsRequired = true)]
            public object data
            {
                get { return data_; }
                set
                {
                    try
                    {
                        data_ = value;
                        // This is just a wrapper for JsonConvert.DeserializeObject - it was more complicated before I found json.net
                        imageData = JSONClient.Deserialize<List<SDProgramImageData>>(data_.ToString());
                        data_ = imageData;
                    }
                    catch (Exception exc)
                    {
                        Console.WriteLine("Failed to deserialize image info: {0}", value.ToString());
                    }
                }
            }
           
glugglug
 
Posts: 16
Joined: Wed Oct 14, 2015 8:52 pm


Return to Developer

Who is online

Users browsing this forum: No registered users and 4 guests