JSException: jsCocoaObject_callAsFunction : method toJSON of object __NSCFString not found — remnant of a split call
Converting to JS also fails:
The reason for this is that .toJS converts NSDictionary into a JS array and adds the keys as named properties.
JSON.stringify() omits named properties when serialising a JS array.
Named properties are correctly represented only for objects.
To me it would seem that an object rather than an array with named properties is a better fit as an NSDictionary equivalent but changing it may break existing code.
var theItem = [NSDictionary dictionaryWithObjectsAndKeys: @"here", @"where", @"you", @"who", nil]return JSON.stringify(theItem)JSException: jsCocoaObject_callAsFunction : method toJSON of object __NSCFString not found — remnant of a split call
var theNSDict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Hello", @"brave", [NSNumber numberWithInteger:100], @"kosmos", [NSDate date], @"now"]
log("JSON.stringify(theNSArray) " + JSON.stringify(theNSArray)) // BAD output is empty object {}
var r = JSON.stringify(theNSDict2) // BAD array value is empty object {}
I don't understand yet how JSCocoa handles the toJSON call for the NSDictionary without it being defined in a similar way.
IMHO it would be much better if the toJSON methods are made globally available without these explicit calls to class_add_js_function in the client.
NSDictionary -> JSON is a pretty fundamental requirement.
They could be tacked on to class.js but a better solution would be a separate json.js resource that gets loaded in the same way.
IMHO it would be much better if the toJSON methods are made globally available without these explicit calls to class_add_js_function in the client.
NSDictionary -> JSON is a pretty fundamental requirement.
They could be tacked on to class.js but a better solution would be a separate json.js resource that gets loaded in the same way.Done, in json.js.You can load it with[jsc evalJSFile:[[NSBundle mainBundle] pathForResource:@"json" ofType:@"js"]];