8 Replies - 391 Views - Last Post: 28 October 2012 - 03:49 PM Rate Topic: -----

#1 hawksprite  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 11-September 09

Casting Issue

Posted 28 October 2012 - 11:31 AM

I've been extremely frustrated with a problem I've been having.

I'll see if I can give some premise as to what's wrong.

This snippet creates a RootObject by using Newtonsoft Json library.

        var rootResult = new RootObject();
        rootResult = JsonConvert.DeserializeObject<RootObject>(raw);



This is the class RootObject where the data is held (They are object's because the Rootobject isn't always going to get all the data, this object simply has all the possibilities.
    public class RootObject
    {
        public object name { get; set; }
        public object permalink { get; set; }
        public object crunchbase_url { get; set; }
        public object homepage_url { get; set; }
        public object blog_url { get; set; }
        public object blog_feed_url { get; set; }
        public object twitter_username { get; set; }
        public object category_code { get; set; }
        public object number_of_employees { get; set; }
        public object founded_year { get; set; }
        public object founded_month { get; set; }
        public object founded_day { get; set; }
        public object deadpooled_year { get; set; }
        public object deadpooled_month { get; set; }
        public object deadpooled_day { get; set; }
        public object deadpooled_url { get; set; }
        public object tag_list { get; set; }
        public object alias_list { get; set; }
        public object email_address { get; set; }
        public object phone_number { get; set; }
        public object description { get; set; }
        public object created_at { get; set; }
        public object updated_at { get; set; }
        public object overview { get; set; }
        public object image { get; set; }
        public List<object> products { get; set; }
        public List<object> relationships { get; set; }
        public List<object> competitions { get; set; }
        public List<object> providerships { get; set; }
        public object total_money_raised { get; set; }
        public List<object> funding_rounds { get; set; }
        public List<object> investments { get; set; }
        public object acquisition { get; set; }
        public List<object> acquisitions { get; set; }
        public List<object> offices { get; set; }
        public List<object> milestones { get; set; }
        public object ipo { get; set; }
        public List<object> video_embeds { get; set; }
        public List<object> screenshots { get; set; }
        public List<object> external_links { get; set; }
    }



However later whenever I get a result that I know has the object image in the input this fails
        Image image = (Image)input.image;



Also I found that you can make it work by making
int integer;
int? integer;

This works but not with classes
Image? image; <- Still throws an error

Any suggestions/help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Casting Issue

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Casting Issue

Posted 28 October 2012 - 11:43 AM

As a test try setting the type for Image


27 public Image image { get; set; }

If it still fails then its the data you are using to set the value. IN other words, take the casting out of the testing scenario and see if the problem remains.
Was This Post Helpful? 0
  • +
  • -

#3 hawksprite  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 11-September 09

Re: Casting Issue

Posted 28 October 2012 - 11:54 AM

When I set it to Public Image image it works if the input has an Image in the Json field.

However it throws an error when the input doesn't have an image field.
Was This Post Helpful? 0
  • +
  • -

#4 hawksprite  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 11-September 09

Re: Casting Issue

Posted 28 October 2012 - 12:01 PM

I think I found a solution here's what I did.

I desiralized it all into just objects like before.

Then I think the objects like this
              if (input.image != null)
              {
               // result.image = (Image)input.image;
                result.image = JsonConvert.DeserializeObject<Image>(JsonConvert.SerializeObject(input.image));
            }



Sorry if the format is bad, basically it looks at the object and if it's not null it converts it back to raw JSON then back into the actual object. This way it's only casting to the real object if there's data in the JSON response.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Casting Issue

Posted 28 October 2012 - 12:27 PM

So in other words...

All your properties are null when the RootObject is created.
You're not initializing any of your variables.
You get an exception when you try to cast 'null' to a non-nullable type like Image.

That makes perfectly good sense.

Many of your properties really could/should be more specific about their type. And should do some validation when set. For example:


13 public object founded_month { get; set; }

The rest of your program has to know what to expect of this property in order to make use of it. I assume its going to be an in between 1-12. So why not make this an int instead of an object. Set the default value to 0 indicating it is unset. And qualify your set to require a value between 1-12

private int _founded_month = 0;
public int founded_month
{
   get { return _founded_month;}
   set
   {
      if (value < 1 && value > 12) return; // Not valid
      if (_founded_month != value)
      {
          _founded_month = value;
          RaisePropertyChanged("founded_month");// Implementation of INotifyPropertyChanged
      }
}

Was This Post Helpful? 1
  • +
  • -

#6 hawksprite  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 11-September 09

Re: Casting Issue

Posted 28 October 2012 - 12:38 PM

I probably should have structured the rootobject's better in the first place to handle it.

The classes themselves were actually generated with this:
http://json2csharp.com/

Thanks for your help with the problem, much appreciated.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Casting Issue

Posted 28 October 2012 - 12:46 PM

Ewwww.... Tool generated coding... Get it off me... get it off me....
Was This Post Helpful? 1
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,450
  • Joined: 29-May 08

Re: Casting Issue

Posted 28 October 2012 - 01:38 PM

Integer? is shorthand for Nullable<Integer>. Since value type always have a value, how do you represent the absence of a value? This where nullable come in.
Reference type can express the absence of a reference with null.
But this leads to an issue that null maybe a valid state, so this often leads to creating of MayBe<T> or Option<T> type .

This post has been edited by AdamSpeight2008: 28 October 2012 - 01:40 PM

Was This Post Helpful? 2
  • +
  • -

#9 hawksprite  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 137
  • Joined: 11-September 09

Re: Casting Issue

Posted 28 October 2012 - 03:49 PM

View PosttlhIn`toq, on 28 October 2012 - 08:46 PM, said:

Ewwww.... Tool generated coding... Get it off me... get it off me....


Haha yeah it's not the greatest but it saves me a good bit of time and frustration.

View PostAdamSpeight2008, on 28 October 2012 - 09:38 PM, said:

Integer? is shorthand for Nullable<Integer>. Since value type always have a value, how do you represent the absence of a value? This where nullable come in.
Reference type can express the absence of a reference with null.
But this leads to an issue that null maybe a valid state, so this often leads to creating of MayBe<T> or Option<T> type .


Thanks for the info I was attempting to make a Class<T> at one point but I was just doing something wrong so I ended up going with the solution I posted about earlier.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1