Json in Objective-C

Here’s the translation of the JSON example from Go to Objective-C, formatted for Hugo:

Our first program will demonstrate JSON encoding and decoding in Objective-C. Here’s the full source code:

#import <Foundation/Foundation.h>

@interface Response : NSObject
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSArray<NSString *> *fruits;
@end

@implementation Response
@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // Encoding basic data types to JSON strings
        NSData *boolData = [NSJSONSerialization dataWithJSONObject:@YES options:0 error:nil];
        NSString *boolString = [[NSString alloc] initWithData:boolData encoding:NSUTF8StringEncoding];
        NSLog(@"%@", boolString);
        
        NSData *intData = [NSJSONSerialization dataWithJSONObject:@1 options:0 error:nil];
        NSString *intString = [[NSString alloc] initWithData:intData encoding:NSUTF8StringEncoding];
        NSLog(@"%@", intString);
        
        NSData *floatData = [NSJSONSerialization dataWithJSONObject:@2.34 options:0 error:nil];
        NSString *floatString = [[NSString alloc] initWithData:floatData encoding:NSUTF8StringEncoding];
        NSLog(@"%@", floatString);
        
        NSData *stringData = [NSJSONSerialization dataWithJSONObject:@"gopher" options:0 error:nil];
        NSString *stringString = [[NSString alloc] initWithData:stringData encoding:NSUTF8StringEncoding];
        NSLog(@"%@", stringString);
        
        // Encoding slices and maps
        NSArray *sliceData = @[@"apple", @"peach", @"pear"];
        NSData *sliceJSONData = [NSJSONSerialization dataWithJSONObject:sliceData options:0 error:nil];
        NSString *sliceJSONString = [[NSString alloc] initWithData:sliceJSONData encoding:NSUTF8StringEncoding];
        NSLog(@"%@", sliceJSONString);
        
        NSDictionary *mapData = @{@"apple": @5, @"lettuce": @7};
        NSData *mapJSONData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:nil];
        NSString *mapJSONString = [[NSString alloc] initWithData:mapJSONData encoding:NSUTF8StringEncoding];
        NSLog(@"%@", mapJSONString);
        
        // Encoding custom data types
        Response *res = [[Response alloc] init];
        res.page = 1;
        res.fruits = @[@"apple", @"peach", @"pear"];
        NSData *resJSONData = [NSJSONSerialization dataWithJSONObject:@{@"page": @(res.page), @"fruits": res.fruits} options:0 error:nil];
        NSString *resJSONString = [[NSString alloc] initWithData:resJSONData encoding:NSUTF8StringEncoding];
        NSLog(@"%@", resJSONString);
        
        // Decoding JSON data
        NSString *jsonString = @"{\"num\":6.13,\"strs\":[\"a\",\"b\"]}";
        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
        NSLog(@"%@", jsonDict);
        
        NSNumber *num = jsonDict[@"num"];
        NSLog(@"%@", num);
        
        NSArray *strs = jsonDict[@"strs"];
        NSString *str1 = strs[0];
        NSLog(@"%@", str1);
        
        // Decoding JSON into custom data types
        NSString *responseString = @"{\"page\": 1, \"fruits\": [\"apple\", \"peach\"]}";
        NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
        
        Response *response = [[Response alloc] init];
        response.page = [responseDict[@"page"] integerValue];
        response.fruits = responseDict[@"fruits"];
        
        NSLog(@"Page: %ld, First fruit: %@", (long)response.page, response.fruits[0]);
    }
    return 0;
}

This program demonstrates JSON encoding and decoding in Objective-C:

  1. We start by encoding basic data types (boolean, integer, float, string) to JSON.

  2. We then encode more complex structures like arrays and dictionaries (equivalent to slices and maps in Go).

  3. We create a custom Response class and demonstrate how to encode it to JSON.

  4. We show how to decode JSON data into Objective-C dictionaries and arrays.

  5. Finally, we decode JSON data into our custom Response class.

To run the program, save it as json_example.m and compile it with:

$ clang -framework Foundation json_example.m -o json_example
$ ./json_example

This will output the JSON representations of various data types and structures.

Note that Objective-C uses NSJSONSerialization for JSON operations, which is part of the Foundation framework. The @autoreleasepool block is used for memory management.

Unlike Go, Objective-C doesn’t have built-in JSON tags for properties. Instead, you typically use key-value coding or custom serialization methods for more complex JSON mapping scenarios.