Browse files

Merge branch 'CL-179'

  • Loading branch information...
2 parents bf9354a + 6876ff0 commit 02d6a26ec469f61c5a71f3b8b9471cc5fcc2947e geremy cohen committed Oct 2, 2012
View
18 objective-c/3.3/Pubnub.xcodeproj/project.pbxproj
@@ -7,8 +7,10 @@
objects = {
/* Begin PBXBuildFile section */
+ C8C1CDD81611C71100FE66B6 /* JSONKit.m in Sources */ = {isa = PBXBuildFile; fileRef = C8C1CDD71611C71100FE66B6 /* JSONKit.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
+ C8C1CDDB1611CC3C00FE66B6 /* JSON.m in Sources */ = {isa = PBXBuildFile; fileRef = C8C1CDDA1611CC3C00FE66B6 /* JSON.m */; };
C8FD4D38153D447200D0A418 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C8FD4D37153D447200D0A418 /* UIKit.framework */; };
- C8FD4D3A153D447200D0A418 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C8FD4D39153D447200D0A418 /* Foundation.framework */; };
+ C8FD4D3A153D447200D0A418 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C8FD4D39153D447200D0A418 /* Foundation.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
C8FD4D3C153D447200D0A418 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C8FD4D3B153D447200D0A418 /* CoreGraphics.framework */; };
C8FD4D42153D447200D0A418 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = C8FD4D40153D447200D0A418 /* InfoPlist.strings */; };
C8FD4D44153D447200D0A418 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = C8FD4D43153D447200D0A418 /* main.m */; };
@@ -22,6 +24,10 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ C8C1CDD61611C71100FE66B6 /* JSONKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONKit.h; sourceTree = "<group>"; };
+ C8C1CDD71611C71100FE66B6 /* JSONKit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONKit.m; sourceTree = "<group>"; };
+ C8C1CDD91611CC1A00FE66B6 /* JSON.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSON.h; sourceTree = "<group>"; };
+ C8C1CDDA1611CC3C00FE66B6 /* JSON.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSON.m; sourceTree = "<group>"; };
C8FD4D33153D447200D0A418 /* Pubnub.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Pubnub.app; sourceTree = BUILT_PRODUCTS_DIR; };
C8FD4D37153D447200D0A418 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
C8FD4D39153D447200D0A418 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -114,6 +120,8 @@
C8FD4D6A153D469D00D0A418 /* PubNub */ = {
isa = PBXGroup;
children = (
+ C8C1CDD61611C71100FE66B6 /* JSONKit.h */,
+ C8C1CDD71611C71100FE66B6 /* JSONKit.m */,
C8FD4D6B153D469D00D0A418 /* Base64.h */,
C8FD4D6C153D469D00D0A418 /* Base64.m */,
C8FD4D6D153D469D00D0A418 /* CEPubnub.h */,
@@ -122,6 +130,8 @@
C8FD4D70153D469D00D0A418 /* Cipher.m */,
C8FD4D71153D469D00D0A418 /* Common.h */,
C8FD4D72153D469D00D0A418 /* Common.m */,
+ C8C1CDD91611CC1A00FE66B6 /* JSON.h */,
+ C8C1CDDA1611CC3C00FE66B6 /* JSON.m */,
);
path = PubNub;
sourceTree = "<group>";
@@ -195,6 +205,8 @@
C8FD4D7A153D469D00D0A418 /* CEPubnub.m in Sources */,
C8FD4D7B153D469D00D0A418 /* Cipher.m in Sources */,
C8FD4D7C153D469D00D0A418 /* Common.m in Sources */,
+ C8C1CDD81611C71100FE66B6 /* JSONKit.m in Sources */,
+ C8C1CDDB1611CC3C00FE66B6 /* JSON.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -263,7 +275,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Pubnub/Pubnub-Prefix.pch";
INFOPLIST_FILE = "Pubnub/Pubnub-Info.plist";
- IPHONEOS_DEPLOYMENT_TARGET = 4.0;
+ IPHONEOS_DEPLOYMENT_TARGET = 5.1;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
@@ -275,7 +287,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Pubnub/Pubnub-Prefix.pch";
INFOPLIST_FILE = "Pubnub/Pubnub-Info.plist";
- IPHONEOS_DEPLOYMENT_TARGET = 4.0;
+ IPHONEOS_DEPLOYMENT_TARGET = 5.1;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
View
BIN ...proj/project.xcworkspace/xcuserdata/dahiwadkar.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
View
10 ...deproj/project.xcworkspace/xcuserdata/dahiwadkar.xcuserdatad/WorkspaceSettings.xcsettings
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>HasAskedToTakeAutomaticSnapshotBeforeSignificantChanges</key>
+ <true/>
+ <key>SnapshotAutomaticallyBeforeSignificantChanges</key>
+ <true/>
+</dict>
+</plist>
View
BIN ...codeproj/project.xcworkspace/xcuserdata/pubnub.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
View
78 objective-c/3.3/Pubnub/PubNub/CEPubnub.m
@@ -14,6 +14,7 @@
#import "CEPubnub.h"
#import "Common.h"
+#import "JSON.h"
#define kDefaultOrigin @"pubsub.pubnub.com"
#define kMaxHistorySize 100 // From documentation
@@ -116,19 +117,29 @@ - (void) connectionDidFinishLoading:(NSURLConnection*)connection {
NSString* contentType = [[_response allHeaderFields] objectForKey:@"Content-Type"];
if ([contentType hasPrefix:@"text/javascript"] && [contentType containsString:@"UTF-8"]) { // Should be [text/javascript; charset="UTF-8"] but is sometimes different on 3G
NSError* error = nil;
- id result = [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&error];
- if (error != nil) result = nil;
- [_pubNub connection:self didCompleteWithResponse:result];
- // NSLog(@"PubNub request returned unexpected content type: %@", contentType);
+
+ if ([NSJSONSerialization class]) {
+ id result= [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&error];
+ if (error != nil) result = nil;
+ [_pubNub connection:self didCompleteWithResponse:result];
+ }else
+ {
+ [_pubNub connection:self didCompleteWithResponse:JSONParseData(_data)];
+ }
} else if ([contentType hasPrefix:@"text/javascript"])
{
if(_command== kCommand_Here_Now)
{
NSError* error = nil;
- id result = [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&error];
- if (error != nil) result = nil;
- [_pubNub connection:self didCompleteWithResponse:result];
+ if ([NSJSONSerialization class]) {
+ id result = [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&error];
+ if (error != nil) result = nil;
+ [_pubNub connection:self didCompleteWithResponse:result];
+ }else
+ {
+ [_pubNub connection:self didCompleteWithResponse:JSONParseData(_data)];
+ }
}
}else {
NSLog(@"PubNub request returned unexpected content type: %@", contentType);
@@ -494,7 +505,6 @@ - (void) fetchHistory:(NSDictionary * )arg1 {
[_connections addObject:connection];
}
-
- (void) detailedHistory:(NSDictionary * )arg1 {
NSString* channel;
@@ -558,8 +568,6 @@ - (void) detailedHistory:(NSDictionary * )arg1 {
[_connections addObject:connection];
}
-
-
- (void) getTime {
NSString* url = [NSString stringWithFormat:@"%@/time/0", _host];
PubNubConnection* connection = [[PubNubConnection alloc] initWithPubNub:self
@@ -569,39 +577,6 @@ - (void) getTime {
[_connections addObject:connection];
}
-NSDecimalNumber* time_token = 0;
-
-- (void) getTime1 {
- NSString* url = [NSString stringWithFormat:@"%@/time/0", _host];
- NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:kConnectionTimeOut];
-
- [NSURLConnection
- sendAsynchronousRequest:urlRequest
- queue:[[NSOperationQueue alloc] init]
- completionHandler:^(NSURLResponse *response2,
- NSData *data,
- NSError *error)
- {
-
- if ([data length] >0 && error == nil)
- {
- NSError* error = nil;
- id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
- if (error != nil) result = nil;
-
- if ([result isKindOfClass:[NSArray class]] && ([result count] == 1)) {
- time_token = [result objectAtIndex:0];
- }
- }
- else if ([data length] == 0 && error == nil)
- {
- time_token=0;
- }
- else if (error != nil){
- time_token=0;
- }
- }];
-}
+ (NSString *) getUUID
{
@@ -716,25 +691,17 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
}
}
}
- [self getTime1 ];
- if (time_token == 0) {
- _tryCount++;
-
- }else
- {
- _tryCount=0;
- if ([_delegate respondsToSelector:@selector(pubnub:Re_ConnectToChannel:)]) {
- [_delegate pubnub:self Re_ConnectToChannel:connection.channel];
- }
- }
+ _tryCount++;
}
else {
for (ChannelStatus* it in [_subscriptions copy]) {
if ([it.channel isEqualToString:connection.channel])
{
+ _tryCount=0;
// Connect Callback
if (it.first == NO) {
it.first = YES;
+ it.connected=YES;
if ([_delegate respondsToSelector:@selector(pubnub:ConnectToChannel:)]) {
// NSLog(@"_Connected to channel %@",connection.channel);
[_delegate pubnub:self ConnectToChannel:connection.channel];
@@ -809,7 +776,7 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
if(kMinRetry == -1)
{
[self performSelector:@selector(_resubscribeToChannel:) withObject:connection.channel afterDelay:kMinRetryInterval];
- }else if(_tryCount < kMinRetry)
+ }else if(_tryCount <= kMinRetry)
{
[self performSelector:@selector(_resubscribeToChannel:) withObject:connection.channel afterDelay:kMinRetryInterval];
}
@@ -907,7 +874,6 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
case kCommand_Here_Now: {
if ([response isKindOfClass:[NSDictionary class]] ) {
if ([_delegate respondsToSelector:@selector(pubnub:here_now:onChannel:)]) {
-
[_delegate pubnub:self here_now:response onChannel:connection.channel];
}
}
View
28 objective-c/3.3/Pubnub/PubNub/Common.m
@@ -9,7 +9,7 @@
#import "Common.h"
#import "Base64.h"
#import "Cipher.h"
-
+#import "JSON.h"
@implementation NSString (Extensions)
@@ -108,17 +108,21 @@ + (NSString *)AES128DecryptWithKey:(NSString *)key Data:(NSString *)data
+(NSString*)JSONToString:(id)object
{
NSString * jsonString= nil;
- if ([object isKindOfClass:[NSString class]]) {
- object = [NSString stringWithFormat:@"\"%@\"", object];
-
- return object;
- } else {
- NSError* error = nil;
- id result = [NSJSONSerialization dataWithJSONObject:object options:kNilOptions error:&error];
- if (error != nil) return nil;
- jsonString =[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
-
- }
+ if ([NSJSONSerialization class]) {
+ if ([object isKindOfClass:[NSString class]]) {
+ object = [NSString stringWithFormat:@"\"%@\"", object];
+
+ return object;
+ } else {
+ NSError* error = nil;
+ id result = [NSJSONSerialization dataWithJSONObject:object options:kNilOptions error:&error];
+ if (error != nil) return nil;
+ jsonString =[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
+
+ }
+ }else{
+ jsonString = JSONWriteString(object);
+ }
return jsonString;
}
View
32 objective-c/3.3/Pubnub/PubNub/JSON.h
@@ -0,0 +1,32 @@
+ // Copyright 2011 Cooliris, Inc.
+ //
+ // Licensed under the Apache License, Version 2.0 (the "License");
+ // you may not use this file except in compliance with the License.
+ // You may obtain a copy of the License at
+ //
+ // http://www.apache.org/licenses/LICENSE-2.0
+ //
+ // Unless required by applicable law or agreed to in writing, software
+ // distributed under the License is distributed on an "AS IS" BASIS,
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ // See the License for the specific language governing permissions and
+ // limitations under the License.
+
+#import <Foundation/Foundation.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ id JSONParseData(NSData* data); // Objects must be NSArray or NSDictionary - Assumes UTF8 encoding
+ NSData* JSONWriteData(id object); // Objects must be NSArray or NSDictionary - Assumes UTF8 encoding
+
+ id JSONParseString(NSString* string); // Objects must be NSArray or NSDictionary
+ NSString* JSONWriteString(id object); // Objects must be NSArray or NSDictionary
+
+ id JSONGetDictionaryValueForKey(NSDictionary* dictionary, NSString* key); // Converts NSNull to nil
+ id JSONGetArrayValueAtIndex(NSArray* array, NSUInteger index); // Allows out of bounds and converts NSNull to nil
+
+#ifdef __cplusplus
+}
+#endif
View
94 objective-c/3.3/Pubnub/PubNub/JSON.m
@@ -0,0 +1,94 @@
+ // Copyright 2011 Cooliris, Inc.
+ //
+ // Licensed under the Apache License, Version 2.0 (the "License");
+ // you may not use this file except in compliance with the License.
+ // You may obtain a copy of the License at
+ //
+ // http://www.apache.org/licenses/LICENSE-2.0
+ //
+ // Unless required by applicable law or agreed to in writing, software
+ // distributed under the License is distributed on an "AS IS" BASIS,
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ // See the License for the specific language governing permissions and
+ // limitations under the License.
+
+#import "JSON.h"
+#import "JSONKit.h"
+
+#if 1
+ //#import "Logging.h"
+#else
+#define LOG_ERROR(...) NSLog(__VA_ARGS__)
+#define DCHECK(...)
+#endif
+
+id JSONParseData(NSData* data) {
+ NSError* error = nil;
+ id object = [data objectFromJSONDataWithParseOptions:JKParseOptionNone error:&error];
+ if (object == nil) {
+ NSLog(@"JSON deserializing failed: %@", error);
+ }
+ return object;
+}
+
+NSData* JSONWriteData(id object) {
+ NSData* data = nil;
+ NSError* error = nil;
+ if ([object isKindOfClass:[NSString class]]) {
+ data = [(NSString*)object JSONDataWithOptions:JKSerializeOptionNone includeQuotes:YES error:&error];
+ } else if ([object isKindOfClass:[NSArray class]]) {
+ data = [(NSArray*)object JSONDataWithOptions:JKSerializeOptionNone error:&error];
+ } else if ([object isKindOfClass:[NSDictionary class]]) {
+ data = [(NSDictionary*)object JSONDataWithOptions:JKSerializeOptionNone error:&error];
+ } else {
+ // NOT_REACHED();
+ }
+ if (data == nil) {
+ NSLog(@"JSON serializing failed: %@", error);
+ }
+ return data;
+}
+
+id JSONParseString(NSString* string) {
+ NSError* error = nil;
+ id object = [string objectFromJSONStringWithParseOptions:JKParseOptionNone error:&error];
+ if (object == nil) {
+ NSLog(@"JSON deserializing failed: %@", error);
+ }
+ return object;
+}
+
+NSString* JSONWriteString(id object) {
+ NSString* string = nil;
+ NSError* error = nil;
+ if ([object isKindOfClass:[NSString class]]) {
+ string = [(NSString*)object JSONStringWithOptions:JKSerializeOptionNone includeQuotes:YES error:&error];
+ } else if ([object isKindOfClass:[NSArray class]]) {
+ string = [(NSArray*)object JSONStringWithOptions:JKSerializeOptionNone error:&error];
+ } else if ([object isKindOfClass:[NSDictionary class]]) {
+ string = [(NSDictionary*)object JSONStringWithOptions:JKSerializeOptionNone error:&error];
+ } else {
+ // NOT_REACHED();
+ }
+ if (string == nil) {
+ NSLog(@"JSON serializing failed: %@", error);
+ }
+ return string;
+}
+
+id JSONGetDictionaryValueForKey(NSDictionary* dictionary, NSString* key) {
+ // DCHECK(!dictionary || [dictionary isKindOfClass:[NSDictionary class]]);
+ id value = nil;
+ if(!dictionary || [dictionary isKindOfClass:[NSDictionary class]])
+ {
+ value = [dictionary objectForKey:key];
+
+ }
+ return (value == [NSNull null] ? nil : value);
+}
+
+id JSONGetArrayValueAtIndex(NSArray* array, NSUInteger index) {
+ // DCHECK(!array || [array isKindOfClass:[NSArray class]]);
+ id value = index < array.count ? [array objectAtIndex:index] : nil;
+ return (value == [NSNull null] ? nil : value);
+}
View
251 objective-c/3.3/Pubnub/PubNub/JSONKit.h
@@ -0,0 +1,251 @@
+//
+// JSONKit.h
+// http://github.com/johnezang/JSONKit
+// Dual licensed under either the terms of the BSD License, or alternatively
+// under the terms of the Apache License, Version 2.0, as specified below.
+//
+
+/*
+ Copyright (c) 2011, John Engelhart
+
+ 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 Zang Industries 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.
+*/
+
+/*
+ Copyright 2011 John Engelhart
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#include <stddef.h>
+#include <stdint.h>
+#include <limits.h>
+#include <TargetConditionals.h>
+#include <AvailabilityMacros.h>
+
+#ifdef __OBJC__
+#import <Foundation/NSArray.h>
+#import <Foundation/NSData.h>
+#import <Foundation/NSDictionary.h>
+#import <Foundation/NSError.h>
+#import <Foundation/NSObjCRuntime.h>
+#import <Foundation/NSString.h>
+#endif // __OBJC__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+// For Mac OS X < 10.5.
+#ifndef NSINTEGER_DEFINED
+#define NSINTEGER_DEFINED
+#if defined(__LP64__) || defined(NS_BUILD_32_LIKE_64)
+typedef long NSInteger;
+typedef unsigned long NSUInteger;
+#define NSIntegerMin LONG_MIN
+#define NSIntegerMax LONG_MAX
+#define NSUIntegerMax ULONG_MAX
+#else // defined(__LP64__) || defined(NS_BUILD_32_LIKE_64)
+typedef int NSInteger;
+typedef unsigned int NSUInteger;
+#define NSIntegerMin INT_MIN
+#define NSIntegerMax INT_MAX
+#define NSUIntegerMax UINT_MAX
+#endif // defined(__LP64__) || defined(NS_BUILD_32_LIKE_64)
+#endif // NSINTEGER_DEFINED
+
+
+#ifndef _JSONKIT_H_
+#define _JSONKIT_H_
+
+#if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__APPLE_CC__) && (__APPLE_CC__ >= 5465)
+#define JK_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
+#else
+#define JK_DEPRECATED_ATTRIBUTE
+#endif
+
+#define JSONKIT_VERSION_MAJOR 1
+#define JSONKIT_VERSION_MINOR 4
+
+typedef NSUInteger JKFlags;
+
+/*
+ JKParseOptionComments : Allow C style // and /_* ... *_/ (without a _, obviously) comments in JSON.
+ JKParseOptionUnicodeNewlines : Allow Unicode recommended (?:\r\n|[\n\v\f\r\x85\p{Zl}\p{Zp}]) newlines.
+ JKParseOptionLooseUnicode : Normally the decoder will stop with an error at any malformed Unicode.
+ This option allows JSON with malformed Unicode to be parsed without reporting an error.
+ Any malformed Unicode is replaced with \uFFFD, or "REPLACEMENT CHARACTER".
+ */
+
+enum {
+ JKParseOptionNone = 0,
+ JKParseOptionStrict = 0,
+ JKParseOptionComments = (1 << 0),
+ JKParseOptionUnicodeNewlines = (1 << 1),
+ JKParseOptionLooseUnicode = (1 << 2),
+ JKParseOptionPermitTextAfterValidJSON = (1 << 3),
+ JKParseOptionValidFlags = (JKParseOptionComments | JKParseOptionUnicodeNewlines | JKParseOptionLooseUnicode | JKParseOptionPermitTextAfterValidJSON),
+};
+typedef JKFlags JKParseOptionFlags;
+
+enum {
+ JKSerializeOptionNone = 0,
+ JKSerializeOptionPretty = (1 << 0),
+ JKSerializeOptionEscapeUnicode = (1 << 1),
+ JKSerializeOptionEscapeForwardSlashes = (1 << 4),
+ JKSerializeOptionValidFlags = (JKSerializeOptionPretty | JKSerializeOptionEscapeUnicode | JKSerializeOptionEscapeForwardSlashes),
+};
+typedef JKFlags JKSerializeOptionFlags;
+
+#ifdef __OBJC__
+
+typedef struct JKParseState JKParseState; // Opaque internal, private type.
+
+// As a general rule of thumb, if you use a method that doesn't accept a JKParseOptionFlags argument, it defaults to JKParseOptionStrict
+
+@interface JSONDecoder : NSObject {
+ JKParseState *parseState;
+}
++ (id)decoder;
++ (id)decoderWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
+- (id)initWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
+- (void)clearCache;
+
+// The parse... methods were deprecated in v1.4 in favor of the v1.4 objectWith... methods.
+- (id)parseUTF8String:(const unsigned char *)string length:(size_t)length JK_DEPRECATED_ATTRIBUTE; // Deprecated in JSONKit v1.4. Use objectWithUTF8String:length: instead.
+- (id)parseUTF8String:(const unsigned char *)string length:(size_t)length error:(NSError **)error JK_DEPRECATED_ATTRIBUTE; // Deprecated in JSONKit v1.4. Use objectWithUTF8String:length:error: instead.
+// The NSData MUST be UTF8 encoded JSON.
+- (id)parseJSONData:(NSData *)jsonData JK_DEPRECATED_ATTRIBUTE; // Deprecated in JSONKit v1.4. Use objectWithData: instead.
+- (id)parseJSONData:(NSData *)jsonData error:(NSError **)error JK_DEPRECATED_ATTRIBUTE; // Deprecated in JSONKit v1.4. Use objectWithData:error: instead.
+
+// Methods that return immutable collection objects.
+- (id)objectWithUTF8String:(const unsigned char *)string length:(NSUInteger)length;
+- (id)objectWithUTF8String:(const unsigned char *)string length:(NSUInteger)length error:(NSError **)error;
+// The NSData MUST be UTF8 encoded JSON.
+- (id)objectWithData:(NSData *)jsonData;
+- (id)objectWithData:(NSData *)jsonData error:(NSError **)error;
+
+// Methods that return mutable collection objects.
+- (id)mutableObjectWithUTF8String:(const unsigned char *)string length:(NSUInteger)length;
+- (id)mutableObjectWithUTF8String:(const unsigned char *)string length:(NSUInteger)length error:(NSError **)error;
+// The NSData MUST be UTF8 encoded JSON.
+- (id)mutableObjectWithData:(NSData *)jsonData;
+- (id)mutableObjectWithData:(NSData *)jsonData error:(NSError **)error;
+
+@end
+
+////////////
+#pragma mark Deserializing methods
+////////////
+
+@interface NSString (JSONKitDeserializing)
+- (id)objectFromJSONString;
+- (id)objectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
+- (id)objectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
+- (id)mutableObjectFromJSONString;
+- (id)mutableObjectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
+- (id)mutableObjectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
+@end
+
+@interface NSData (JSONKitDeserializing)
+// The NSData MUST be UTF8 encoded JSON.
+- (id)objectFromJSONData;
+- (id)objectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
+- (id)objectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
+- (id)mutableObjectFromJSONData;
+- (id)mutableObjectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
+- (id)mutableObjectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
+@end
+
+////////////
+#pragma mark Serializing methods
+////////////
+
+@interface NSString (JSONKitSerializing)
+// Convenience methods for those that need to serialize the receiving NSString (i.e., instead of having to serialize a NSArray with a single NSString, you can "serialize to JSON" just the NSString).
+// Normally, a string that is serialized to JSON has quotation marks surrounding it, which you may or may not want when serializing a single string, and can be controlled with includeQuotes:
+// includeQuotes:YES `a "test"...` -> `"a \"test\"..."`
+// includeQuotes:NO `a "test"...` -> `a \"test\"...`
+- (NSData *)JSONData; // Invokes JSONDataWithOptions:JKSerializeOptionNone includeQuotes:YES
+- (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions includeQuotes:(BOOL)includeQuotes error:(NSError **)error;
+- (NSString *)JSONString; // Invokes JSONStringWithOptions:JKSerializeOptionNone includeQuotes:YES
+- (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions includeQuotes:(BOOL)includeQuotes error:(NSError **)error;
+@end
+
+@interface NSArray (JSONKitSerializing)
+- (NSData *)JSONData;
+- (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;
+- (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingDelegate:(id)delegate selector:(SEL)selector error:(NSError **)error;
+- (NSString *)JSONString;
+- (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;
+- (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingDelegate:(id)delegate selector:(SEL)selector error:(NSError **)error;
+@end
+
+@interface NSDictionary (JSONKitSerializing)
+- (NSData *)JSONData;
+- (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;
+- (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingDelegate:(id)delegate selector:(SEL)selector error:(NSError **)error;
+- (NSString *)JSONString;
+- (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;
+- (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingDelegate:(id)delegate selector:(SEL)selector error:(NSError **)error;
+@end
+
+#ifdef __BLOCKS__
+
+@interface NSArray (JSONKitSerializingBlockAdditions)
+- (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingBlock:(id(^)(id object))block error:(NSError **)error;
+- (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingBlock:(id(^)(id object))block error:(NSError **)error;
+@end
+
+@interface NSDictionary (JSONKitSerializingBlockAdditions)
+- (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingBlock:(id(^)(id object))block error:(NSError **)error;
+- (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingBlock:(id(^)(id object))block error:(NSError **)error;
+@end
+
+#endif
+
+
+#endif // __OBJC__
+
+#endif // _JSONKIT_H_
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
View
3,065 objective-c/3.3/Pubnub/PubNub/JSONKit.m
3,065 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.

0 comments on commit 02d6a26

Please sign in to comment.