Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. plutil -convert json Data.plist
  2.  
  3. plutil -convert json -o Data.json Data.plist
  4.  
  5. // convertPlistToJSON.m
  6. #import <Foundation/Foundation.h>
  7. #import "JSONKit.h"
  8.  
  9. int main(int argc, char *argv[]) {
  10. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  11.  
  12. if(argc != 3) { fprintf(stderr, "usage: %s FILE_PLIST FILE_JSONn", argv[0]); exit(5); }
  13.  
  14. NSString *plistFileNameString = [NSString stringWithUTF8String:argv[1]];
  15. NSString *jsonFileNameString = [NSString stringWithUTF8String:argv[2]];
  16.  
  17. NSError *error = NULL;
  18.  
  19. NSData *plistFileData = [NSData dataWithContentsOfFile:plistFileNameString options:0UL error:&error];
  20. if(plistFileData == NULL) {
  21. NSLog(@"Unable to read plist file. Error: %@, info: %@", error, [error userInfo]);
  22. exit(1);
  23. }
  24.  
  25. id plist = [NSPropertyListSerialization propertyListWithData:plistFileData options:NSPropertyListImmutable format:NULL error:&error];
  26. if(plist == NULL) {
  27. NSLog(@"Unable to deserialize property list. Error: %@, info: %@", error, [error userInfo]);
  28. exit(1);
  29. }
  30.  
  31. NSData *jsonData = [plist JSONDataWithOptions:JKSerializeOptionPretty error:&error];
  32. if(jsonData == NULL) {
  33. NSLog(@"Unable to serialize plist to JSON. Error: %@, info: %@", error, [error userInfo]);
  34. exit(1);
  35. }
  36.  
  37. if([jsonData writeToFile:jsonFileNameString options:NSDataWritingAtomic error:&error] == NO) {
  38. NSLog(@"Unable to write JSON to file. Error: %@, info: %@", error, [error userInfo]);
  39. exit(1);
  40. }
  41.  
  42. [pool release]; pool = NULL;
  43. return(0);
  44. }
  45.  
  46. shell% gcc -o convertPlistToJSON convertPlistToJSON.m JSONKit.m -framework Foundation
  47.  
  48. shell% convertPlistTOJSON
  49. usage: convertPlistToJSON FILE_PLIST FILE_JSON
  50.  
  51. shell% convertPlistTOJSON input.plist output.json
  52.  
  53. NSArray* array = [[NSArray arrayWithContentsOfFile:[@"~/input.plist" stringByExpandingTildeInPath]]retain];
  54. SBJsonWriter* writer = [[SBJsonWriter alloc] init];
  55. NSString* s = [[writer stringWithObject:array] retain];
  56. [s writeToFile:[@"~/output.json" stringByExpandingTildeInPath] atomically:YES];
  57. [array release];
  58.  
  59. NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:@"input.plist"];
  60.  
  61. NSError *error;
  62. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:plistDict options:NSJSONWritingPrettyPrinted error:&error];
  63. NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  64. [jsonString writeToFile:@"output.json" atomically:NO encoding:NSUTF8StringEncoding error:&error];
  65.  
  66. plutil -convert json -o output.json input.plist
  67.  
  68. plutil -convert xml1 input.json -o output.plist
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement