Skip to content

Commit

Permalink
More JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
parmanoir committed Apr 26, 2012
1 parent ae096a0 commit 3a36eb5
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 18 deletions.
21 changes: 21 additions & 0 deletions JSCocoa/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

// String JSON method
String.prototype.toJSON = function () { return this }

// Javascript JSON methods for native classes
// These need to return Javascript values, not boxed ObjC objects
class_add_js_function(NSNumber, 'toJSON', function () { return this.valueOf() } )
class_add_js_function(NSDate, 'toJSON', function () { return String(this.description) } )
class_add_js_function(NSArray, 'toJSON', function () {
var r = []
for (var i=0; i<this.length; i++)
r.push(this[i].toJSON())
return r
} )
class_add_js_function(NSDictionary, 'toJSON', function () {
var r = {}
var keys = Object.keys(this)
for (var i=0; i<keys.length; i++)
r[keys[i]] = this[keys[i]].toJSON()
return r
} )
67 changes: 65 additions & 2 deletions Tests/57 JSON with ObjC classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,30 @@
var date = [NSDate date]
var dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Hello", @"brave", [NSNumber numberWithInteger:100], @"kosmos", date, @"now"]

/*
// Moved to json.js, to be loaded separately
// String JSON method
String.prototype.toJSON = function () { return this }
// Javascript JSON methods for native classes
// These need to return Javascript values, not boxed ObjC objects
class_add_js_function(NSNumber, 'toJSON', function () { return this.valueOf() } )
class_add_js_function(NSDate, 'toJSON', function () { return String(this.description) } )
class_add_js_function(NSNumber, 'toJSON', function () { return this.valueOf() } )
class_add_js_function(NSDate, 'toJSON', function () { return String(this.description) } )
class_add_js_function(NSArray, 'toJSON', function () {
var r = []
for (var i=0; i<this.length; i++)
r.push(this[i].toJSON())
return r
} )
class_add_js_function(NSDictionary, 'toJSON', function () {
var r = {}
var keys = Object.keys(this)
for (var i=0; i<keys.length; i++)
r[keys[i]] = this[keys[i]].toJSON()
return r
} )
*/

// Convert to JSON and back
var json = JSON.stringify(dict)
Expand All @@ -34,5 +51,51 @@
if (o.kosmos != 100) throw 'JSON failed (6)'
if (o.now != String(date.description)) throw 'JSON failed (7)'


//
// NSArray test
//
var array = [NSArray arrayWithObjects:@"Look", @"up", @"down", nil]
var json = JSON.stringify(array)
var o = JSON.parse(json).sort()
if (o.length != 3) throw 'JSON failed (7)'
if (o[0] != 'Look') throw 'JSON failed (8)'
if (o[1] != 'down') throw 'JSON failed (9)'
if (o[2] != 'up') throw 'JSON failed (10)'

// log("JSON.stringify(theNSArray) " + JSON.stringify(array))
// log('o=' +o)


//
// Embedded dictionary and array test
//
var dict = [NSDictionary dictionaryWithObjectsAndKeys:"up", "key1", "down", "key2"]
var dict2 = [NSDictionary dictionaryWithObjectsAndKeys:array, @"array", dict, @"dict", @"Hello", @"brave", [NSNumber numberWithInteger:100], @"kosmos", [NSDate date], @"now"]

var json = JSON.stringify(dict2)
var o = JSON.parse(json)
// log('json=' + json)
// log('o=' + dumpHash(o))
// log('o.dict=' + dumpHash(o.dict))

if (Object.keys(o).length != 5) throw 'JSON failed (11)'

// Embedded array
var o2 = o.array.sort()
if (o2.length != 3) throw 'JSON failed (12)'
if (o2[0] != 'Look') throw 'JSON failed (13)'
if (o2[1] != 'down') throw 'JSON failed (14)'
if (o2[2] != 'up') throw 'JSON failed (15)'

