Permalink
Browse files

CL-147:Replace JSONKit with NSJSON

  • Loading branch information...
1 parent daa59c5 commit 79b6929d6b03a6ab8e07acea80cb77cb2a9c1fdf tanaji committed Sep 4, 2012
@@ -18,8 +18,6 @@
C8FD4D7A153D469D00D0A418 /* CEPubnub.m in Sources */ = {isa = PBXBuildFile; fileRef = C8FD4D6E153D469D00D0A418 /* CEPubnub.m */; };
C8FD4D7B153D469D00D0A418 /* Cipher.m in Sources */ = {isa = PBXBuildFile; fileRef = C8FD4D70153D469D00D0A418 /* Cipher.m */; };
C8FD4D7C153D469D00D0A418 /* Common.m in Sources */ = {isa = PBXBuildFile; fileRef = C8FD4D72153D469D00D0A418 /* Common.m */; };
- C8FD4D7E153D469D00D0A418 /* JSON.m in Sources */ = {isa = PBXBuildFile; fileRef = C8FD4D76153D469D00D0A418 /* JSON.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
- C8FD4D7F153D469D00D0A418 /* JSONKit.m in Sources */ = {isa = PBXBuildFile; fileRef = C8FD4D78153D469D00D0A418 /* JSONKit.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
C8FD4D85153D505500D0A418 /* MainWindow_iPhone.xib in Resources */ = {isa = PBXBuildFile; fileRef = C8FD4D84153D505500D0A418 /* MainWindow_iPhone.xib */; };
/* End PBXBuildFile section */
@@ -44,10 +42,6 @@
C8FD4D70153D469D00D0A418 /* Cipher.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Cipher.m; sourceTree = "<group>"; };
C8FD4D71153D469D00D0A418 /* Common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Common.h; sourceTree = "<group>"; };
C8FD4D72153D469D00D0A418 /* Common.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Common.m; sourceTree = "<group>"; };
- C8FD4D75153D469D00D0A418 /* JSON.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSON.h; sourceTree = "<group>"; };
- C8FD4D76153D469D00D0A418 /* JSON.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSON.m; sourceTree = "<group>"; };
- C8FD4D77153D469D00D0A418 /* JSONKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONKit.h; sourceTree = "<group>"; };
- C8FD4D78153D469D00D0A418 /* JSONKit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONKit.m; sourceTree = "<group>"; };
C8FD4D84153D505500D0A418 /* MainWindow_iPhone.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MainWindow_iPhone.xib; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -128,10 +122,6 @@
C8FD4D70153D469D00D0A418 /* Cipher.m */,
C8FD4D71153D469D00D0A418 /* Common.h */,
C8FD4D72153D469D00D0A418 /* Common.m */,
- C8FD4D75153D469D00D0A418 /* JSON.h */,
- C8FD4D76153D469D00D0A418 /* JSON.m */,
- C8FD4D77153D469D00D0A418 /* JSONKit.h */,
- C8FD4D78153D469D00D0A418 /* JSONKit.m */,
);
path = PubNub;
sourceTree = "<group>";
@@ -205,8 +195,6 @@
C8FD4D7A153D469D00D0A418 /* CEPubnub.m in Sources */,
C8FD4D7B153D469D00D0A418 /* Cipher.m in Sources */,
C8FD4D7C153D469D00D0A418 /* Common.m in Sources */,
- C8FD4D7E153D469D00D0A418 /* JSON.m in Sources */,
- C8FD4D7F153D469D00D0A418 /* JSONKit.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -13,7 +13,6 @@
// limitations under the License.
#import "CEPubnub.h"
-#import "JSON.h"
#import "Common.h"
#define kDefaultOrigin @"pubsub.pubnub.com"
@@ -93,7 +92,6 @@ - (id) initWithPubNub:(CEPubnub*)pubNub url:(NSURL*)url command:(Command)command
_channel = [channel copy];
_message=[message copy];
}
-
return self;
}
@@ -116,13 +114,20 @@ - (void) connectionDidFinishLoading:(NSURLConnection*)connection {
// NSLog(@"PubNub request returned Content-Encoding : %@", contente);
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
- [_pubNub connection:self didCompleteWithResponse:JSONParseData(_data)];
+ 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);
} else if ([contentType hasPrefix:@"text/javascript"])
{
if(_command== kCommand_Here_Now)
{
- [_pubNub connection:self didCompleteWithResponse:JSONParseData(_data)];
+
+ NSError* error = nil;
+ id result = [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:&error];
+ if (error != nil) result = nil;
+ [_pubNub connection:self didCompleteWithResponse:result];
}
}else {
NSLog(@"PubNub request returned unexpected content type: %@", contentType);
@@ -303,7 +308,7 @@ - (void) publish:(NSDictionary * )arg1{
msg = [self getEncryptedDictionary:(NSDictionary*) message];
}
- NSString* json = JSONWriteString(msg);
+ NSString* json = [CommonFunction JSONToString:msg ];
NSString* signature;
if (_secretKey) {
signature =[CommonFunction HMAC_SHA256withKey:[NSString stringWithFormat:@"%@",_secretKey] Input:[NSString stringWithFormat:@"%@/%@/%@/%@/%@", _publishKey, _subscribeKey, _secretKey, channel, json] ];
@@ -437,11 +442,11 @@ - (BOOL) isSubscribedToChannel:(NSString*)channel {
}
for (ChannelStatus* it in [_subscriptions copy]) {
if ([it.channel isEqualToString:channel])
- {
+ {
it.connected=false;
it.first=false;
}
- }
+ }
return NO;
}
@@ -450,29 +455,29 @@ - (void) unsubscribeFromAllChannels {
}
- (void) fetchHistory:(NSUInteger)limit forChannel:(NSString*)channel {
- NSNumber * aWrappedInt = [NSNumber numberWithInteger:limit];
+ NSNumber * aWrappedInt = [NSNumber numberWithInteger:limit];
NSDictionary* disc= [NSDictionary dictionaryWithObjectsAndKeys: aWrappedInt,@"limit", channel,@"channel",nil];
[self fetchHistory:disc];
}
- (void) fetchHistory:(NSDictionary * )arg1 {
int limit;
NSString* channel;
- if (![arg1 objectForKey:@"limit"])
+ if (![arg1 objectForKey:@"limit"])
{
NSLog(@"ERROR::limit not found.");
return;
- }else
+ }else
{
limit=[[arg1 objectForKey:@"limit"] intValue];
}
- if (![arg1 objectForKey:@"channel"])
+ if (![arg1 objectForKey:@"channel"])
{
NSLog(@"ERROR::Channel name not found.");
return;
- }else
+ }else
{
channel=[arg1 objectForKey:@"channel"];
}
@@ -506,7 +511,10 @@ - (void) detailedHistory:(NSDictionary * )arg1 {
if ([arg1 objectForKey:@"count"])
{
- [parameters appendFormat:@"count=%@",[arg1 objectForKey:@"count"]];
+ [parameters appendFormat:@"count=%@",[arg1 objectForKey:@"count"]];
+ }else
+ {
+ [parameters appendString:@"count=100"];
}
if ([arg1 objectForKey:@"start"])
@@ -576,9 +584,12 @@ - (void) getTime1 {
if ([data length] >0 && error == nil)
{
- NSArray* resp= (NSArray *)JSONParseData(data);
- if ([resp isKindOfClass:[NSArray class]] && ([resp count] == 1)) {
- time_token = [resp objectAtIndex:0];
+ 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)
@@ -633,7 +644,7 @@ -(NSArray*) getDecryptedArray:(NSArray*)array
if ([object isKindOfClass:[NSString class]]) {
[messages addObject:[self getDecryptedString:(NSString *)object ]];
} else if ([object isKindOfClass:[NSArray class]]) {
- [messages addObject:[self getDecryptedArray:(NSArray *)object ]];
+ [messages addObject:[self getDecryptedArray:(NSArray *)object ]];
} else if ([object isKindOfClass:[NSDictionary class]]) {
[messages addObject:[self getDecryptedDictionary:(NSDictionary *)object ]];
}
@@ -694,7 +705,7 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
if (response == nil ) {
for (ChannelStatus* it in [_subscriptions copy]) {
if ([it.channel isEqualToString:connection.channel])
- {
+ {
if(it.first && it.connected) {
it.connected=NO;
@@ -704,11 +715,9 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
}
}
}
-//
-// BOOL is_reconnected = NO;
[self getTime1 ];
if (time_token == 0) {
-
+
}else
{
if ([_delegate respondsToSelector:@selector(pubnub:Re_ConnectToChannel:)]) {
@@ -730,16 +739,18 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
break;
}else
{
- if ([_delegate respondsToSelector:@selector(pubnub:Re_ConnectToChannel:)]) {
-
- [_delegate pubnub:self Re_ConnectToChannel:connection.channel];
+ if (it.connected == NO ) {
+ it.connected =YES;
+ if ([_delegate respondsToSelector:@selector(pubnub:Re_ConnectToChannel:)]) {
+ [_delegate pubnub:self Re_ConnectToChannel:connection.channel];
+ }
}
}
}
}
}
}
-
+
if ([response isKindOfClass:[NSArray class]] && ([response count] == 2)) {
if(!isPresence)
NSLog(@"Received %i messages from PubNub channel \"%@\"", [[response objectAtIndex:0] count], connection.channel);
@@ -750,12 +761,12 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
if ([message isKindOfClass:[NSDictionary class]]) {
if ([_delegate respondsToSelector:@selector(pubnub:subscriptionDidReceiveDictionary:onChannel:)]) {
NSDictionary * disc=[self getDecryptedDictionary:(NSDictionary *)message];
- [_delegate pubnub:self subscriptionDidReceiveDictionary:disc onChannel:connection.channel];
+ [_delegate pubnub:self subscriptionDidReceiveDictionary:disc onChannel:connection.channel];
}
}else if ([message isKindOfClass:[NSArray class]]) {
if ([_delegate respondsToSelector:@selector(pubnub:subscriptionDidReceiveArray:onChannel:)]) {
NSArray * arr=[self getDecryptedArray:(NSArray *)message];
- [_delegate pubnub:self subscriptionDidReceiveArray:arr onChannel:connection.channel];
+ [_delegate pubnub:self subscriptionDidReceiveArray:arr onChannel:connection.channel];
}
}else if ([message isKindOfClass:[NSString class]]) {
if ([_delegate respondsToSelector:@selector(pubnub:subscriptionDidReceiveArray:onChannel:)]) {
@@ -773,7 +784,7 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
if ([message isKindOfClass:[NSDictionary class]]) {
if ([_delegate respondsToSelector:@selector(pubnub:presence:onChannel:)]) {
NSString* channel = [connection.channel stringByReplacingOccurrencesOfString:@"-pnpres" withString:@""];
- [_delegate pubnub:self presence:message onChannel:channel];
+ [_delegate pubnub:self presence:message onChannel:channel];
}
}
}
@@ -790,17 +801,17 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
} else {
[self _resubscribeToChannel:connection.channel];
}
- }
+ }
else {
[self performSelector:@selector(_resubscribeToChannel:) withObject:connection.channel afterDelay:kMinRetryInterval];
}
break;
}
case kCommand_FetchDetailHistory:{
NSMutableArray *mainArray = [NSMutableArray arrayWithCapacity: 2];
- NSMutableArray *returnArray = [NSMutableArray arrayWithArray:response];
+ NSMutableArray *returnArray = [NSMutableArray arrayWithArray:response];
if ([response isKindOfClass:[NSArray class]]) {
- id msg= [response objectAtIndex:0];
+ id msg= [response objectAtIndex:0];
for (id message in msg) {
if ([message isKindOfClass:[NSDictionary class]]) {
NSDictionary * disc=[self getDecryptedDictionary:(NSDictionary *)message];
@@ -813,7 +824,7 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
[mainArray addObject:str];
}
}
-
+
[returnArray replaceObjectAtIndex:0 withObject:mainArray];
}else if (response) {
@@ -848,7 +859,7 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
}else if ([message isKindOfClass:[NSArray class]]) {
NSArray * arr=[self getDecryptedArray:(NSArray *)message];
[mainArray addObject:arr];
- }else if ([message isKindOfClass:[NSString class]]) {
+ }else if ([message isKindOfClass:[NSString class]]) {
NSString * str=[self getDecryptedString:(NSString *)message];
[mainArray addObject:str];
}
@@ -888,7 +899,7 @@ - (void) connection:(PubNubConnection*)connection didCompleteWithResponse:(id)re
if ([response isKindOfClass:[NSDictionary class]] ) {
if ([_delegate respondsToSelector:@selector(pubnub:here_now:onChannel:)]) {
- [_delegate pubnub:self here_now:response onChannel:connection.channel];
+ [_delegate pubnub:self here_now:response onChannel:connection.channel];
}
}
break;
@@ -23,4 +23,5 @@
+ (NSString *)AES128EncryptWithKey:(NSString *)key Data:(NSString *)data ;
+ (NSString *)AES128DecryptWithKey:(NSString *)key Data:(NSString *)data ;
+ (NSString *)AES128EncryptWithKeyAndData:(NSString *)key Data:(NSData *)val;
++ (NSString*)JSONToString:(id)object;
@end
@@ -104,6 +104,24 @@ + (NSString *)AES128DecryptWithKey:(NSString *)key Data:(NSString *)data
return dec ;
}
+
++(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];
+
+ }
+ return jsonString;
+}
+
@end
@@ -1,32 +0,0 @@
-// 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
Oops, something went wrong.

0 comments on commit 79b6929

Please sign in to comment.