Skip to content
Browse files

added wifi bssid location lookup

  • Loading branch information...
1 parent 111247a commit 880ba9aff677020cf35a983fe1cde0d8da324414 @nst committed Oct 31, 2010
Showing with 5,685 additions and 598 deletions.
  1. +8 −1 Classes/SPSourceKeyboardTVC.m
  2. +5 −1 Classes/SPSourceLocationTVC.h
  3. +14 −0 Classes/SPSourceLocationTVC.m
  4. +2 −1 Classes/SPSourceWifiTVC.h
  5. +12 −0 Classes/SPSourceWifiTVC.m
  6. +10 −10 Classes/SpyPhoneAppDelegate.m
  7. 0 {Classes → EXIF}/EXF.h
  8. 0 {Classes → EXIF}/EXFConstants.h
  9. 0 {Classes → EXIF}/EXFGPS.h
  10. 0 {Classes → EXIF}/EXFGPS.m
  11. 0 {Classes → EXIF}/EXFHandlers.h
  12. 0 {Classes → EXIF}/EXFHandlers.m
  13. 0 {Classes → EXIF}/EXFJFIF.h
  14. 0 {Classes → EXIF}/EXFJFIF.m
  15. 0 {Classes → EXIF}/EXFJpeg.h
  16. 0 {Classes → EXIF}/EXFJpeg.m
  17. 0 {Classes → EXIF}/EXFLogging.h
  18. 0 {Classes → EXIF}/EXFMetaData.h
  19. 0 {Classes → EXIF}/EXFMetaData.m
  20. 0 {Classes → EXIF}/EXFMutableMetaData.h
  21. 0 {Classes → EXIF}/EXFTagDefinitionHolder.h
  22. 0 {Classes → EXIF}/EXFTagDefinitionHolder.m
  23. 0 {Classes → EXIF}/EXFUtils.h
  24. 0 {Classes → EXIF}/EXFUtils.m
  25. 0 {Classes → FMDB}/FMDatabase.h
  26. 0 {Classes → FMDB}/FMDatabase.m
  27. 0 {Classes → FMDB}/FMDatabaseAdditions.h
  28. 0 {Classes → FMDB}/FMDatabaseAdditions.m
  29. 0 {Classes → FMDB}/FMResultSet.h
  30. 0 {Classes → FMDB}/FMResultSet.m
  31. +67 −0 JSON/JSON.h
  32. +24 −0 JSON/LICENSE
  33. +53 −0 JSON/NSObject+SBJSON.h
  34. +44 −0 JSON/NSObject+SBJSON.m
  35. +48 −0 JSON/NSString+SBJSON.h
  36. +45 −0 JSON/NSString+SBJSON.m
  37. +28 −0 JSON/Readme.markdown
  38. +86 −0 JSON/SBJsonBase.h
  39. +78 −0 JSON/SBJsonBase.m
  40. +86 −0 JSON/SBJsonParser.h
  41. +516 −0 JSON/SBJsonParser.m
  42. +144 −0 JSON/SBJsonStreamWriter.h
  43. +509 −0 JSON/SBJsonStreamWriter.m
  44. +111 −0 JSON/SBJsonWriter.h
  45. +96 −0 JSON/SBJsonWriter.m
  46. +58 −0 JSON/SBProxyForJson.h
  47. +26 −0 OUILookupTool/OUILookupTool.h
  48. +110 −0 OUILookupTool/OUILookupTool.m
  49. +3,258 −330 SpyPhone.xcodeproj/nst.pbxuser
  50. +100 −50 SpyPhone.xcodeproj/nst.perspectivev3
  51. +147 −74 SpyPhone.xcodeproj/project.pbxproj
  52. +0 −131 UIApplication_TVOut.m
