Permalink
Browse files

Refactore JSON serialization code

Extracted static methods from DumpCommand to ViewJSONSerializer
  • Loading branch information...
1 parent 6549723 commit 3e0fd205c9e9ee316a4840c86fb432415e29e7f3 @olarivain olarivain committed Feb 25, 2012
Showing with 179 additions and 145 deletions.
  1. +8 −0 Frank.xcodeproj/project.pbxproj
  2. +0 −2 src/DumpCommand.h
  3. +5 −142 src/DumpCommand.m
  4. +3 −1 src/MapOperationCommand.m
  5. +17 −0 src/ViewJSONSerializer.h
  6. +146 −0 src/ViewJSONSerializer.m
@@ -7,6 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
+ 0071264614F8956700E738ED /* ViewJSONSerializer.h in Headers */ = {isa = PBXBuildFile; fileRef = 0071264414F8956700E738ED /* ViewJSONSerializer.h */; };
+ 0071264714F8956700E738ED /* ViewJSONSerializer.m in Sources */ = {isa = PBXBuildFile; fileRef = 0071264514F8956700E738ED /* ViewJSONSerializer.m */; };
4C1DD76C12BADFE100E10B8C /* OrientationCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C1DD76812BADFE100E10B8C /* OrientationCommand.m */; };
4C1DD76D12BADFE100E10B8C /* OrientationCommand.h in Headers */ = {isa = PBXBuildFile; fileRef = 4C1DD76912BADFE100E10B8C /* OrientationCommand.h */; };
4C1DD76E12BADFE100E10B8C /* AppCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C1DD76A12BADFE100E10B8C /* AppCommand.m */; };
@@ -152,6 +154,8 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ 0071264414F8956700E738ED /* ViewJSONSerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ViewJSONSerializer.h; path = src/ViewJSONSerializer.h; sourceTree = "<group>"; };
+ 0071264514F8956700E738ED /* ViewJSONSerializer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ViewJSONSerializer.m; path = src/ViewJSONSerializer.m; sourceTree = "<group>"; };
4C1DD76812BADFE100E10B8C /* OrientationCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OrientationCommand.m; path = src/OrientationCommand.m; sourceTree = "<group>"; };
4C1DD76912BADFE100E10B8C /* OrientationCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OrientationCommand.h; path = src/OrientationCommand.h; sourceTree = "<group>"; };
4C1DD76A12BADFE100E10B8C /* AppCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AppCommand.m; path = src/AppCommand.m; sourceTree = "<group>"; };
@@ -380,6 +384,8 @@
D6BD521D146C34BF001770B1 /* SelectorEngineRegistry.m */,
D6BD5220146C36A3001770B1 /* UIQuerySelectorEngine.h */,
D6BD5221146C36A3001770B1 /* UIQuerySelectorEngine.m */,
+ 0071264414F8956700E738ED /* ViewJSONSerializer.h */,
+ 0071264514F8956700E738ED /* ViewJSONSerializer.m */,
);
name = src;
sourceTree = "<group>";
@@ -649,6 +655,7 @@
D6325EE21437F7180057330D /* UIProxy.h in Headers */,
D6325EE41437F7180057330D /* WaitUntilIdle.h in Headers */,
D6BD5222146C36A3001770B1 /* UIQuerySelectorEngine.h in Headers */,
+ 0071264614F8956700E738ED /* ViewJSONSerializer.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -786,6 +793,7 @@
D6B1056114644827000DD32F /* UIQuery+ShelleyCompatibiilityOverrides.m in Sources */,
D6BD521F146C34BF001770B1 /* SelectorEngineRegistry.m in Sources */,
D6BD5223146C36A3001770B1 /* UIQuerySelectorEngine.m in Sources */,
+ 0071264714F8956700E738ED /* ViewJSONSerializer.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -14,6 +14,4 @@
NSMutableDictionary *classMapping;
}
-+ (NSObject *) jsonify: (id<NSObject>) obj;
-
@end
View
@@ -8,6 +8,8 @@
#import "objc/runtime.h"
+#import "ViewJSONSerializer.h"
+
#import "DumpCommand.h"
#import "UIQuery.h"
@@ -16,11 +18,6 @@
@interface DumpCommand()
@property (nonatomic, readwrite, retain) NSMutableDictionary *classMapping;
-// extracting classes, will end up in a proper static class to avoid weird dependencies
-+ (id) extractInstanceFromValue: (NSValue *) value;
-+ (id) extractInstanceFromColor: (UIColor *) color;
-+ (id) extractInstanceFromFont: (UIFont *) font;
-
- (NSDictionary *) serializeView: (UIView *) view;
- (id) valueForAttribute: (NSString *) attribute onObject: (NSObject *) object;
- (void) loadClassMapping;
@@ -153,15 +150,15 @@ - (id) valueForAttribute: (NSString *) attribute onObject: (NSObject *) object {
// apply special treatment for special cases: UIColor, UIFont, NSValue, add more as appropriate
if( [value isKindOfClass: UIColor.class]) {
- value = [DumpCommand extractInstanceFromColor: (UIColor *) value];
+ value = [ViewJSONSerializer extractInstanceFromColor: (UIColor *) value];
}
if([value isKindOfClass: UIFont.class]) {
- value = [DumpCommand extractInstanceFromFont: (UIFont *) value];
+ value = [ViewJSONSerializer extractInstanceFromFont: (UIFont *) value];
}
if([value isKindOfClass: NSValue.class]) {
- value = [DumpCommand extractInstanceFromValue: (NSValue *) value];
+ value = [ViewJSONSerializer extractInstanceFromValue: (NSValue *) value];
}
// at this point, we want only NSNumbers, NSArray, NSDictionary, NSString or NSNull,
@@ -177,140 +174,6 @@ - (id) valueForAttribute: (NSString *) attribute onObject: (NSObject *) object {
return value;
}
-#pragma mark - Special conversions
-#pragma mark UIColor
-+ (id) extractInstanceFromColor: (UIColor *) color {
- CGColorSpaceModel colorModel = CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor));
- const CGFloat *colors = CGColorGetComponents(color.CGColor);
-
- id value = nil;
- // so far, only RGB and monochrome color spaces are supported. Adding CMYK and other fancy pants
- // color spaces should be dead simple, just no need for it so far.
- switch (colorModel) {
- case kCGColorSpaceModelRGB:
- value = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithFloat:colors[0]], @"red",
- [NSNumber numberWithFloat:colors[1]], @"blue",
- [NSNumber numberWithFloat:colors[2]], @"green",
- [NSNumber numberWithFloat:colors[3]], @"alpha",
- nil];
- break;
- case kCGColorSpaceModelMonochrome:
- value = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithFloat:colors[0]], @"red",
- [NSNumber numberWithFloat:colors[0]], @"blue",
- [NSNumber numberWithFloat:colors[0]], @"green",
- [NSNumber numberWithFloat:colors[1]], @"alpha",
- nil];
- break;
- default:
- value = @"<NON-RGB COLOR>";
- break;
- }
-
- return value;
-}
-
-+ (id) extractInstanceFromFont: (UIFont *) font {
- if(font == nil) {
- return nil;
- }
- NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity: 4];
-
- // grab font name, family and size
- [dictionary setObject: font.fontName forKey: @"fontName"];
- [dictionary setObject: font.familyName forKey: @"familyName"];
- NSNumber *pointSize = [NSNumber numberWithFloat: font.pointSize];
- [dictionary setObject: pointSize forKey: @"pointSize"];
-
- return dictionary;
-}
-
-#pragma mark NSValue
-+ (id) extractInstanceFromValue:(NSValue *)value {
-
- const char * objcType = [value objCType];
- if(objcType == NULL) {
- return nil;
- }
-
- // Numbers just passthrough
- if([value isKindOfClass: NSNumber.class]) {
- return value;
- }
-
- // CG types
- if(strcmp(@encode(CGRect), objcType) == 0) {
- CGRect rawRect;
- [value getValue:&rawRect];
- NSDictionary *originDict = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithFloat:rawRect.origin.x], @"x",
- [NSNumber numberWithFloat:rawRect.origin.y], @"y",
- nil];
- NSDictionary *sizeDict = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithFloat:rawRect.size.width], @"width",
- [NSNumber numberWithFloat:rawRect.size.height], @"height",
- nil];
-
- return [NSDictionary dictionaryWithObjectsAndKeys:
- originDict, @"origin",
- sizeDict, @"size",
- nil];
- }
-
- if(strcmp(@encode(CGSize), objcType) == 0) {
- CGSize rawSize;
- [value getValue:&rawSize];
- return [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithFloat:rawSize.width], @"width",
- [NSNumber numberWithFloat:rawSize.height], @"height",
- nil];
- }
-
- if(strcmp(@encode(CGPoint), objcType) == 0) {
- CGPoint point;
- [value getValue:&point];
- return [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithFloat: point.x], @"x",
- [NSNumber numberWithFloat: point.y], @"y",
- nil];
- }
-
- NSString *typeString = [NSString stringWithFormat:@"%s", objcType];
- if([typeString hasPrefix: @"{"]){
- // Extract Class name from the type, format is {ClassName=Blablabla}
- NSString *valueType = [[[typeString substringFromIndex:1] componentsSeparatedByString:@"="] objectAtIndex:0];
- // In the future we could add support for converting any generic type into a dictionary, if it is helpful to do that
- return [NSString stringWithFormat:@"<%@>", valueType];
-
- }
-
- // just return nothing, we can't do much more anyway
- return nil;
-}
-
-#pragma mark - generic JSON conversion
-+ (NSObject *) jsonify: (id<NSObject>) obj {
- if(obj == nil || obj == [NSNull null]) {
- return [NSNull null];
- }
-
-
- if([obj isKindOfClass: NSString.class] ||
- [obj isKindOfClass: NSNumber.class]) {
- return obj;
- }
-
- if( [obj isKindOfClass: NSValue.class] ) {
- return [DumpCommand extractInstanceFromValue: (NSValue *) obj];
- }
-
- if ([obj isKindOfClass: UIColor.class]) {
- return [DumpCommand extractInstanceFromColor: (UIColor *) obj];
- }
-
- return [NSString stringWithFormat:@"<%@>", NSStringFromClass(obj.class)];
-}
@end
@@ -8,6 +8,8 @@
#import "MapOperationCommand.h"
+#import "ViewJSONSerializer.h"
+
#import "SelectorEngineRegistry.h"
#import "JSON.h"
#import "Operation.h"
@@ -84,7 +86,7 @@ - (NSString *)handleCommandWithRequestBody:(NSString *)requestBody {
for (UIView *view in viewsToMap) {
@try {
id result = [self performOperation:operation onView:view];
- [results addObject:[DumpCommand jsonify:result]];
+ [results addObject:[ViewJSONSerializer jsonify:result]];
}
@catch (NSException * e) {
NSLog( @"Exception while performing operation %@\n%@", operation, e );
View
@@ -0,0 +1,17 @@
+//
+// ViewJSONSerializer.h
+// Frank
+//
+// Created by Olivier Larivain on 2/24/12.
+// Copyright (c) 2012 kra. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface ViewJSONSerializer : NSObject
++ (NSObject *) jsonify: (id<NSObject>) obj;
+
++ (id) extractInstanceFromValue: (NSValue *) value;
++ (id) extractInstanceFromColor: (UIColor *) color;
++ (id) extractInstanceFromFont: (UIFont *) font;
+@end
Oops, something went wrong.

0 comments on commit 3e0fd20

Please sign in to comment.