summaryrefslogtreecommitdiffstats
path: root/Classes/Source/Experimental
diff options
context:
space:
mode:
Diffstat (limited to 'Classes/Source/Experimental')
-rw-r--r--Classes/Source/Experimental/CFilteringJSONSerializer.h25
-rw-r--r--Classes/Source/Experimental/CFilteringJSONSerializer.m87
-rw-r--r--Classes/Source/Experimental/CJSONDeserializer_BlocksExtensions.h16
-rw-r--r--Classes/Source/Experimental/CJSONDeserializer_BlocksExtensions.m63
-rw-r--r--Classes/Source/Experimental/CJSONSerialization.h34
-rw-r--r--Classes/Source/Experimental/CJSONSerialization.m59
-rw-r--r--Classes/Source/Experimental/CJSONSerializedData.h25
-rw-r--r--Classes/Source/Experimental/CJSONSerializedData.m42
8 files changed, 351 insertions, 0 deletions
diff --git a/Classes/Source/Experimental/CFilteringJSONSerializer.h b/Classes/Source/Experimental/CFilteringJSONSerializer.h
new file mode 100644
index 0000000..f004a79
--- /dev/null
+++ b/Classes/Source/Experimental/CFilteringJSONSerializer.h
@@ -0,0 +1,25 @@
+//
+// CFilteringJSONSerializer.h
+// CouchNotes
+//
+// Created by Jonathan Wight on 06/20/10.
+// Copyright 2010 toxicsoftware.com. All rights reserved.
+//
+
+#import "CJSONSerializer.h"
+
+typedef NSString *(^JSONConversionTest)(id inObject);
+typedef id (^JSONConversionConverter)(id inObject); // TODO replace with value transformers.
+
+@interface CFilteringJSONSerializer : CJSONSerializer {
+ NSSet *tests;
+ NSDictionary *convertersByName;
+}
+
+@property (readwrite, nonatomic, retain) NSSet *tests;
+@property (readwrite, nonatomic, retain) NSDictionary *convertersByName;
+
+- (void)addTest:(JSONConversionTest)inTest;
+- (void)addConverter:(JSONConversionConverter)inConverter forName:(NSString *)inName;
+
+@end
diff --git a/Classes/Source/Experimental/CFilteringJSONSerializer.m b/Classes/Source/Experimental/CFilteringJSONSerializer.m
new file mode 100644
index 0000000..1ee2a3d
--- /dev/null
+++ b/Classes/Source/Experimental/CFilteringJSONSerializer.m
@@ -0,0 +1,87 @@
+//
+// CFilteringJSONSerializer.m
+// CouchNotes
+//
+// Created by Jonathan Wight on 06/20/10.
+// Copyright 2010 toxicsoftware.com. All rights reserved.
+//
+
+#import "CFilteringJSONSerializer.h"
+
+@implementation CFilteringJSONSerializer
+
+@synthesize tests;
+@synthesize convertersByName;
+
+- (void)dealloc
+ {
+ [tests release];
+ tests = NULL;
+ //
+ [convertersByName release];
+ convertersByName = NULL;
+ //
+ [super dealloc];
+ }
+
+- (NSData *)serializeObject:(id)inObject error:(NSError **)outError
+ {
+ NSData *theData = NULL;
+ for (JSONConversionTest theTest in self.tests)
+ {
+ NSString *theName = theTest(inObject);
+ if (theName != NULL)
+ {
+ id theObject = NULL;
+ JSONConversionConverter theConverter = [self.convertersByName objectForKey:theName];
+ if (theConverter)
+ {
+ theObject = theConverter(inObject);
+ }
+
+ if (theObject)
+ {
+ if ([theObject isKindOfClass:[NSData class]])
+ {
+ theData = theObject;
+ break;
+ }
+ else
+ {
+ NSError *theError = NULL;
+ theData = [super serializeObject:theObject error:&theError];
+ if (theData != NULL)
+ {
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ if (theData == NULL)
+ {
+ theData = [super serializeObject:inObject error:outError];
+ }
+
+ return(theData);
+ }
+
+- (void)addTest:(JSONConversionTest)inTest
+ {
+ inTest = [[inTest copy] autorelease];
+ NSSet *theTests = [self.tests setByAddingObject:inTest];
+ self.tests = theTests;
+ }
+
+- (void)addConverter:(JSONConversionConverter)inConverter forName:(NSString *)inName
+ {
+ NSMutableDictionary *theConvertersByName = [[self.convertersByName mutableCopy] autorelease];
+
+ inConverter = [[inConverter copy] autorelease];
+ [theConvertersByName setObject:inConverter forKey:inName];
+ self.convertersByName = theConvertersByName;
+ }
+
+
+@end
diff --git a/Classes/Source/Experimental/CJSONDeserializer_BlocksExtensions.h b/Classes/Source/Experimental/CJSONDeserializer_BlocksExtensions.h
new file mode 100644
index 0000000..17631af
--- /dev/null
+++ b/Classes/Source/Experimental/CJSONDeserializer_BlocksExtensions.h
@@ -0,0 +1,16 @@
+//
+// CJSONDeserializer_BlocksExtensions.h
+// TouchJSON
+//
+// Created by Jonathan Wight on 10/15/10.
+// Copyright 2010 toxicsoftware.com. All rights reserved.
+//
+
+#import "CJSONDeserializer.h"
+
+@interface CJSONDeserializer (CJSONDeserializer_BlocksExtensions)
+
+- (void)deserializeAsDictionary:(NSData *)inData completionBlock:(void (^)(id result, NSError *error))block;
+- (void)deserializeAsArray:(NSData *)inData completionBlock:(void (^)(id result, NSError *error))block;
+
+@end
diff --git a/Classes/Source/Experimental/CJSONDeserializer_BlocksExtensions.m b/Classes/Source/Experimental/CJSONDeserializer_BlocksExtensions.m
new file mode 100644
index 0000000..7ea774c
--- /dev/null
+++ b/Classes/Source/Experimental/CJSONDeserializer_BlocksExtensions.m
@@ -0,0 +1,63 @@
+//
+// CJSONDeserializer_BlocksExtensions.m
+// TouchJSON
+//
+// Created by Jonathan Wight on 10/15/10.
+// Copyright 2010 toxicsoftware.com. All rights reserved.
+//
+
+#import "CJSONDeserializer_BlocksExtensions.h"
+
+#import "CJSONScanner.h"
+
+@implementation CJSONDeserializer (CJSONDeserializer_BlocksExtensions)
+
+- (void)deserializeAsDictionary:(NSData *)inData completionBlock:(void (^)(id result, NSError *error))block {
+
+ NSError *noDataError = nil;
+ if (inData == NULL || [inData length] == 0) {
+ noDataError = [NSError errorWithDomain:kJSONDeserializerErrorDomain code:kJSONScannerErrorCode_NothingToScan userInfo:NULL];
+ block(nil, noDataError);
+ }
+
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+
+ NSError *deserializationError = nil;
+ self.scanner.data = inData;
+ NSDictionary *theDictionary = NULL;
+ BOOL successful = [self.scanner scanJSONDictionary:&theDictionary error:&deserializationError];
+
+ dispatch_async(dispatch_get_main_queue (), ^{
+ if (successful)
+ block(theDictionary, nil);
+ else
+ block(nil, deserializationError);
+ });
+ }];
+}
+
+- (void)deserializeAsArray:(NSData *)inData completionBlock:(void (^)(id result, NSError *error))block {
+
+ NSError *nullInDataError = nil;
+ if (inData == NULL || [inData length] == 0) {
+ nullInDataError = [NSError errorWithDomain:kJSONDeserializerErrorDomain code:kJSONScannerErrorCode_NothingToScan userInfo:NULL];
+ block(nil, nullInDataError);
+ }
+
+ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
+
+ NSError *deserializationError = nil;
+ self.scanner.data = inData;
+ NSArray *theArray = NULL;
+ BOOL successful = [self.scanner scanJSONArray:&theArray error:&deserializationError];
+
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (successful)
+ block(theArray, nil);
+ else
+ block(nil, deserializationError);
+ });
+ }];
+}
+
+@end
diff --git a/Classes/Source/Experimental/CJSONSerialization.h b/Classes/Source/Experimental/CJSONSerialization.h
new file mode 100644
index 0000000..83c9bb2
--- /dev/null
+++ b/Classes/Source/Experimental/CJSONSerialization.h
@@ -0,0 +1,34 @@
+//
+// CJSONSerialization.h
+// TouchJSON
+//
+// Created by Jonathan Wight on 03/04/11.
+// Copyright 2011 toxicsoftware.com. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+enum {
+ kCJSONReadingMutableContainers = 0x1,
+ kCJSONReadingMutableLeaves = 0x2,
+ kCJSONReadingAllowFragments = 0x04,
+};
+typedef NSUInteger EJSONReadingOptions;
+
+enum {
+ kCJJSONWritingPrettyPrinted = 0x1
+};
+typedef NSUInteger EJSONWritingOptions;
+
+
+@interface CJSONSerialization : NSObject {
+
+}
+
++ (BOOL)isValidJSONObject:(id)obj;
++ (NSData *)dataWithJSONObject:(id)obj options:(EJSONWritingOptions)opt error:(NSError **)error;
++ (id)JSONObjectWithData:(NSData *)data options:(EJSONReadingOptions)opt error:(NSError **)error;
++ (NSInteger)writeJSONObject:(id)obj toStream:(NSOutputStream *)stream options:(EJSONWritingOptions)opt error:(NSError **)error;
++ (id)JSONObjectWithStream:(NSInputStream *)stream options:(EJSONReadingOptions)opt error:(NSError **)error;
+
+@end
diff --git a/Classes/Source/Experimental/CJSONSerialization.m b/Classes/Source/Experimental/CJSONSerialization.m
new file mode 100644
index 0000000..5f603f6
--- /dev/null
+++ b/Classes/Source/Experimental/CJSONSerialization.m
@@ -0,0 +1,59 @@
+//
+// CJSONSerialization.m
+// TouchJSON
+//
+// Created by Jonathan Wight on 03/04/11.
+// Copyright 2011 toxicsoftware.com. All rights reserved.
+//
+
+#import "CJSONSerialization.h"
+
+#import "CJSONDeserializer.h"
+#import "CJSONSerializer.h"
+
+@implementation CJSONSerialization
+
++ (BOOL)isValidJSONObject:(id)obj
+ {
+ CJSONSerializer *theSerializer = [CJSONSerializer serializer];
+ return([theSerializer isValidJSONObject:obj]);
+ }
+
++ (NSData *)dataWithJSONObject:(id)obj options:(EJSONWritingOptions)opt error:(NSError **)error
+ {
+ #pragma unused (opt)
+
+ CJSONSerializer *theSerializer = [CJSONSerializer serializer];
+ NSData *theData = [theSerializer serializeObject:obj error:error];
+ return(theData);
+ }
+
++ (id)JSONObjectWithData:(NSData *)data options:(EJSONReadingOptions)opt error:(NSError **)error
+ {
+ CJSONDeserializer *theDeserializer = [CJSONDeserializer deserializer];
+ theDeserializer.options = (opt & kCJSONReadingMutableContainers ? 0 : kJSONDeserializationOptions_MutableContainers)
+ | (opt & kCJSONReadingMutableLeaves ? 0 : kJSONDeserializationOptions_MutableLeaves);
+ id theObject = [theDeserializer deserialize:data error:error];
+ return(theObject);
+ }
+
++ (NSInteger)writeJSONObject:(id)obj toStream:(NSOutputStream *)stream options:(EJSONWritingOptions)opt error:(NSError **)error
+ {
+ // TODO -- this is a quick work around.
+ NSInteger theSize = -1;
+ NSData *theData = [self dataWithJSONObject:obj options:opt error:error];
+ if (theData)
+ {
+ theSize = [stream write:[theData bytes] maxLength:[theData length]];
+ }
+ return(theSize);
+ }
+
++ (id)JSONObjectWithStream:(NSInputStream *)stream options:(EJSONReadingOptions)opt error:(NSError **)error
+ {
+ #pragma unused (stream, opt, error)
+ // TODO -- how much to read? Ugh.
+ return(NULL);
+ }
+
+@end
diff --git a/Classes/Source/Experimental/CJSONSerializedData.h b/Classes/Source/Experimental/CJSONSerializedData.h
new file mode 100644
index 0000000..4bba1be
--- /dev/null
+++ b/Classes/Source/Experimental/CJSONSerializedData.h
@@ -0,0 +1,25 @@
+//
+// CJSONSerializedData.h
+// TouchMetricsTest
+//
+// Created by Jonathan Wight on 10/31/10.
+// Copyright 2010 toxicsoftware.com. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@protocol CJSONSerializable <NSObject>
+@property (readonly, nonatomic, retain) NSData *serializedJSONData;
+@end
+
+#pragma mark -
+
+@interface CJSONSerializedData : NSObject <CJSONSerializable> {
+ NSData *data;
+}
+
+@property (readonly, nonatomic, retain) NSData *data;
+
+- (id)initWithData:(NSData *)inData;
+
+@end
diff --git a/Classes/Source/Experimental/CJSONSerializedData.m b/Classes/Source/Experimental/CJSONSerializedData.m
new file mode 100644
index 0000000..881899d
--- /dev/null
+++ b/Classes/Source/Experimental/CJSONSerializedData.m
@@ -0,0 +1,42 @@
+//
+// CJSONSerializedData.m
+// TouchMetricsTest
+//
+// Created by Jonathan Wight on 10/31/10.
+// Copyright 2010 toxicsoftware.com. All rights reserved.
+//
+
+#import "CJSONSerializedData.h"
+
+@interface CJSONSerializedData ()
+@end
+
+#pragma mark -
+
+@implementation CJSONSerializedData
+
+@synthesize data;
+
+- (id)initWithData:(NSData *)inData
+ {
+ if ((self = [super init]) != NULL)
+ {
+ data = [inData retain];
+ }
+ return(self);
+ }
+
+- (void)dealloc
+ {
+ [data release];
+ data = NULL;
+ //
+ [super dealloc];
+ }
+
+- (NSData *)serializedJSONData
+ {
+ return(self.data);
+ }
+
+@end