title: JSON Write Error - Invalid type when dealing with custom classes
body: <p>I have a number of custom classes, subclassed from NSObject, that I'm trying to write to a JSON file. My top level class, called Survey, is a singleton, and one of its properties is addressArray, which contains instances of another class called Address. </p>
  
  <p>There can be any number of addresses in Survey's addressArray, and I'm trying to write all the data to a JSON file. My code is as follows: </p>
  
  <pre><code>//In both Survey.m and Address.m, I have a method like this: 
  - (NSDictionary *)surveyDictionaryRepresentation
  {
      NSMutableDictionary *dictionary = [NSMutableDictionary new];
      dictionary[@"name"] = [self name] ? : [NSNull null];
      dictionary[@"emailAddress"] = [self emailAddress] ? : [NSNull null];
      dictionary[@"addressArray"] = [self addressArray] ? : [NSNull null];
      dictionary[@"storage"] = [NSNumber numberWithInteger:[self storage]] ? : [NSNull null];
      dictionary[@"extraStops"] = [NSNumber numberWithBool:[self extraStops]] ? : [NSNull null];
  
      return [NSDictionary dictionaryWithDictionary:dictionary];
  }
  </code></pre>
  
  <p>Then, in the view controller, I have the following in the ViewDidLoad method. </p>
  
  <pre><code>NSArray *surveys = @[[[Survey sharedInstance] surveyDictionaryRepresentation]];
  NSMutableArray *addresses = [[NSMutableArray alloc]init];
  for (Address *address in [[Survey sharedInstance]addressArray]){
      //What the hell was I thinking? 
      [addresses addObject:[addressaddressDictionaryRepresentation]];
  }
  NSDictionary *dictionary = @{@"Survey": surveys, @"Address":addresses};
  NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
  NSLog(@"%@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
  </code></pre>
  
  <p>Now, I'm getting the following error: 
      Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Address)'</p>
  
  <p>I can see why there would be a problem. Because addressArray, containing instances of Address, is in Survey, there seems to be a problem with adding the Address data to the JSON object created for Survey. I'm not sure how to get around this problem. After using breaking points to step through the program, I see that the crash is occurring after it attempts to execute the line: <code>NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];</code></p>
  
  <p>For what it's worth, I NSLog'd the addresses array after looping through it and running the addressesDictionaryRepresentation method on its contents, and it logged perfectly, showing all the properties and all the values perfectly. I just cant get it into the JSON document. Any suggestions? Help is very much appreciated. </p>
  
  <p>EDIT: First few lines of the log of AddressArray: </p>
  
  <pre><code>  2015-02-18 11:21:29.122 [70958:920733] (
      {
      aCCOI = "-1";
      aCfloorNumber = "&lt;null&gt;";
      activity = 0;
      addressType = "-1";
      allowedToReserveDock = "-1";
  </code></pre>
  
  <p>EDIT 2: The only other code in Survey.m is that which establishes the singleton: 
      + (instancetype)sharedInstance {
      static Survey *_instance;
      static dispatch_once_t onceToken;
      dispatch_once(&amp;onceToken, ^{
          _instance = [[Survey alloc] init];
      });
      return _instance;
      }</p>
  
  <p>EDIT3: 
      if ([self addressArray]){
          NSMutableArray *addresses = [[NSMutableArray alloc]init];
          for (Address *address in [[Survey sharedInstance]addressArray]){
              [addresses addObject:[address addressDictionaryRepresentation]];
          }
          dictionary[@"addressArray"] = [self addressArray];
      } else {
          dictionary[@"addressArray"] = [NSNull null];
      }</p>