-
Notifications
You must be signed in to change notification settings - Fork 1
/
plist2json.m
93 lines (76 loc) · 3.1 KB
/
plist2json.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// plist2json / json2plist
//
// Clément Wehrung @cwehrung ([email protected])
// Adapted from johne: http://stackoverflow.com/questions/6066350/command-line-tool-for-converting-plist-to-json
//
// alternatively: plutil -convert json Data.plist
//
// usage: FILE_FROM (optional: FILE_TO)
// recognizes the file format from extension (.plist or .json)
// if no FILE_TO provided, FILE_TO is FILE_FROM with new file extension
#import <Foundation/Foundation.h>
#import "JSONKit.h"
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if(argc < 2) { fprintf(stderr, "usage: %s FILE_FROM (optional: FILE_TO)\n", argv[0]); exit(5); }
NSString *startFileNameString = [NSString stringWithUTF8String:argv[1]];
BOOL convertPlistToJSON = NO;
if ([startFileNameString.pathExtension isEqualToString:@"plist"])
{
convertPlistToJSON = YES;
}
else if (![startFileNameString.pathExtension isEqualToString:@"json"])
{
fprintf(stderr, "use .plist or .json files\n", argv[0]); exit(5);
}
NSString *endFileNameString = nil;
if (argc == 3)
{
endFileNameString = [NSString stringWithUTF8String:argv[2]];
} else
{
endFileNameString = [startFileNameString.stringByDeletingPathExtension stringByAppendingPathExtension:(convertPlistToJSON)?@"json":@"plist"];
}
NSError *error = NULL;
NSData *fileData = [NSData dataWithContentsOfFile:startFileNameString options:0UL error:&error];
if(fileData == NULL) {
NSLog(@"Unable to read file. Error: %@, info: %@", error, [error userInfo]);
exit(1);
}
NSData *writeData = nil;
if (convertPlistToJSON)
{
id plist = [NSPropertyListSerialization propertyListWithData:fileData options:NSPropertyListImmutable format:NULL error:&error];
if(plist == NULL) {
NSLog(@"Unable to deserialize property list. Error: %@, info: %@", error, [error userInfo]);
exit(1);
}
writeData = [plist JSONDataWithOptions:JKSerializeOptionPretty error:&error];
if(writeData == NULL) {
NSLog(@"Unable to serialize plist to JSON. Error: %@, info: %@", error, [error userInfo]);
exit(1);
}
}
else
{
JSONDecoder *decoder = [JSONDecoder decoder];
id json = [decoder objectWithData:fileData error:&error];
if (json == NULL)
{
NSLog(@"Unable to deserialize JSON list. Error: %@, info: %@", error, [error userInfo]);
exit(1);
}
writeData = [NSPropertyListSerialization dataWithPropertyList:json format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
if(writeData == NULL) {
NSLog(@"Unable to serialize JSON to plist. Error: %@, info: %@", error, [error userInfo]);
exit(1);
}
}
if([writeData writeToFile:endFileNameString options:NSDataWritingAtomic error:&error] == NO)
{
NSLog(@"Unable to write to file. Error: %@, info: %@", error, [error userInfo]);
exit(1);
}
[pool release]; pool = NULL;
return(0);
}