JSONModel Lightning Talk

5,096 views

Published on

The slides from my Lightning Talk for the UIKonf in Berlin. (20 slides / 5min total time)

Published in: Technology

JSONModel Lightning Talk

  1. 1. Blah blahBlah blahBlah blahBlah blah !!!!#$!App Store 2008:-connected apps, showing realtime data! Whoa Nelly!- in your pocket! Boom!
  2. 2. Communication OK?XML? CSV? binary?<myverylongtagname>1</myverylongtagname>MyName, 1, 2, 5, left, TRUEOther, 5, 4, 3, left, FALSE010000100110010100100000011100110111010101110010011001010010000001110100011011110JSON
  3. 3. JSON is awesome1) it’s very SIMPLE!2) supported by tons of languages3) structural data
  4. 4. • Step1: Stackoverflow• Step2: click the link in approved answer• Step3:•How to JSON?
  5. 5. Simplest approach[[[[[json objectForKey:@”name”] objectAtIndex:1]objectAtIndex: 2] objectForKey:@”value”] boolValue]2009 code2013 code[json[@”name”][1][2][@”value”] boolValue]
  6. 6. never!- json ... is nil ...- EXC_BAD_ACCESS- ... unrecognized selector ... [NSString boolValue]- ... index out of bounds ...
  7. 7. Pros & Cons• very quick code turnaround!!!• no structure validation• no data validation• difficult to error handle• it’s the road to maintenanceHELL!evolution indicator
  8. 8. A better way!NSDictionary* json = [NSJSONSerialization ... ];if (json == nil) [self errorHandleEmptyJSON];if (json[@”name”] == nil)[self errorHandleEmptyName];NSArray* name = json[@”name”];if (name.count<1][self errorHandleWrongJSONStructure];NSNumber* value = name[1][2][@”value”];
  9. 9. Forget much? oh noes...Looks good so far, what’s left?- validate the data for all values you use- check if the incoming objects are of theexpected class type (yuicks!)- make sure you decend into the JSONaccording to the incoming structure
  10. 10. Pros & cons?• Horribly slow to write code• Difficult to maintain• How do you recover from errorsin the middle of the parsing process???• Better data validation• Better structure validationevolution indicator
  11. 11. Data models?The “M” in MVC standsfor “MODEL”Wrap up all the code inclasses for all your models
  12. 12. feels good!Your models feature custom initwhich reads the JSON and buildsup the model representationAll IF statements and all the errorhandling is handled within themodel classOther magic you coded at 4.17am on a Thursday
  13. 13. Pros & cons• Using a class instead of NSDictionary• OOP 4 Ever!• Very flexible code• Lot of redundant parsing code• Lot of implementation• Still harder to adapt to changesevolution indicator
  14. 14. Then what?Throughout 2012 I workedmainly on JSON poweredapplications.In December I was alreadyfed-up with:self.name = json[@”name”];Eureka!
  15. 15. JSONModel- import JSON- structure validation- data type validation- data type conversion- atomic operations- error handlingAutomatically does/has:
  16. 16. Import JSON.h .mFor relatively simple models eliminatesthe need to code in your .m file
  17. 17. Validation@property (strong, nonatomic) NSString* name;What you want is:What you should begetting might be:{“name”:“Name”}It is about what you want to have!
  18. 18. Conversion@property (strong, nonatomic) NSURL* blogSiteUrl;What you want is:What you get is:{“blogSiteUrl”:“http://www.yahoo.com”}It is about what you want to have!
  19. 19. JSONModelevolution indicatorTakes the hum-drum out ofcodingIt’s very fastGrowing (testing) fan base
  20. 20. Thankswww.JSONModel.comImage credits:http://www.clker.comhttp://clipart.christiansunite.comhttp://office.microsoft.com

×