Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nested JSON objects in x-success callback #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions xcall/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ - (void)handleURLAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAp
// Get action name
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:YES];
NSString *actionName = components.path.lastPathComponent;



// Convert URL response parameters to JSON, set exit code
NSString *target;
NSString *output;
Expand Down Expand Up @@ -113,9 +112,22 @@ + (NSString *)jsonStringFromQueryItems:(NSArray<NSURLQueryItem *> *)queryItems
NSMutableDictionary *items = [NSMutableDictionary new];

// Convert each query item to a key/value pair
for (NSURLQueryItem *queryItem in queryItems)
items[queryItem.name] = queryItem.value ?: NSNull.null;

for (NSURLQueryItem *queryItem in queryItems) {
// queryItem.value may be a JSON object, so attempt to deserialize from JSON first
NSData *data = [queryItem.value dataUsingEncoding:NSUTF8StringEncoding];
if (data == nil) {
continue;
}

NSError *error = nil;
id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error) {
items[queryItem.name] = queryItem.value ?: NSNull.null;
} else {
[items setObject:object forKey:queryItem.name];
}
}

// Convert to JSON string
NSData *data = [NSJSONSerialization dataWithJSONObject:items options:NSJSONWritingPrettyPrinted error:NULL];
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Expand Down