18 September 2011

Building and installing NSJSONSerialization on gnustep to parse JSON data

I was seeking for a JSON Parser for Objective-c on gnustep runtime..
Thanks to sudo rm -rf he directed me to use the source code from gnustep repository .

I got the source code for Three Files NSJSONSerialization.h, NSJSONSerialization.m and GSFastEnumeration.h, although not worked directly, I've did very small modifications

Steps to use:

Note: Steps below are executed on Mingw and should be used with little or no modifications on Linux.
  1.  Go and download the archive that contains the modified Three files from here.
    put them into a directory say in your home folder with the following path: ~/objc/json
  2. cd ~/objc/json and issue the following command to build the files:
    gcc -c `gnustep-config --objc-flags` *.m
    If no errors reported, this means the build process succeed. and the object file has been generated.
  3. Now let's put this files inside the gnustep installation, to do so, mv the the .h files NSJSONSerialization.h and GSFastEnumeration.h to /GNUstep/System/Library/Headers/Foundation
  4. Now let's create a static library and put it inside gnustep libraries path:
    Use the following command:
    ar q libNSJSONSerialization.a NSJSONSerialization.o Then move the generated libNSJSONSerialization.a file to /GNUstep/System/Library/Libraries/
Note that, we now copied two .h files and one .a file into gnustep installation directories. 
You now safe to remove all the directory ~/objc/json and all of its contents.

Now You are done installing the NSJSONSerialization as a static lib into gnustep.

Now let's write a test program. Now create a brand new directory into your home directory say ~/objc/json_test and create a file named json_main.m in this directory.
Put the following code in it:

  1. #import <Foundation/Foundation.h>
  2. #import <Foundation/NSJSONSerialization.h>
  4. int main(void)
  5. {
  6.     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc]init];
  8.     NSDictionary* dictionary = [NSDictionary dictionaryWithObjects:
  9.                 [NSArray arrayWithObjects: @"Muhammad", @"26", @"2011", @"Cairo, Egypt", 
 10.                 [NSArray arrayWithObjects: @"Arabic", @"English", nil] , nil]
 11.                                                             forKeys: 
 12.                 [NSArray arrayWithObjects: @"name", @"age", @"year", @"address", @"language", nil]];
 13.     NSError* error;
 14.     NSData* data = [NSJSONSerialization dataWithJSONObject: dictionary
 15.                 options:NSJSONWritingPrettyPrinted
 16.                 error: &error];
 17.     if (error)
 18.     {
 19.         NSLog(@"ERROR %@", error);
 20.     }else
 21.     {
 22.         NSString* jsonString = [[NSString alloc] initWithData: data 
 23.                                         encoding: NSASCIIStringEncoding];
 24.         NSLog(@"%@", jsonString);
 26.         [jsonString release];        
 27.     }
 29.     [pool release];
 30.     return 0;
 31. }
Now use the following command to compile it:
gcc `gnustep-config --objc-flags` *.m -L /GNUstep/System/Library/Libraries -lNSJSONSerialization -lobjc -lgnustep-base 
[The inclusion for the libraries should be exactly in this order]

Now run:
$ a.exe
2011-09-18 01:35:49.726 a[5696] {
"language": [
"year": "2011",
"name": "Muhammad",
"age": "26",
"address": "Cairo, Egypt"

Congratulations! you can use gnustep to parse json data.

Disclaimer: This article is distributed in the hope that it will be useful, but without any warranty. Use it on your own.
Post a Comment