class.m.tpl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. {{range .Tables}}
  2. #import "{{UnTitle (Mapper .Name)}}.h"
  3. #import "../NSDictionary+json.h"
  4. @implementation {{Mapper .Name}}
  5. {{$name := .Name}}
  6. +({{Mapper .Name}} *)paserJson:(NSString *)jsonString{
  7. NSError *error = nil;
  8. if (jsonString == nil){
  9. return nil;
  10. }
  11. id jsonObject = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
  12. if (jsonObject != nil && [jsonObject isKindOfClass:[NSDictionary class]]) {
  13. NSDictionary *json = (NSDictionary*)jsonObject;
  14. {{Mapper .Name}} *{{.Name}} = [[[{{Mapper .Name}} alloc]init] autorelease];
  15. {{range .Columns}}{{if or (eq (Type .) "int") (eq (Type .) "long")}}{{if eq .Name "id"}}{{$name}}.remote_id = [[json valueForKey:@"{{Mapper .Name}}"] longValue];
  16. {{else}}{{$name}}.{{Mapper .Name}} = [[json valueForKey:@"{{Mapper .Name}}"] longValue];
  17. {{end}}{{else if eq (Type .) "BOOL"}}{{$name}}.{{Mapper .Name}} = [[json valueForKey:@"{{Mapper .Name}}"] boolValue];
  18. {{else if eq (Type .) "float"}}{{$name}}.{{Mapper .Name}} = [[json valueForKey:@"{{Mapper .Name}}"] floatValue];
  19. {{else if eq (Type .) "double"}}{{$name}}.{{Mapper .Name}} = [[json valueForKey:@"{{Mapper .Name}}"] doubleValue];
  20. {{else}}{{$name}}.{{Mapper .Name}} = [json valueForKey:@"{{Mapper .Name}}"];
  21. {{end}}{{end}}
  22. return {{$name}};
  23. }
  24. return nil;
  25. }
  26. -(NSString *)toJson{
  27. NSMutableDictionary *json = [[NSMutableDictionary alloc]init];
  28. {{range .Columns}}{{if or (eq (Type .) "int") (eq (Type .) "long") (eq (Type .) "float") (eq (Type .) "double")}}{{if eq .Name "id"}}[json setValue:[NSNumber numberWithLong:self.remote_id] forKey:@"{{Mapper .Name}}"];
  29. {{else}}[json setValue:[NSNumber numberWithLong:self.{{Mapper .Name}}] forKey:@"{{Mapper .Name}}"];
  30. {{end}}{{else}}[json setValue:self.{{Mapper .Name}} forKey:@"{{Mapper .Name}}"];
  31. {{end}}{{end}}
  32. NSError *error = nil;
  33. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:kNilOptions error:&error];
  34. [json release];
  35. if (jsonData == nil){
  36. return @"";
  37. }
  38. return [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  39. }
  40. -(void)dealloc{
  41. {{range .Columns}}{{if eq (Type .) "NSString*"}}[_{{Mapper .Name}} release];
  42. {{end}}{{end}}
  43. [super dealloc];
  44. }
  45. @end
  46. {{end}}