Browse files

modified sample6

  • Loading branch information...
1 parent 6eeea63 commit 4eec3d1e8bcce4b65e53466e99fd8f3da4c0feb2 @couchdeveloper committed Sep 17, 2012
View
3 Bench/Bench.xcodeproj/xcshareddata/xcschemes/Bench JPJsonParser.xcscheme
@@ -30,10 +30,9 @@
displayScale = "1.00"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
- buildConfiguration = "Release"
+ buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
- enableOpenGLFrameCaptureMode = "0"
allowLocationSimulation = "YES">
<BuildableProductRunnable>
<BuildableReference
View
34 Bench/JPJsonParser/main.mm
@@ -992,7 +992,7 @@ int main (int argc, const char * argv[])
#if defined (DEBUG)
int N = 1;
#else
- int N = 1000;
+ int N = 10000;
#endif
@@ -1005,22 +1005,22 @@ int main (int argc, const char * argv[])
NSLog(@"Start Bench");
bench_JPJsonParser1a(JSON_TEST_FILE, N);
- bench_JPJsonParserString1(JSON_TEST_FILE,N);
- bench_JPJsonParser1a(JSON_TEST_FILE, N);
- bench_JPJsonParser1b(JSON_TEST_FILE, N);
- bench_JPJsonParser1x(JSON_TEST_FILE, N);
- bench_JPJsonParser1d(JSON_TEST_FILE, N);
- bench_JPJsonParser2(JSON_TEST_FILE, N);
- bench_JPAsyncJsonParser(JSON_TEST_FILE, N);
-#if defined (USE_JSONKit)
- bench_JSONKit1(JSON_TEST_FILE, N);
- bench_JSONKit2(JSON_TEST_FILE, N);
- bench_JSONKitString1(JSON_TEST_FILE, N);
-#endif
-#if 1
- bench_NSJSONSerialization1(JSON_TEST_FILE, N);
- bench_NSJSONSerialization2(JSON_TEST_FILE, N);
-#endif
+// bench_JPJsonParserString1(JSON_TEST_FILE,N);
+// bench_JPJsonParser1a(JSON_TEST_FILE, N);
+// bench_JPJsonParser1b(JSON_TEST_FILE, N);
+// bench_JPJsonParser1x(JSON_TEST_FILE, N);
+// bench_JPJsonParser1d(JSON_TEST_FILE, N);
+// bench_JPJsonParser2(JSON_TEST_FILE, N);
+// bench_JPAsyncJsonParser(JSON_TEST_FILE, N);
+//#if defined (USE_JSONKit)
+// bench_JSONKit1(JSON_TEST_FILE, N);
+// bench_JSONKit2(JSON_TEST_FILE, N);
+// bench_JSONKitString1(JSON_TEST_FILE, N);
+//#endif
+//#if 1
+// bench_NSJSONSerialization1(JSON_TEST_FILE, N);
+// bench_NSJSONSerialization2(JSON_TEST_FILE, N);
+//#endif
[pool drain];
return 0;
}
View
83 Examples/FoundationExamples/Sample6/main.m
@@ -6,7 +6,7 @@
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
-#if !__has_feature(objc_arc)
+#if !__has_feature(objc_arc)
#error This Objective-C file shall be compiled with ARC enabled.
#endif
@@ -15,14 +15,16 @@
#import "JPJson/JPJsonWriter.h"
#import "JPJson/JPJsonWriterExtensions.h"
+#include <time.h>
+
// Objectives:
//
// 1) Create a custom class which can be serialized by JPJsonWriter
// Helper category for creating a RFC3339DateFormatter
-@implementation NSDateFormatter (RFC3339DateFormatter)
+@implementation NSDateFormatter (RFC3339DateFormatter)
+(NSDateFormatter*) rfc3339DateFormatter {
NSDateFormatter* rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale* enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
@@ -44,24 +46,37 @@ +(NSDateFormatter*) rfc3339DateFormatter {
//
// A NSDate object may be serialized as a formatted date string. So, in order to
// make this work - we just ned to define a Category for NSDate and implement the
-// JPJsonSerializableProtocol.
+// JPJsonSerializableProtocol.
//
// Serializing a NSDate involves to convert the date into a string with an
// appropriate format. The format purposfully meets RFC 3339. Then we just serialize
-// the string using the JPJsonSerializableProtocol. The JPJsonSerializableProtocol
+// the string using the JPJsonSerializableProtocol. The JPJsonSerializableProtocol
// is already implemented for a NSString by the JPJson library.
//
@interface NSDate (JPJsonWriterExtension) <JPJsonSerializableProtocol>
@end
-@implementation NSDate (JPJsonWriterExtension)
+@implementation NSDate (JPJsonWriterExtension)
- (int) JPJson_serializeTo:(id<JPJsonStreambufferProtocol>) buffer
- encoding:(JPUnicodeEncoding)encoding
- options:(JPJsonWriterOptions)options
- level:(int)level
+ encoding:(JPUnicodeEncoding)encoding
+ options:(JPJsonWriterOptions)options
+ level:(int)level
{
+#if 0 /* Using a NSDateFormatter */
NSDateFormatter* rfc3339DateFormatter = [NSDateFormatter rfc3339DateFormatter];
- id<JPJsonSerializableProtocol> dateString = (id<JPJsonSerializableProtocol>)[rfc3339DateFormatter stringFromDate:self];
- return [dateString JPJson_serializeTo:buffer encoding:encoding options:options level:level];
+ id<JPJsonSerializableProtocol> dateString = (id<JPJsonSerializableProtocol>)[rfc3339DateFormatter stringFromDate:self];
+ return [dateString JPJson_serializeTo:buffer encoding:encoding options:options level:level];
+#else
+ /* Use time utilities from ISO C (or POSIX, C99 UNIX Spec)*/
+ struct tm* timeinfo;
+ char buf[64];
+ time_t rawtime = [self timeIntervalSince1970];
+ timeinfo = gmtime(&rawtime);
+ size_t len = strftime(buf, sizeof(buf), "%FT%TZ", timeinfo);
+ CFStringRef cfDateString = CFStringCreateWithBytesNoCopy(NULL, (const UInt8 *)buf, len, kCFStringEncodingASCII, false, kCFAllocatorNull);
+ int result = [(__bridge id)cfDateString JPJson_serializeTo:buffer encoding:encoding options:options level:level];
+ CFRelease(cfDateString);
+ return result;
+#endif
}
@end
@@ -103,7 +118,7 @@ - (id) initWithFirstName:(NSString*)firstName lastName:(NSString*)lastName dayOf
- (void) addComment:(NSString*) comment
{
- if (comment == nil)
+ if (comment == nil)
return;
if (_comments == nil) {
@@ -125,12 +140,12 @@ - (void) addComment:(NSString*) comment
// For this purpose the implementation utilizes the JPJsonWriter class method
// `serializeObjectAsJSONObject:buffer:encoding:options:level:`, which in turn
// requires that the class Person implements the `NSFastEnumeration` protocol,
-// the JPJsonSerializableProtocol, and as well responds to message `count` and
+// the JPJsonSerializableProtocol, and as well responds to message `count` and
// message `objectForKey:`
//
@interface Person (JPJsonWriterExtension) <JPJsonSerializableProtocol, NSFastEnumeration>
@end
-@implementation Person (JPJsonWriterExtension)
+@implementation Person (JPJsonWriterExtension)
- (id) objectForKey:(id)aKey {
return [self valueForKey:aKey];
@@ -140,27 +155,27 @@ - (NSUInteger) count {
return 4; // return the number of properies
}
-- (NSUInteger) countByEnumeratingWithState:(NSFastEnumerationState *)state
- objects:(id __unsafe_unretained [])buffer
+- (NSUInteger) countByEnumeratingWithState:(NSFastEnumerationState *)state
+ objects:(id __unsafe_unretained [])buffer
count:(NSUInteger)len
{
static id __unsafe_unretained properties[] = {@"firstName", @"lastName", @"dayOfBirth", @"comments"};
if (state->state >= 4) {
return 0;
- }
+ }
state->itemsPtr = properties;
state->state = 4;
state->mutationsPtr = &state->extra[0];
return 4;
}
- (int) JPJson_serializeTo:(id<JPJsonStreambufferProtocol>) buffer
- encoding:(JPUnicodeEncoding)encoding
- options:(JPJsonWriterOptions)options
- level:(int)level
+ encoding:(JPUnicodeEncoding)encoding
+ options:(JPJsonWriterOptions)options
+ level:(int)level
{
- return [JPJsonWriter serializeObjectAsJSONObject:self buffer:buffer encoding:encoding options:options level:level];
+ return [JPJsonWriter serializeObjectAsJSONObject:self buffer:buffer encoding:encoding options:options level:level];
}
@end
@@ -176,23 +191,25 @@ - (int) JPJson_serializeTo:(id<JPJsonStreambufferProtocol>) buffer
int main (int argc, const char * argv[])
{
@autoreleasepool {
-
+
+ // Date in local time zone
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
- [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd"];
+ [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH:mm:ss"];
+ //[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Create an array of Persons:
NSArray* json = [[NSArray alloc] initWithObjects:
- [[Person alloc] initWithFirstName:@"John"
- lastName:@"Appleseed"
- dayOfBirth:[dateFormatter dateFromString:@"1962-04-21"]],
- [[Person alloc] initWithFirstName:@"Pete"
- lastName:@"Pearson"
- dayOfBirth:[dateFormatter dateFromString:@"1912-10-12"]],
- [[Person alloc] initWithFirstName:@"Tab"
- lastName:@"Benoit"
- dayOfBirth:[dateFormatter dateFromString:@"1970-03-09"]],
- nil];
+ [[Person alloc] initWithFirstName:@"John"
+ lastName:@"Appleseed"
+ dayOfBirth:[dateFormatter dateFromString:@"1962-04-21 20:00:00"]],
+ [[Person alloc] initWithFirstName:@"Pete"
+ lastName:@"Pearson"
+ dayOfBirth:[dateFormatter dateFromString:@"1912-10-12 08:00:00"]],
+ [[Person alloc] initWithFirstName:@"Tab"
+ lastName:@"Benoit"
+ dayOfBirth:[dateFormatter dateFromString:@"1970-03-09 09:00:00"]],
+ nil];
Person* p0 = [json objectAtIndex:0];
[p0 addComment:@"This is a comment"];
@@ -223,7 +240,7 @@ int main (int argc, const char * argv[])
error:nil];
NSLog(@"%@:\n%@", filePath, s);
-#endif
+#endif
}
}
return 0;

0 comments on commit 4eec3d1

Please sign in to comment.