NSObject+AGSSBJson.h in v100

691
2
12-12-2016 08:35 AM
MaximoMobile
New Contributor

NSObject+AGSSBJson.h has been removed from v100 ??

how can we use ags_JSONRepresentation  ??

0 Kudos
2 Replies
RyanOlson1
Esri Contributor

SBJson was a third party library that has no longer been necessary the last few years now that Apple has shipped NSJSONSerialization. Objects that support AGSJSONSerializable have a toJSON method you can call. That will give you a JSON object (most likely an NSDictionary, but could be an NSArray). You can use that JSON object with NSJSONSerialization to get NSData:

AGSObject -> NSDictionary -> NSJSONSerialization -> NSData

You can also go the other way if you have NSData:

NSData -> NSJSONSerialization -> NSDictionary -> AGSObject

Here is sample code showing you how to do both:

-(void)testJSONSerialization{
    
    // Example 1: AGSPoint to NSData of JSON string...
    //
    AGSPoint *point = [AGSPoint pointWithX:34 y:-117 spatialReference:[AGSSpatialReference WGS84]];
    id jsonObject = [point toJSON:nil];
    NSData *data = [NSJSONSerialization dataWithJSONObject:jsonObject options:0 error:nil];
    
    // do whatever you want with the NSData...
    // ...
    // ...
    // For example print out a json string:
    NSLog(@"the json string: %@", [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
    
    // Example 2: NSData of JSON String to AGSPoint
    //
    id jsonObject2 = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    AGSPoint *point2 = (AGSPoint*)[AGSPoint fromJSON:jsonObject2 error:nil];
    
    // print out and test for equality
    NSLog(@"point1: %@", point);
    NSLog(@"point2: %@", point2);
    NSAssert([point isEqualToGeometry:point2], @"point comparison failed...");
}
0 Kudos
MaximoMobile
New Contributor

Thank you so much

0 Kudos