// Embeded dict
var o2 = o.dict
var keys = Object.keys(o2).sort()
if (keys.length != 2) throw 'JSON failed (16)'
if (keys[0] != 'key1') throw 'JSON failed (17)'
if (keys[1] != 'key2') throw 'JSON failed (18)'
if (o2.key1 != 'up') throw 'JSON failed (19)'
if (o2.key2 != 'down') throw 'JSON failed (20)'



2 changes: 2 additions & 0 deletions TestsRunner/ApplicationController.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ - (void)cycleContext
[jsc release];
jsc = [JSCocoa new];
[jsc evalJSFile:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"js"]];
// Load JSON lib
[jsc evalJSFile:[[NSBundle mainBundle] pathForResource:@"json" ofType:@"js"]];
}


Expand Down
6 changes: 6 additions & 0 deletions TestsRunner/TestsRunner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
3D36E2C310522A5900132E23 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 3D36E2BF10522A5900132E23 /* InfoPlist.strings */; };
3D36E2C410522A5900132E23 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3D36E2C110522A5900132E23 /* MainMenu.xib */; };
3D44EA621072FC7300F20BAD /* jslint-jscocoa.js in Resources */ = {isa = PBXBuildFile; fileRef = 3D44EA611072FC7300F20BAD /* jslint-jscocoa.js */; };
3DB9C7151549F39500A590B3 /* json.js in Sources */ = {isa = PBXBuildFile; fileRef = 3DB9C7141549F39500A590B3 /* json.js */; };
3DB9C7161549F3CD00A590B3 /* json.js in Resources */ = {isa = PBXBuildFile; fileRef = 3DB9C7141549F39500A590B3 /* json.js */; };
3DBE5C4112F0B6D000579DC8 /* JavaScriptCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3D36E2C610522A6F00132E23 /* JavaScriptCore.framework */; };
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -56,6 +58,7 @@
3D36E2C210522A5900132E23 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/MainMenu.xib; sourceTree = "<group>"; };
3D36E2C610522A6F00132E23 /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = /System/Library/Frameworks/JavaScriptCore.framework; sourceTree = "<absolute>"; };
3D44EA611072FC7300F20BAD /* jslint-jscocoa.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "jslint-jscocoa.js"; sourceTree = "<group>"; };
3DB9C7141549F39500A590B3 /* json.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = json.js; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* TestsRunner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TestsRunner.app; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -156,6 +159,7 @@
children = (
3D36E262105229E700132E23 /* BridgeSupportController.m */,
3D36E261105229E700132E23 /* BridgeSupportController.h */,
3DB9C7141549F39500A590B3 /* json.js */,
3D36E263105229E700132E23 /* class.js */,
3D44EA611072FC7300F20BAD /* jslint-jscocoa.js */,
3D36E282105229E700132E23 /* JSCocoa.h */,
Expand Down Expand Up @@ -228,6 +232,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3DB9C7161549F3CD00A590B3 /* json.js in Resources */,
3D36E2B710522A2100132E23 /* test.js in Resources */,
3D36E292105229E700132E23 /* class.js in Resources */,
3D44EA621072FC7300F20BAD /* jslint-jscocoa.js in Resources */,
Expand All @@ -252,6 +257,7 @@
3D36E2A4105229E700132E23 /* JSCocoaPrivateObject.m in Sources */,
3D36E2B510522A1200132E23 /* main.m in Sources */,
3D36E2BA10522A2600132E23 /* ApplicationController.m in Sources */,
3DB9C7151549F39500A590B3 /* json.js in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,4 @@
<Bucket
type = "1"
version = "1.0">
<FileBreakpoints>
<FileBreakpoint
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
isPathRelative = "0"
filePath = "/Users/mini/Software Inexdo/JSCocoa/JSCocoa/JSCocoaFFIArgument.m"
timestampString = "356700213.930288"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "1188"
endingLineNumber = "1188"
landmarkName = "+unboxJSValueRef:toObject:inContext:"
landmarkType = "5">
</FileBreakpoint>
</FileBreakpoints>
</Bucket>

0 comments on commit 3a36eb5

Please sign in to comment.