View
9 Classes/SPSourceKeyboardTVC.m
@@ -63,7 +63,14 @@ - (void)loadData {
NSMutableSet *set = [NSMutableSet set];
NSString *dir = @"/var/mobile/Library/Keyboard/";
- NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:dir];
+ NSError *error = nil;
+ NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dir error:&error];
+
+ if(dirContents == nil) {
+ NSLog(@"-- error: %@", error);
+ return;
+ }
+
for(NSString *filePath in dirContents) {
if(![filePath hasSuffix:@".dat"]) continue;
NSArray *a = [self wordsInDictionaryCacheFileAtPath:[dir stringByAppendingPathComponent:filePath]];
View
6 Classes/SPSourceLocationTVC.h
@@ -11,6 +11,8 @@
#import <MapKit/MapKit.h>
#import "SPSourceTVC.h"
+@class CLLocation;
+
@interface SPSourceLocationTVC : SPSourceTVC /* <MKReverseGeocoderDelegate> */ {
NSArray *items;
// MKReverseGeocoder *geo;
@@ -20,11 +22,13 @@
NSString *locDateString;
NSString *timezone;
NSArray *cities;
+
+ CLLocation *cachedLocationFromMaps;
}
//@property (nonatomic, retain) MKReverseGeocoder *geo;
@property (nonatomic, retain) NSString *geoString;
-
+@property (nonatomic, retain) CLLocation *cachedLocationFromMaps;
@property (nonatomic, retain) NSArray *cities;
@property (nonatomic, retain) NSString *locString;
@property (nonatomic, retain) NSString *locDateString;
View
14 Classes/SPSourceLocationTVC.m
@@ -9,6 +9,8 @@
#import "SPSourceLocationTVC.h"
#import <CoreLocation/CoreLocation.h>
+#import "SPMapVC.h"
+#import "SPImageAnnotation.h"
@implementation SPSourceLocationTVC
@@ -18,6 +20,16 @@ @implementation SPSourceLocationTVC
@synthesize locString;
@synthesize locDateString;
@synthesize timezone;
+@synthesize cachedLocationFromMaps;
+
+- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
+ if(indexPath.section == 0 && indexPath.row == 0 && cachedLocationFromMaps) {
+ SPMapVC *mapVC = [[SPMapVC alloc] initWithNibName:@"SPMapVC" bundle:[NSBundle mainBundle]];
+ [self.navigationController pushViewController:mapVC animated:YES];
+ SPImageAnnotation *annotation = [SPImageAnnotation annotationWithCoordinate:cachedLocationFromMaps.coordinate date:nil path:nil];
+ [mapVC addAnnotation:annotation];
+ }
+}
- (void)loadData {
if(contentsDictionaries) return;
@@ -26,6 +38,7 @@ - (void)loadData {
NSDictionary *d = [NSDictionary dictionaryWithContentsOfFile:path];
NSData *data = [d valueForKey:@"UserLocation"];
CLLocation *loc = data ? [NSKeyedUnarchiver unarchiveObjectWithData:data] : nil;
+ self.cachedLocationFromMaps = loc;
self.locString = @"";
self.locDateString = @"";
if(loc) {
@@ -62,6 +75,7 @@ - (void)dealloc {
[items release];
// [geo release];
[geoString release];
+ [cachedLocationFromMaps release];
[super dealloc];
}
/*
View
3 Classes/SPSourceWifiTVC.h
@@ -9,8 +9,9 @@
#import <UIKit/UIKit.h>
#import "SPSourceTVC.h"
+#import "OUILookupTool.h"
-@interface SPSourceWifiTVC : SPSourceTVC {
+@interface SPSourceWifiTVC : SPSourceTVC <OUILookupToolDelegate> {
}
View
12 Classes/SPSourceWifiTVC.m
@@ -8,10 +8,14 @@
//
#import "SPSourceWifiTVC.h"
+#import "OUILookupTool.h"
@implementation SPSourceWifiTVC
- (void)loadData {
+
+ //[OUILookupTool lookupBSSID:@"0:30:bd:97:7:72" delegate:self];
+
if(contentsDictionaries) return;
self.contentsDictionaries = [NSMutableArray array];
@@ -24,6 +28,8 @@ - (void)loadData {
if(!a) return;
for(NSDictionary *d in a) {
+ [OUILookupTool locateWifiAccessPoint:d delegate:self];
+
NSString *name = [d valueForKey:@"SSID_STR"];
NSData *joined = [d valueForKey:@"lastJoined"];
@@ -35,4 +41,10 @@ - (void)loadData {
}
}
+#pragma mark OUILookupTool
+
+- (void)OUILookupTool:(OUILookupTool *)ouiLookupTool didLocateAccessPoint:(NSDictionary *)ap {
+ //NSLog(@"-- %@", ap);
+}
+
@end
View
20 Classes/SpyPhoneAppDelegate.m
@@ -9,10 +9,11 @@
#import "SpyPhoneAppDelegate.h"
-@interface UIApplication (tvout)
-- (void) startTVOut;
-@end
-
+//
+//@interface UIApplication (tvout)
+//- (void) startTVOut;
+//@end
+//
@implementation SpyPhoneAppDelegate
@synthesize window;
@@ -23,12 +24,11 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application {
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
- /*
- BOOL isTVOutEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"TVOutEnabled"];
- if(isTVOutEnabled) {
- [[UIApplication sharedApplication] startTVOut];
- }
- */
+
+// BOOL isTVOutEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"TVOutEnabled"];
+// if(isTVOutEnabled) {
+// [[TVOutManager sharedInstance] startTVOut];
+// }
}
View
0 Classes/EXF.h → EXIF/EXF.h
File renamed without changes.
View
0 Classes/EXFConstants.h → EXIF/EXFConstants.h
File renamed without changes.
View
0 Classes/EXFGPS.h → EXIF/EXFGPS.h
File renamed without changes.
View
0 Classes/EXFGPS.m → EXIF/EXFGPS.m
File renamed without changes.
View
0 Classes/EXFHandlers.h → EXIF/EXFHandlers.h
File renamed without changes.
View
0 Classes/EXFHandlers.m → EXIF/EXFHandlers.m
File renamed without changes.
View
0 Classes/EXFJFIF.h → EXIF/EXFJFIF.h
File renamed without changes.
View
0 Classes/EXFJFIF.m → EXIF/EXFJFIF.m
File renamed without changes.
View
0 Classes/EXFJpeg.h → EXIF/EXFJpeg.h
File renamed without changes.
View
0 Classes/EXFJpeg.m → EXIF/EXFJpeg.m
File renamed without changes.
View
0 Classes/EXFLogging.h → EXIF/EXFLogging.h
File renamed without changes.
View
0 Classes/EXFMetaData.h → EXIF/EXFMetaData.h
File renamed without changes.
View
0 Classes/EXFMetaData.m → EXIF/EXFMetaData.m
File renamed without changes.
View
0 Classes/EXFMutableMetaData.h → EXIF/EXFMutableMetaData.h
File renamed without changes.
View
0 Classes/EXFTagDefinitionHolder.h → EXIF/EXFTagDefinitionHolder.h
File renamed without changes.
View
0 Classes/EXFTagDefinitionHolder.m → EXIF/EXFTagDefinitionHolder.m
File renamed without changes.
View
0 Classes/EXFUtils.h → EXIF/EXFUtils.h
File renamed without changes.
View
0 Classes/EXFUtils.m → EXIF/EXFUtils.m
File renamed without changes.
View
0 Classes/FMDatabase.h → FMDB/FMDatabase.h
File renamed without changes.
View
0 Classes/FMDatabase.m → FMDB/FMDatabase.m
File renamed without changes.
View
0 Classes/FMDatabaseAdditions.h → FMDB/FMDatabaseAdditions.h
File renamed without changes.
View
0 Classes/FMDatabaseAdditions.m → FMDB/FMDatabaseAdditions.m
File renamed without changes.
View
0 Classes/FMResultSet.h → FMDB/FMResultSet.h
File renamed without changes.
View
0 Classes/FMResultSet.m → FMDB/FMResultSet.m
File renamed without changes.
View
67 JSON/JSON.h
@@ -0,0 +1,67 @@
+/*
+ Copyright (C) 2009-2010 Stig Brautaset. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ @mainpage A strict JSON parser and generator for Objective-C
+
+ JSON (JavaScript Object Notation) is a lightweight data-interchange
+ format. This framework provides two apis for parsing and generating
+ JSON. One standard object-based and a higher level api consisting of
+ categories added to existing Objective-C classes.
+
+ This framework does its best to be as strict as possible, both in what it accepts and what it generates. For example, it does not support trailing commas in arrays or objects. Nor does it support embedded comments, or anything else not in the JSON specification. This is considered a feature.
+
+ @section Links
+
+ @li <a href="http://stig.github.com/json-framework">Project home page</a>.
+ @li Online version of the <a href="http://stig.github.com/json-framework/api">API documentation</a>.
+
+*/
+
+
+// This setting of 1 is best if you copy the source into your project.
+// The build transforms the 1 to a 0 when building the framework and static lib.
+
+#if 1
+
+#import "SBJsonParser.h"
+#import "SBJsonWriter.h"
+#import "SBJsonStreamWriter.h"
+#import "NSObject+SBJSON.h"
+#import "NSString+SBJSON.h"
+
+#else
+
+#import <JSON/SBJsonParser.h>
+#import <JSON/SBJsonWriter.h>
+#import <JSON/SBJsonStreamWriter.h>
+#import <JSON/NSObject+SBJSON.h>
+#import <JSON/NSString+SBJSON.h>
+
+#endif
View
24 JSON/LICENSE
@@ -0,0 +1,24 @@
+Copyright (C) 2007-2010 Stig Brautaset. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+* Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+* Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
53 JSON/NSObject+SBJSON.h
@@ -0,0 +1,53 @@
+/*
+ Copyright (C) 2009 Stig Brautaset. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Foundation/Foundation.h>
+
+
+/**
+ @brief Adds JSON generation to Foundation classes
+
+ This is a category on NSObject that adds methods for returning JSON representations
+ of standard objects to the objects themselves. This means you can call the
+ -JSONRepresentation method on an NSArray object and it'll do what you want.
+ */
+@interface NSObject (NSObject_SBJSON)
+
+/**
+ @brief Returns a string containing the receiver encoded in JSON.
+
+ This method is added as a category on NSObject but is only actually
+ supported for the following objects:
+ @li NSDictionary
+ @li NSArray
+ */
+- (NSString *)JSONRepresentation;
+
+@end
+
View
44 JSON/NSObject+SBJSON.m
@@ -0,0 +1,44 @@
+/*
+ Copyright (C) 2009 Stig Brautaset. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "NSObject+SBJSON.h"
+#import "SBJsonWriter.h"
+
+@implementation NSObject (NSObject_SBJSON)
+
+- (NSString *)JSONRepresentation {
+ SBJsonWriter *jsonWriter = [SBJsonWriter new];
+ NSString *json = [jsonWriter stringWithObject:self];
+ if (!json)
+ NSLog(@"-JSONRepresentation failed. Error trace is: %@", [jsonWriter errorTrace]);
+ [jsonWriter release];
+ return json;
+}
+
+@end
View
48 JSON/NSString+SBJSON.h
@@ -0,0 +1,48 @@
+/*
+ Copyright (C) 2009 Stig Brautaset. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Foundation/Foundation.h>
+
+/**
+ @brief Adds JSON parsing methods to NSString
+
+This is a category on NSString that adds methods for parsing the target string.
+*/
+@interface NSString (NSString_SBJSON)
+
+/**
+ @brief Returns the NSDictionary or NSArray represented by the current string's JSON representation.
+
+ Returns the dictionary or array represented in the receiver, or nil on error.
+
+ Returns the NSDictionary or NSArray represented by the current string's JSON representation.
+ */
+- (id)JSONValue;
+
+@end
View
45 JSON/NSString+SBJSON.m
@@ -0,0 +1,45 @@
+/*
+ Copyright (C) 2007-2009 Stig Brautaset. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "NSString+SBJSON.h"
+#import "SBJsonParser.h"
+
+@implementation NSString (NSString_SBJSON)
+
+- (id)JSONValue
+{
+ SBJsonParser *jsonParser = [SBJsonParser new];
+ id repr = [jsonParser objectWithString:self];
+ if (!repr)
+ NSLog(@"-JSONValue failed. Error trace is: %@", [jsonParser errorTrace]);
+ [jsonParser release];
+ return repr;
+}
+
+@end
View
28 JSON/Readme.markdown
@@ -0,0 +1,28 @@
+JSON Framework
+==============
+
+JSON is a light-weight data interchange format that's easy to read and
+write for humans and computers alike. This framework implements a strict
+JSON parser and generator in Objective-C.
+
+Features
+--------
+
+* BSD license.
+* Easy-to-use API.
+* Strict parsing & generation.
+* Stack of error available in case of failure so you can easily figure out what is wrong.
+* Optional pretty-printing of JSON output.
+* Optionally sorted dictionary keys in JSON output.
+* Configurable recursion depth for parsing, for added security.
+
+Links
+-----
+
+* The GitHub [project page][src].
+* The online [API documentation][api].
+* The new [website][web].
+
+[api]: http://stig.github.com/json-framework/api
+[web]: http://stig.github.com/json-framework
+[src]: http://github.com/stig/json-framework
View
86 JSON/SBJsonBase.h
@@ -0,0 +1,86 @@
+/*
+ Copyright (C) 2009 Stig Brautaset. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Foundation/Foundation.h>
+
+extern NSString * SBJSONErrorDomain;
+
+
+enum {
+ EUNSUPPORTED = 1,
+ EPARSENUM,
+ EPARSE,
+ EFRAGMENT,
+ ECTRL,
+ EUNICODE,
+ EDEPTH,
+ EESCAPE,
+ ETRAILCOMMA,
+ ETRAILGARBAGE,
+ EEOF,
+ EINPUT
+};
+
+/**
+ @brief Common base class for parsing & writing.
+
+ This class contains the common error-handling code and option between the parser/writer.
+ */
+@interface SBJsonBase : NSObject {
+ NSMutableArray *errorTrace;
+
+@protected
+ NSUInteger depth, maxDepth;
+}
+
+/**
+ @brief The maximum recursing depth.
+
+ Defaults to 512. If the input is nested deeper than this the input will be deemed to be
+ malicious and the parser returns nil, signalling an error. ("Nested too deep".) You can
+ turn off this security feature by setting the maxDepth value to 0.
+ */
+@property NSUInteger maxDepth;
+
+/**
+ @brief Return an error trace, or nil if there was no errors.
+
+ Note that this method returns the trace of the last method that failed.
+ You need to check the return value of the call you're making to figure out
+ if the call actually failed, before you know call this method.
+ */
+ @property(copy,readonly) NSArray* errorTrace;
+
+/// @internal for use in subclasses to add errors to the stack trace
+- (void)addErrorWithCode:(NSUInteger)code description:(NSString*)str;
+
+/// @internal for use in subclasess to clear the error before a new parsing attempt
+- (void)clearErrorTrace;
+
+@end
View
78 JSON/SBJsonBase.m
@@ -0,0 +1,78 @@
+/*
+ Copyright (C) 2009 Stig Brautaset. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "SBJsonBase.h"
+NSString * SBJSONErrorDomain = @"org.brautaset.JSON.ErrorDomain";
+
+
+@implementation SBJsonBase
+
+@synthesize errorTrace;
+@synthesize maxDepth;
+
+- (id)init {
+ self = [super init];
+ if (self)
+ self.maxDepth = 512;
+ return self;
+}
+
+- (void)dealloc {
+ [errorTrace release];
+ [super dealloc];
+}
+
+- (void)addErrorWithCode:(NSUInteger)code description:(NSString*)str {
+ NSDictionary *userInfo;
+ if (!errorTrace) {
+ errorTrace = [NSMutableArray new];
+ userInfo = [NSDictionary dictionaryWithObject:str forKey:NSLocalizedDescriptionKey];
+
+ } else {
+ userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
+ str, NSLocalizedDescriptionKey,
+ [errorTrace lastObject], NSUnderlyingErrorKey,
+ nil];
+ }
+
+ NSError *error = [NSError errorWithDomain:SBJSONErrorDomain code:code userInfo:userInfo];
+
+ [self willChangeValueForKey:@"errorTrace"];
+ [errorTrace addObject:error];
+ [self didChangeValueForKey:@"errorTrace"];
+}
+
+- (void)clearErrorTrace {
+ [self willChangeValueForKey:@"errorTrace"];
+ [errorTrace release];
+ errorTrace = nil;
+ [self didChangeValueForKey:@"errorTrace"];
+}
+
+@end
View
86 JSON/SBJsonParser.h
@@ -0,0 +1,86 @@
+/*
+ Copyright (C) 2009 Stig Brautaset. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Foundation/Foundation.h>
+#import "SBJsonBase.h"
+
+/**
+ @brief The JSON parser class.
+
+ JSON is mapped to Objective-C types in the following way:
+
+ @li Null -> NSNull
+ @li String -> NSMutableString
+ @li Array -> NSMutableArray
+ @li Object -> NSMutableDictionary
+ @li Boolean -> NSNumber (initialised with -initWithBool:)
+ @li Number -> (NSNumber | NSDecimalNumber)
+
+ Since Objective-C doesn't have a dedicated class for boolean values, these turns into NSNumber
+ instances. These are initialised with the -initWithBool: method, and
+ round-trip back to JSON properly. (They won't silently suddenly become 0 or 1; they'll be
+ represented as 'true' and 'false' again.)
+
+ As an optimisation short JSON integers turn into NSNumber instances, while complex ones turn into NSDecimalNumber instances.
+ We can thus avoid any loss of precision as JSON allows ridiculously large numbers.
+
+ */
+@interface SBJsonParser : SBJsonBase {
+
+@private
+ const char *c;
+}
+
+/**
+ @brief Return the object represented by the given string
+
+ Returns the object represented by the passed-in string or nil on error. The returned object can be
+ a string, number, boolean, null, array or dictionary.
+
+ @param repr the json string to parse
+ */
+- (id)objectWithString:(NSString *)repr;
+
+/**
+ @brief Return the object represented by the given string
+
+ Returns the object represented by the passed-in string or nil on error. The returned object can be
+ a string, number, boolean, null, array or dictionary.
+
+ @param jsonText the json string to parse
+ @param error pointer to an NSError object to populate on error
+ */
+
+- (id)objectWithString:(NSString*)jsonText
+ error:(NSError**)error;
+
+
+@end
+
+
View
516 JSON/SBJsonParser.m
@@ -0,0 +1,516 @@
+/*
+ Copyright (C) 2009,2010 Stig Brautaset. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "SBJsonParser.h"
+
+@interface SBJsonParser ()
+
+- (BOOL)scanValue:(NSObject **)o;
+
+- (BOOL)scanRestOfArray:(NSMutableArray **)o;
+- (BOOL)scanRestOfDictionary:(NSMutableDictionary **)o;
+- (BOOL)scanRestOfNull:(NSNull **)o;
+- (BOOL)scanRestOfFalse:(NSNumber **)o;
+- (BOOL)scanRestOfTrue:(NSNumber **)o;
+- (BOOL)scanRestOfString:(NSMutableString **)o;
+
+// Cannot manage without looking at the first digit
+- (BOOL)scanNumber:(NSNumber **)o;
+
+- (BOOL)scanHexQuad:(unichar *)x;
+- (BOOL)scanUnicodeChar:(unichar *)x;
+
+- (BOOL)scanIsAtEnd;
+
+@end
+
+#define skipWhitespace(c) while (isspace(*c)) c++
+#define skipDigits(c) while (isdigit(*c)) c++
+
+
+@implementation SBJsonParser
+
+static char ctrl[0x22];
+
+
++ (void)initialize {
+ ctrl[0] = '\"';
+ ctrl[1] = '\\';
+ for (int i = 1; i < 0x20; i++)
+ ctrl[i+1] = i;
+ ctrl[0x21] = 0;
+}
+
+- (id)objectWithString:(NSString *)repr {
+ [self clearErrorTrace];
+
+ if (!repr) {
+ [self addErrorWithCode:EINPUT description:@"Input was 'nil'"];
+ return nil;
+ }
+
+ depth = 0;
+ c = [repr UTF8String];
+
+ id o;
+ if (![self scanValue:&o]) {
+ return nil;
+ }
+
+ // We found some valid JSON. But did it also contain something else?
+ if (![self scanIsAtEnd]) {
+ [self addErrorWithCode:ETRAILGARBAGE description:@"Garbage after JSON"];
+ return nil;
+ }
+
+ NSAssert1(o, @"Should have a valid object from %@", repr);
+
+ // Check that the object we've found is a valid JSON container.
+ if (![o isKindOfClass:[NSDictionary class]] && ![o isKindOfClass:[NSArray class]]) {
+ [self addErrorWithCode:EFRAGMENT description:@"Valid fragment, but not JSON"];
+ return nil;
+ }
+
+ return o;
+}
+
+- (id)objectWithString:(NSString*)repr error:(NSError**)error {
+ id tmp = [self objectWithString:repr];
+ if (tmp)
+ return tmp;
+
+ if (error)
+ *error = [self.errorTrace lastObject];
+ return nil;
+}
+
+
+/*
+ In contrast to the public methods, it is an error to omit the error parameter here.
+ */
+- (BOOL)scanValue:(NSObject **)o
+{
+ skipWhitespace(c);
+
+ switch (*c++) {
+ case '{':
+ return [self scanRestOfDictionary:(NSMutableDictionary **)o];
+ break;
+ case '[':
+ return [self scanRestOfArray:(NSMutableArray **)o];
+ break;
+ case '"':
+ return [self scanRestOfString:(NSMutableString **)o];
+ break;
+ case 'f':
+ return [self scanRestOfFalse:(NSNumber **)o];
+ break;
+ case 't':
+ return [self scanRestOfTrue:(NSNumber **)o];
+ break;
+ case 'n':
+ return [self scanRestOfNull:(NSNull **)o];
+ break;
+ case '-':
+ case '0'...'9':
+ c--; // cannot verify number correctly without the first character
+ return [self scanNumber:(NSNumber **)o];
+ break;
+ case '+':
+ [self addErrorWithCode:EPARSENUM description: @"Leading + disallowed in number"];
+ return NO;
+ break;
+ case 0x0:
+ [self addErrorWithCode:EEOF description:@"Unexpected end of string"];
+ return NO;
+ break;
+ default:
+ [self addErrorWithCode:EPARSE description: @"Unrecognised leading character"];
+ return NO;
+ break;
+ }
+
+ NSAssert(0, @"Should never get here");
+ return NO;
+}
+
+- (BOOL)scanRestOfTrue:(NSNumber **)o
+{
+ if (!strncmp(c, "rue", 3)) {
+ c += 3;
+ *o = [NSNumber numberWithBool:YES];
+ return YES;
+ }
+ [self addErrorWithCode:EPARSE description:@"Expected 'true'"];
+ return NO;
+}
+
+- (BOOL)scanRestOfFalse:(NSNumber **)o
+{
+ if (!strncmp(c, "alse", 4)) {
+ c += 4;
+ *o = [NSNumber numberWithBool:NO];
+ return YES;
+ }
+ [self addErrorWithCode:EPARSE description: @"Expected 'false'"];
+ return NO;
+}
+
+- (BOOL)scanRestOfNull:(NSNull **)o {
+ if (!strncmp(c, "ull", 3)) {
+ c += 3;
+ *o = [NSNull null];
+ return YES;
+ }
+ [self addErrorWithCode:EPARSE description: @"Expected 'null'"];
+ return NO;
+}
+
+- (BOOL)scanRestOfArray:(NSMutableArray **)o {
+ if (maxDepth && ++depth > maxDepth) {
+ [self addErrorWithCode:EDEPTH description: @"Nested too deep"];
+ return NO;
+ }
+
+ *o = [NSMutableArray arrayWithCapacity:8];
+
+ for (; *c ;) {
+ id v;
+
+ skipWhitespace(c);
+ if (*c == ']' && c++) {
+ depth--;
+ return YES;
+ }
+
+ if (![self scanValue:&v]) {
+ [self addErrorWithCode:EPARSE description:@"Expected value while parsing array"];
+ return NO;
+ }
+
+ [*o addObject:v];
+
+ skipWhitespace(c);
+ if (*c == ',' && c++) {
+ skipWhitespace(c);
+ if (*c == ']') {
+ [self addErrorWithCode:ETRAILCOMMA description: @"Trailing comma disallowed in array"];
+ return NO;
+ }
+ }
+ }
+
+ [self addErrorWithCode:EEOF description: @"End of input while parsing array"];
+ return NO;
+}
+
+- (BOOL)scanRestOfDictionary:(NSMutableDictionary **)o
+{
+ if (maxDepth && ++depth > maxDepth) {
+ [self addErrorWithCode:EDEPTH description: @"Nested too deep"];
+ return NO;
+ }
+
+ *o = [NSMutableDictionary dictionaryWithCapacity:7];
+
+ for (; *c ;) {
+ id k, v;
+
+ skipWhitespace(c);
+ if (*c == '}' && c++) {
+ depth--;
+ return YES;
+ }
+
+ if (!(*c == '\"' && c++ && [self scanRestOfString:&k])) {
+ [self addErrorWithCode:EPARSE description: @"Object key string expected"];
+ return NO;
+ }
+
+ skipWhitespace(c);
+ if (*c != ':') {
+ [self addErrorWithCode:EPARSE description: @"Expected ':' separating key and value"];
+ return NO;
+ }
+
+ c++;
+ if (![self scanValue:&v]) {
+ NSString *string = [NSString stringWithFormat:@"Object value expected for key: %@", k];
+ [self addErrorWithCode:EPARSE description: string];
+ return NO;
+ }
+
+ [*o setObject:v forKey:k];
+
+ skipWhitespace(c);
+ if (*c == ',' && c++) {
+ skipWhitespace(c);
+ if (*c == '}') {
+ [self addErrorWithCode:ETRAILCOMMA description: @"Trailing comma disallowed in object"];
+ return NO;
+ }
+ }
+ }
+
+ [self addErrorWithCode:EEOF description: @"End of input while parsing object"];
+ return NO;
+}
+
+- (BOOL)scanRestOfString:(NSMutableString **)o
+{
+ // if the string has no control characters in it, return it in one go, without any temporary allocations.
+ size_t len = strcspn(c, ctrl);
+ if (len && *(c + len) == '\"')
+ {
+ *o = [[[NSMutableString alloc] initWithBytes:(char*)c length:len encoding:NSUTF8StringEncoding] autorelease];
+ c += len + 1;
+ return YES;
+ }
+
+ *o = [NSMutableString stringWithCapacity:16];
+ do {
+ // First see if there's a portion we can grab in one go.
+ // Doing this caused a massive speedup on the long string.
+ len = strcspn(c, ctrl);
+ if (len) {
+ // check for
+ id t = [[NSString alloc] initWithBytesNoCopy:(char*)c
+ length:len
+ encoding:NSUTF8StringEncoding
+ freeWhenDone:NO];
+ if (t) {
+ [*o appendString:t];
+ [t release];
+ c += len;
+ }
+ }
+
+ if (*c == '"') {
+ c++;
+ return YES;
+
+ } else if (*c == '\\') {
+ unichar uc = *++c;
+ switch (uc) {
+ case '\\':
+ case '/':
+ case '"':
+ break;
+
+ case 'b': uc = '\b'; break;
+ case 'n': uc = '\n'; break;
+ case 'r': uc = '\r'; break;
+ case 't': uc = '\t'; break;
+ case 'f': uc = '\f'; break;
+
+ case 'u':
+ c++;
+ if (![self scanUnicodeChar:&uc]) {
+ [self addErrorWithCode:EUNICODE description: @"Broken unicode character"];
+ return NO;
+ }
+ c--; // hack.
+ break;
+ default:
+ [self addErrorWithCode:EESCAPE description: [NSString stringWithFormat:@"Illegal escape sequence '0x%x'", uc]];
+ return NO;
+ break;
+ }
+ CFStringAppendCharacters((CFMutableStringRef)*o, &uc, 1);
+ c++;
+
+ } else if (*c < 0x20) {
+ [self addErrorWithCode:ECTRL description: [NSString stringWithFormat:@"Unescaped control character '0x%x'", *c]];
+ return NO;
+
+ } else {
+ NSLog(@"should not be able to get here");
+ }
+ } while (*c);
+
+ [self addErrorWithCode:EEOF description:@"Unexpected EOF while parsing string"];
+ return NO;
+}
+
+- (BOOL)scanUnicodeChar:(unichar *)x
+{
+ unichar hi, lo;
+
+ if (![self scanHexQuad:&hi]) {
+ [self addErrorWithCode:EUNICODE description: @"Missing hex quad"];
+ return NO;
+ }
+
+ if (hi >= 0xd800) { // high surrogate char?
+ if (hi < 0xdc00) { // yes - expect a low char
+
+ if (!(*c == '\\' && ++c && *c == 'u' && ++c && [self scanHexQuad:&lo])) {
+ [self addErrorWithCode:EUNICODE description: @"Missing low character in surrogate pair"];
+ return NO;
+ }
+
+ if (lo < 0xdc00 || lo >= 0xdfff) {
+ [self addErrorWithCode:EUNICODE description:@"Invalid low surrogate char"];
+ return NO;
+ }
+
+ hi = (hi - 0xd800) * 0x400 + (lo - 0xdc00) + 0x10000;
+
+ } else if (hi < 0xe000) {
+ [self addErrorWithCode:EUNICODE description:@"Invalid high character in surrogate pair"];
+ return NO;
+ }
+ }
+
+ *x = hi;
+ return YES;
+}
+
+- (BOOL)scanHexQuad:(unichar *)x
+{
+ *x = 0;
+ for (int i = 0; i < 4; i++) {
+ unichar uc = *c;
+ c++;
+ int d = (uc >= '0' && uc <= '9')
+ ? uc - '0' : (uc >= 'a' && uc <= 'f')
+ ? (uc - 'a' + 10) : (uc >= 'A' && uc <= 'F')
+ ? (uc - 'A' + 10) : -1;
+ if (d == -1) {
+ [self addErrorWithCode:EUNICODE description:@"Missing hex digit in quad"];
+ return NO;
+ }
+ *x *= 16;
+ *x += d;
+ }
+ return YES;
+}
+
+- (BOOL)scanNumber:(NSNumber **)o
+{
+ BOOL simple = YES;
+
+ const char *ns = c;
+
+ // The logic to test for validity of the number formatting is relicensed
+ // from JSON::XS with permission from its author Marc Lehmann.
+ // (Available at the CPAN: http://search.cpan.org/dist/JSON-XS/ .)
+
+ if ('-' == *c)
+ c++;
+
+ if ('0' == *c && c++) {
+ if (isdigit(*c)) {
+ [self addErrorWithCode:EPARSENUM description: @"Leading 0 disallowed in number"];
+ return NO;
+ }
+
+ } else if (!isdigit(*c) && c != ns) {
+ [self addErrorWithCode:EPARSENUM description: @"No digits after initial minus"];
+ return NO;
+
+ } else {
+ skipDigits(c);
+ }
+
+ // Fractional part
+ if ('.' == *c && c++) {
+ simple = NO;
+ if (!isdigit(*c)) {
+ [self addErrorWithCode:EPARSENUM description: @"No digits after decimal point"];
+ return NO;
+ }
+ skipDigits(c);
+ }
+
+ // Exponential part
+ if ('e' == *c || 'E' == *c) {
+ simple = NO;
+ c++;
+
+ if ('-' == *c || '+' == *c)
+ c++;
+
+ if (!isdigit(*c)) {
+ [self addErrorWithCode:EPARSENUM description: @"No digits after exponent"];
+ return NO;
+ }
+ skipDigits(c);
+ }
+
+ // If we are only reading integers, don't go through the expense of creating an NSDecimal.
+ // This ends up being a very large perf win.
+ if (simple) {
+ BOOL negate = NO;
+ long long val = 0;
+ const char *d = ns;
+
+ if (*d == '-') {
+ negate = YES;
+ d++;
+ }
+
+ while (isdigit(*d)) {
+ val *= 10;
+ if (val < 0)
+ goto longlong_overflow;
+ val += *d - '0';
+ if (val < 0)
+ goto longlong_overflow;
+ d++;
+ }
+
+ *o = [NSNumber numberWithLongLong:negate ? -val : val];
+ return YES;
+
+ } else {
+ // jumped to by simple branch, if an overflow occured
+ longlong_overflow:;
+
+ id str = [[NSString alloc] initWithBytesNoCopy:(char*)ns
+ length:c - ns
+ encoding:NSUTF8StringEncoding
+ freeWhenDone:NO];
+ [str autorelease];
+ if (str && (*o = [NSDecimalNumber decimalNumberWithString:str]))
+ return YES;
+
+ [self addErrorWithCode:EPARSENUM description: @"Failed creating decimal instance"];
+ return NO;
+ }
+}
+
+- (BOOL)scanIsAtEnd
+{
+ skipWhitespace(c);
+ return !*c;
+}
+
+
+@end
View
144 JSON/SBJsonStreamWriter.h
@@ -0,0 +1,144 @@
+/*
+ Copyright (c) 2010, Stig Brautaset.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are
+ met:
+
+ Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ Neither the name of the the author nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Foundation/Foundation.h>
+
+@class SBJsonStreamWriterStateMachine;
+
+/**
+ @brief The Stream Writer class.
+
+ SBJsonStreamWriter accepts various messages and writes JSON text to an
+ NSOutputStream designated at iniatilisation time.
+
+ A range of high-, mid- and low-level methods. You can mix and match calls
+ to these. For example, you may want to call -writeArrayOpen to start an
+ array and then repeatedly call -writeObject: with an object.
+
+ In JSON the keys of an object must be strings. NSDictionary keys need
+ not be, but attempting to convert an NSDictionary with non-string keys
+ into JSON will result in an error.
+
+ NSNumber instances created with the +initWithBool: method are
+ converted into the JSON boolean "true" and "false" values, and vice
+ versa. Any other NSNumber instances are converted to a JSON number the
+ way you would expect.
+
+ */
+
+@interface SBJsonStreamWriter : NSObject {
+@private
+ NSString *error;
+ SBJsonStreamWriterStateMachine **states;
+ NSOutputStream *stream;
+ NSUInteger depth, maxDepth;
+ BOOL sortKeys, humanReadable;
+}
+
+/**
+ @brief The maximum recursing depth.
+
+ Defaults to 512. If the input is nested deeper than this the input will be deemed to be
+ malicious and the parser returns nil, signalling an error. ("Nested too deep".) You can
+ turn off this security feature by setting the maxDepth value to 0.
+ */
+@property NSUInteger maxDepth;
+
+/**
+ @brief Whether we are generating human-readable (multiline) JSON.
+
+ Set whether or not to generate human-readable JSON. The default is NO, which produces
+ JSON without any whitespace between tokens. If set to YES, generates human-readable
+ JSON with linebreaks after each array value and dictionary key/value pair, indented two
+ spaces per nesting level.
+ */
+@property BOOL humanReadable;
+
+/**
+ @brief Whether or not to sort the dictionary keys in the output.
+
+ If this is set to YES, the dictionary keys in the JSON output will be in sorted order.
+ (This is useful if you need to compare two structures, for example.) The default is NO.
+ */
+@property BOOL sortKeys;
+
+/**
+ @brief Contains the error description after an error has occured.
+ */
+@property (copy, readonly) NSString *error;
+
+/**
+ @brief Initialise a stream writer.
+
+ You have to create an output stream first. You should not open/close the stream
+ manually; this class takes care of that.
+ */
+- (id)initWithStream:(NSOutputStream*)stream;
+
+/**
+ @brief Write an NSDictionary to the JSON stream.
+ */
+- (BOOL)writeObject:(NSDictionary*)dict;
+
+/**
+ @brief Write an NSArray to the JSON stream.
+ */
+- (BOOL)writeArray:(NSArray *)array;
+
+/// Start writing an Object to the stream
+- (BOOL)writeObjectOpen;
+
+/// Close the current object being written
+- (BOOL)writeObjectClose;
+
+/// Start writing an Array to the stream
+- (BOOL)writeArrayOpen;
+
+/// Close the current Array being written
+- (BOOL)writeArrayClose;
+
+/// Write a null to the stream
+- (BOOL)writeNull;
+
+/// Write a boolean to the stream
+- (BOOL)writeBool:(BOOL)x;
+
+//- (BOOL)writeInteger:(long)l;
+//- (BOOL)writeDouble:(double)d;
+
+/// Write a Number to the stream
+- (BOOL)writeNumber:(NSNumber*)n;
+
+/// Write a String to the stream
+- (BOOL)writeString:(NSString*)s;
+
+@end
View
509 JSON/SBJsonStreamWriter.m
@@ -0,0 +1,509 @@
+/*
+ Copyright (c) 2010, Stig Brautaset.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are
+ met:
+
+ Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ Neither the name of the the author nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "SBJsonStreamWriter.h"
+#import "SBProxyForJson.h"
+
+@interface SBJsonStreamWriter ()
+@property(copy) NSString *error;
+@property(readonly) NSObject **states;
+@property(readonly) NSUInteger depth;
+@property(readonly) NSOutputStream *stream;
+- (BOOL)writeValue:(id)v;
+- (void)write:(char const *)utf8 len:(NSUInteger)len;
+@end
+
+@interface SBJsonStreamWriterStateMachine : NSObject
+- (BOOL)isInvalidState:(SBJsonStreamWriter*)writer;
+- (void)appendSeparator:(SBJsonStreamWriter*)writer;
+- (BOOL)expectingKey:(SBJsonStreamWriter*)writer;
+- (void)transitionState:(SBJsonStreamWriter*)writer;
+- (void)appendWhitespace:(SBJsonStreamWriter*)writer;
+@end
+
+@interface ObjectOpenState : SBJsonStreamWriterStateMachine
+@end
+
+@interface ObjectKeyState : ObjectOpenState
+@end
+
+@interface ObjectValueState : SBJsonStreamWriterStateMachine
+@end
+
+@interface ArrayOpenState : SBJsonStreamWriterStateMachine
+@end
+
+@interface ArrayValueState : SBJsonStreamWriterStateMachine
+@end
+
+@interface StartState : SBJsonStreamWriterStateMachine
+@end
+
+@interface CompleteState : SBJsonStreamWriterStateMachine
+@end
+
+@interface ErrorState : SBJsonStreamWriterStateMachine
+@end
+
+static NSMutableDictionary *stringCache;
+static NSDecimalNumber *notANumber;
+
+// States
+static StartState *openState;
+static CompleteState *closeState;
+static ErrorState *errorState;
+static ObjectOpenState *objectOpenState;
+static ObjectKeyState *objectKeyState;
+static ObjectValueState *objectValueState;
+static ArrayOpenState *arrayOpenState;
+static ArrayValueState *arrayValueState;
+
+
+@implementation SBJsonStreamWriterStateMachine
+- (BOOL)isInvalidState:(SBJsonStreamWriter*)writer { return NO; }
+- (void)appendSeparator:(SBJsonStreamWriter*)writer {}
+- (BOOL)expectingKey:(SBJsonStreamWriter*)writer { return NO; }
+- (void)transitionState:(SBJsonStreamWriter *)writer {}
+- (void)appendWhitespace:(SBJsonStreamWriter*)writer {
+ [writer write:"\n" len:1];
+ for (int i = 0; i < writer.depth; i++)
+ [writer write:" " len: 2];
+}
+@end
+
+@implementation ObjectOpenState
+- (void)transitionState:(SBJsonStreamWriter *)writer {
+ writer.states[writer.depth] = objectValueState;
+}
+- (BOOL)expectingKey:(SBJsonStreamWriter *)writer {
+ writer.error = @"JSON object key must be string";
+ return YES;
+}
+@end
+
+@implementation ObjectKeyState
+- (void)appendSeparator:(SBJsonStreamWriter *)writer {
+ [writer write:"," len:1];
+}
+@end
+
+@implementation ObjectValueState
+- (void)appendSeparator:(SBJsonStreamWriter *)writer {
+ [writer write:":" len:1];
+}
+- (void)transitionState:(SBJsonStreamWriter *)writer {
+ writer.states[writer.depth] = objectKeyState;
+}
+- (void)appendWhitespace:(SBJsonStreamWriter *)writer {
+ [writer write:" " len:1];
+}
+@end
+
+@implementation ArrayOpenState
+- (void)transitionState:(SBJsonStreamWriter *)writer {
+ writer.states[writer.depth] = arrayValueState;
+}
+@end
+
+@implementation ArrayValueState
+- (void)appendSeparator:(SBJsonStreamWriter *)writer {
+ [writer write:"," len:1];
+}
+@end
+
+@implementation StartState
+- (void)transitionState:(SBJsonStreamWriter *)writer {
+ writer.states[writer.depth] = closeState;
+ [writer.stream close];
+}
+- (void)appendSeparator:(SBJsonStreamWriter *)writer {
+ [writer.stream open];
+}
+@end
+
+@implementation CompleteState
+- (BOOL)isInvalidState:(SBJsonStreamWriter*)writer {
+ writer.error = @"Stream is closed";
+ return YES;
+}
+@end
+
+@implementation ErrorState
+@end
+
+@implementation SBJsonStreamWriter
+
+@synthesize error;
+@dynamic depth;
+@synthesize maxDepth;
+@synthesize states;
+@synthesize stream;
+@synthesize humanReadable;
+@synthesize sortKeys;
+
++ (void)initialize {
+ notANumber = [NSDecimalNumber notANumber];
+ stringCache = [NSMutableDictionary new];
+
+ openState = [StartState new];
+ closeState = [CompleteState new];
+ errorState = [ErrorState new];
+ objectOpenState = [ObjectOpenState new];
+ objectKeyState = [ObjectKeyState new];
+ objectValueState = [ObjectValueState new];
+ arrayOpenState = [ArrayOpenState new];
+ arrayValueState = [ArrayValueState new];
+}
+
+#pragma mark Housekeeping
+
+- (id)initWithStream:(NSOutputStream*)stream_ {
+ self = [super init];
+ if (self) {
+ stream = [stream_ retain];
+ maxDepth = 512;
+ states = calloc(maxDepth, sizeof(SBJsonStreamWriterStateMachine*));
+ NSAssert(states, @"States not initialised");
+ states[0] = openState;
+ }
+ return self;
+}
+
+- (void)dealloc {
+ self.error = nil;
+ free(states);
+ [stream release];
+ [super dealloc];
+}
+
+#pragma mark Methods
+
+- (BOOL)writeObject:(NSDictionary *)dict {
+ if (![self writeObjectOpen])
+ return NO;
+
+ NSArray *keys = [dict allKeys];
+ if (sortKeys)
+ keys = [keys sortedArrayUsingSelector:@selector(compare:)];
+
+ for (id k in keys) {
+ if (![k isKindOfClass:[NSString class]]) {
+ self.error = [NSString stringWithFormat:@"JSON object key must be string: %@", k];
+ return NO;
+ }
+
+ if (![self writeString:k])
+ return NO;
+ if (![self writeValue:[dict objectForKey:k]])
+ return NO;
+ }
+
+ return [self writeObjectClose];
+}
+
+- (BOOL)writeArray:(NSArray*)array {
+ if (![self writeArrayOpen])
+ return NO;
+ for (id v in array)
+ if (![self writeValue:v])
+ return NO;
+ return [self writeArrayClose];
+}
+
+
+- (BOOL)writeObjectOpen {
+ SBJsonStreamWriterStateMachine *s = states[depth];
+ if ([s isInvalidState:self]) return NO;
+ if ([s expectingKey:self]) return NO;
+ [s appendSeparator:self];
+ if (humanReadable && depth) [s appendWhitespace:self];
+
+ if (maxDepth && ++depth > maxDepth) {
+ self.error = @"Nested too deep";
+ return NO;
+ }
+
+ states[depth] = objectOpenState;
+ [self write:"{" len:1];
+ return YES;
+}
+
+- (BOOL)writeObjectClose {
+ SBJsonStreamWriterStateMachine *state = states[depth--];
+ if ([state isInvalidState:self]) return NO;
+ if (humanReadable) [state appendWhitespace:self];
+ [self write:"}" len:1];
+ [states[depth] transitionState:self];
+ return YES;
+}
+
+- (BOOL)writeArrayOpen {
+ SBJsonStreamWriterStateMachine *s = states[depth];
+ if ([s isInvalidState:self]) return NO;
+ if ([s expectingKey:self]) return NO;
+ [s appendSeparator:self];
+ if (humanReadable && depth) [s appendWhitespace:self];
+
+ if (maxDepth && ++depth > maxDepth) {
+ self.error = @"Nested too deep";
+ return NO;
+ }
+
+ states[depth] = arrayOpenState;
+ [self write:"[" len:1];
+ return YES;
+}
+
+- (BOOL)writeArrayClose {
+ SBJsonStreamWriterStateMachine *state = states[depth--];
+ if ([state isInvalidState:self]) return NO;
+ if ([state expectingKey:self]) return NO;
+ if (humanReadable) [state appendWhitespace:self];
+
+ [self write:"]" len:1];
+ [states[depth] transitionState:self];
+ return YES;
+}
+
+- (BOOL)writeNull {
+ SBJsonStreamWriterStateMachine *s = states[depth];
+ if ([s isInvalidState:self]) return NO;
+ if ([s expectingKey:self]) return NO;
+ [s appendSeparator:self];
+ if (humanReadable) [s appendWhitespace:self];
+
+ [self write:"null" len:4];
+ [s transitionState:self];
+ return YES;
+}
+
+- (BOOL)writeBool:(BOOL)x {
+ SBJsonStreamWriterStateMachine *s = states[depth];
+ if ([s isInvalidState:self]) return NO;
+ if ([s expectingKey:self]) return NO;
+ [s appendSeparator:self];
+ if (humanReadable) [s appendWhitespace:self];
+
+ if (x)
+ [self write:"true" len:4];
+ else
+ [self write:"false" len:5];
+ [s transitionState:self];
+ return YES;
+}
+
+
+- (BOOL)writeValue:(id)o {
+ if ([o isKindOfClass:[NSDictionary class]]) {
+ return [self writeObject:o];
+
+ } else if ([o isKindOfClass:[NSArray class]]) {
+ return [self writeArray:o];
+
+ } else if ([o isKindOfClass:[NSString class]]) {
+ [self writeString:o];
+ return YES;
+
+ } else if ([o isKindOfClass:[NSNumber class]]) {
+ return [self writeNumber:o];
+
+ } else if ([o isKindOfClass:[NSNull class]]) {
+ return [self writeNull];
+
+ } else if ([o respondsToSelector:@selector(proxyForJson)]) {
+ return [self writeValue:[o proxyForJson]];
+
+ }
+
+ self.error = [NSString stringWithFormat:@"JSON serialisation not supported for @%", [o class]];
+ return NO;
+}
+
+static const char *strForChar(int c) {
+ switch (c) {
+ case 0: return "\\u0000"; break;
+ case 1: return "\\u0001"; break;
+ case 2: return "\\u0002"; break;
+ case 3: return "\\u0003"; break;
+ case 4: return "\\u0004"; break;
+ case 5: return "\\u0005"; break;
+ case 6: return "\\u0006"; break;
+ case 7: return "\\u0007"; break;
+ case 8: return "\\b"; break;
+ case 9: return "\\t"; break;
+ case 10: return "\\n"; break;
+ case 11: return "\\u000b"; break;
+ case 12: return "\\f"; break;
+ case 13: return "\\r"; break;
+ case 14: return "\\u000e"; break;
+ case 15: return "\\u000f"; break;
+ case 16: return "\\u0010"; break;
+ case 17: return "\\u0011"; break;
+ case 18: return "\\u0012"; break;
+ case 19: return "\\u0013"; break;
+ case 20: return "\\u0014"; break;
+ case 21: return "\\u0015"; break;
+ case 22: return "\\u0016"; break;
+ case 23: return "\\u0017"; break;
+ case 24: return "\\u0018"; break;
+ case 25: return "\\u0019"; break;
+ case 26: return "\\u001a"; break;
+ case 27: return "\\u001b"; break;
+ case 28: return "\\u001c"; break;
+ case 29: return "\\u001d"; break;
+ case 30: return "\\u001e"; break;
+ case 31: return "\\u001f"; break;
+ case 34: return "\\\""; break;
+ case 92: return "\\\\"; break;
+ }
+ NSLog(@"FUTFUTFUT: -->'%c'<---", c);
+ return "FUTFUTFUT";
+}
+
+- (BOOL)writeString:(NSString*)string {
+ SBJsonStreamWriterStateMachine *s = states[depth];
+ if ([s isInvalidState:self]) return NO;
+ [s appendSeparator:self];
+ if (humanReadable) [s appendWhitespace:self];
+
+ NSMutableData *data = [stringCache objectForKey:string];
+ if (data) {
+ [self write:[data bytes] len:[data length]];
+ [s transitionState:self];
+ return YES;
+ }
+
+ NSUInteger len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+ const char *utf8 = [string UTF8String];
+ NSUInteger written = 0, i = 0;
+
+ data = [NSMutableData dataWithCapacity:len * 1.1];
+ [data appendBytes:"\"" length:1];
+
+ for (i = 0; i < len; i++) {
+ int c = utf8[i];
+ if (c >= 0 && c < 32 || c == '"' || c == '\\') {
+ if (i - written)
+ [data appendBytes:utf8 + written length:i - written];
+ written = i + 1;
+
+ const char *t = strForChar(c);
+ [data appendBytes:t length:strlen(t)];
+ }
+ }
+
+ if (i - written)
+ [data appendBytes:utf8 + written length:i - written];
+
+ [data appendBytes:"\"" length:1];
+ [self write:[data bytes] len:[data length]];
+ [stringCache setObject:data forKey:string];
+ [s transitionState:self];
+ return YES;
+}
+
+- (BOOL)writeNumber:(NSNumber*)number {
+ if ((CFBooleanRef)number == kCFBooleanTrue || (CFBooleanRef)number == kCFBooleanFalse)
+ return [self writeBool:[number boolValue]];
+
+ SBJsonStreamWriterStateMachine *s = states[depth];
+ if ([s isInvalidState:self]) return NO;
+ if ([s expectingKey:self]) return NO;
+ [s appendSeparator:self];
+ if (humanReadable) [s appendWhitespace:self];
+
+ if ((CFNumberRef)number == kCFNumberPositiveInfinity) {
+ self.error = @"+Infinity is not a valid number in JSON";
+ return NO;
+
+ } else if ((CFNumberRef)number == kCFNumberNegativeInfinity) {
+ self.error = @"-Infinity is not a valid number in JSON";
+ return NO;
+
+ } else if ((CFNumberRef)number == kCFNumberNaN) {
+ self.error = @"NaN is not a valid number in JSON";
+ return NO;
+
+ } else if (number == notANumber) {
+ self.error = @"NaN is not a valid number in JSON";
+ return NO;
+ }
+
+ const char *objcType = [number objCType];
+ char num[64];
+ size_t len;
+
+ switch (objcType[0]) {
+ case 'c': case 'i': case 's': case 'l': case 'q':
+ len = sprintf(num, "%lld", [number longLongValue]);
+ break;
+ case 'C': case 'I': case 'S': case 'L': case 'Q':
+ len = sprintf(num, "%llu", [number unsignedLongLongValue]);
+ break;
+ case 'f': case 'd': default:
+ if ([number isKindOfClass:[NSDecimalNumber class]]) {
+ char const *utf8 = [[number stringValue] UTF8String];
+ [self write:utf8 len: strlen(utf8)];
+ [s transitionState:self];
+ return YES;
+ }
+ len = sprintf(num, "%g", [number doubleValue]);
+ break;
+ }
+ [self write:num len: len];
+ [s transitionState:self];
+ return YES;
+}
+
+#pragma mark Private methods
+
+- (NSUInteger)depth {
+ return depth;
+}
+
+- (void)write:(char const *)utf8 len:(NSUInteger)len {
+ NSUInteger written = 0;
+ do {
+ NSInteger w = [stream write:(const uint8_t *)utf8 maxLength:len - written];
+ if (w > 0)
+ written += w;
+ } while (written < len);
+}
+
+- (void)setMaxDepth:(NSUInteger)x {
+ NSAssert(x, @"maxDepth must be greater than 0");
+ maxDepth = x;
+ states = realloc(states, x);
+ NSAssert(states, @"Failed to reallocate more memory for states");
+}
+
+@end
View
111 JSON/SBJsonWriter.h
@@ -0,0 +1,111 @@
+/*
+ Copyright (C) 2009 Stig Brautaset. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Foundation/Foundation.h>
+#import "SBJsonBase.h"
+
+/**
+ @brief The JSON writer class.
+
+ Objective-C types are mapped to JSON types in the following way:
+
+ @li NSNull -> Null
+ @li NSString -> String
+ @li NSArray -> Array
+ @li NSDictionary -> Object
+ @li NSNumber (-initWithBool:) -> Boolean
+ @li NSNumber -> Number
+
+ In JSON the keys of an object must be strings. NSDictionary keys need
+ not be, but attempting to convert an NSDictionary with non-string keys
+ into JSON will throw an exception.
+
+ NSNumber instances created with the +initWithBool: method are
+ converted into the JSON boolean "true" and "false" values, and vice
+ versa. Any other NSNumber instances are converted to a JSON number the
+ way you would expect.
+
+ */
+@interface SBJsonWriter : SBJsonBase {
+
+@private
+ BOOL sortKeys, humanReadable;
+}
+
+/**
+ @brief Whether we are generating human-readable (multiline) JSON.
+
+ Set whether or not to generate human-readable JSON. The default is NO, which produces
+ JSON without any whitespace. (Except inside strings.) If set to YES, generates human-readable
+ JSON with linebreaks after each array value and dictionary key/value pair, indented two
+ spaces per nesting level.
+ */
+@property BOOL humanReadable;
+
+/**
+ @brief Whether or not to sort the dictionary keys in the output.
+
+ If this is set to YES, the dictionary keys in the JSON output will be in sorted order.
+ (This is useful if you need to compare two structures, for example.) The default is NO.
+ */
+@property BOOL sortKeys;
+
+/**
+ @brief Return JSON representation for the given object.
+
+ Returns a string containing JSON representation of the passed in value, or nil on error.
+ If nil is returned and @p error is not NULL, @p *error can be interrogated to find the cause of the error.
+
+ @param value any instance that can be represented as JSON text.
+ */
+- (NSString*)stringWithObject:(id)value;
+
+/**
+ @brief Return JSON representation for the given object.
+
+ Returns an NSData object containing JSON represented as UTF8 text, or nil on error.
+
+ @param value any instance that can be represented as JSON text.
+ */
+- (NSData*)dataWithObject:(id)value;
+
+/**
+ @brief Return JSON representation (or fragment) for the given object.
+
+ Returns a string containing JSON representation of the passed in value, or nil on error.
+ If nil is returned and @p error is not NULL, @p *error can be interrogated to find the cause of the error.
+
+ @param value any instance that can be represented as a JSON fragment
+ @param error pointer to object to be populated with NSError on failure
+
+ */- (NSString*)stringWithObject:(id)value
+ error:(NSError**)error;
+
+
+@end
View
96 JSON/SBJsonWriter.m
@@ -0,0 +1,96 @@
+/*
+ Copyright (C) 2009 Stig Brautaset. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ * Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "SBJsonWriter.h"
+#import "SBJsonStreamWriter.h"
+#import "SBProxyForJson.h"
+
+@interface SBJsonWriter ()
+
+- (NSData*)dataWithObject:(id)value;
+
+@end
+
+@implementation SBJsonWriter
+
+@synthesize sortKeys;
+@synthesize humanReadable;
+
+
+- (NSString*)stringWithObject:(id)value {
+ [self clearErrorTrace];
+
+ NSData *data = [self dataWithObject:value];
+ if (data)
+ return [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
+ return nil;
+}
+
+- (NSString*)stringWithObject:(id)value error:(NSError**)error {
+ NSString *tmp = [self stringWithObject:value];
+ if (tmp)
+ return tmp;
+
+ if (error)
+ *error = [self.errorTrace lastObject];
+ return nil;
+}
+
+- (NSData*)dataWithObject:(id)object {
+ NSOutputStream *stream = [[[NSOutputStream alloc] initToMemory] autorelease];
+
+ SBJsonStreamWriter *streamWriter = [[[SBJsonStreamWriter alloc] initWithStream:stream] autorelease];
+ streamWriter.sortKeys = self.sortKeys;
+ streamWriter.maxDepth = self.maxDepth;
+ streamWriter.humanReadable = self.humanReadable;
+
+ BOOL ok = NO;
+ if ([object isKindOfClass:[NSDictionary class]])
+ ok = [streamWriter writeObject:object];
+
+ else if ([object isKindOfClass:[NSArray class]])
+ ok = [streamWriter writeArray:object];
+
+ else if ([object respondsToSelector:@selector(proxyForJson)])
+ return [self dataWithObject:[object proxyForJson]];
+ else {
+ [self addErrorWithCode:EUNSUPPORTED description:@"Not valid type for JSON"];
+ return nil;
+ }
+
+ if (ok)
+ return [stream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
+
+ [self addErrorWithCode:EUNSUPPORTED description:streamWriter.error];
+ return nil;
+}
+
+
+
+@end
View
58 JSON/SBProxyForJson.h
@@ -0,0 +1,58 @@
+/*
+ Copyright (c) 2010, Stig Brautaset.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are
+ met:
+
+ Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ Neither the name of the the author nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Foundation/Foundation.h>
+
+/**
+ @brief Allows generation of JSON for otherwise unsupported classes.
+
+ If you have a custom class that you want to create a JSON representation for you can implement
+ this method in your class. It should return a representation of your object defined
+ in terms of objects that can be translated into JSON. For example, a Person
+ object might implement it like this:
+
+ @code
+ - (id)proxyForJson {
+ return [NSDictionary dictionaryWithObjectsAndKeys:
+ name, @"name",
+ phone, @"phone",
+ email, @"email",
+ nil];
+ }
+ @endcode
+
+ */
+@interface NSObject (SBProxyForJson)
+
+- (id)proxyForJson;
+
+@end
View
26 OUILookupTool/OUILookupTool.h
@@ -0,0 +1,26 @@
+//
+// OUILookupTool.h
+// OUILookup
+//
+// Created by Nicolas Seriot on 10/31/10.
+// Copyright 2010 IICT. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@class OUILookupTool;
+
+@protocol OUILookupToolDelegate
+- (void)OUILookupTool:(OUILookupTool *)ouiLookupTool didLocateAccessPoint:(NSDictionary *)ap;
+@end
+
+@interface OUILookupTool : NSObject {
+ NSObject <OUILookupToolDelegate> *delegate;
+}
+
+@property (nonatomic, retain) NSObject <OUILookupToolDelegate> *delegate;
+
+// ap should have a "bssid" key
++ (OUILookupTool *)locateWifiAccessPoint:(NSDictionary *)ap