Description
When requiring a JSON file where a value contains a single quote an error like below is thrown, this works fine on Windows/Android
[ERROR] Script Error {
[ERROR] column = 32;
[ERROR] line = 2;
[ERROR] message = "Unexpected identifier 's'. Expected ')' to end an argument list.";
[ERROR] sourceURL = "file:///Users/eharris/Library/Developer/CoreSimulator/Devices/EE6CA02F-4F3A-4407-9929-FEAC97512C66/data/Containers/Bundle/Application/4B53071A-F963-4006-9FEC-DA1C01E87548/ticreateapp.app/jsonfile.json";
[ERROR] stack = " at require@[native code]\n at global code(/app.js:1:32)";
[ERROR] }
[ERROR] Script Error Module "jsonfile.json" failed to leave a valid exports object
*Workaround*
Read the file in like
JSON.parse(Ti.Filesystem.getFile('file.json').read())
Steps to reproduce
Add the below into a JSON file under your project
{
"foo": "Singlequotes can ruin one's day"
}
Add a require statement into your code that requires the file
Actual
Error like the above is thrown
Expected
File should be imported
Could you check if that also happened before SDK 6.2.0? It may be caused by https://github.com/appcelerator/titanium_mobile/pull/9106 then.
[~hknoechel] It still occurs on 6.1.1.GA, I think the [logic for require-ing a JSON file](https://github.com/appcelerator/titanium_mobile/blob/eeffeedd4bc7f20ad5211b69461c0e9b128c0d7b/iphone/Classes/KrollBridge.m#L890-L907) doesn't pass through there from what I can see. Maybe it should?
Changing [this line](https://github.com/appcelerator/titanium_mobile/blob/eeffeedd4bc7f20ad5211b69461c0e9b128c0d7b/iphone/Classes/KrollBridge.m#L899) to {noformat} data = [data stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"]; {noformat} fixes for the original issue, but still errors out on the attached amber.json with the below
There built-in serializer classes in iOS these days (I think iOS7+). I will try to refactor the JSON-import logic using
NSJSONSerialization
, which has options to handle how the JSON should be serialized and which parts of it should be retained how. *EDIT*: This here works fine:[~hknoechel] Reading the file and JSON.parse'ing it works fine, but it doesn't work from require. The file is valid as it works on Titanium Windows/Android and Node. What extra info is needed?
Reopening since it is reproducible with the following test-case:
Fixed it. We can use native JSON-parse/stringify methods for this nowadays. PR: https://github.com/appcelerator/titanium_mobile/pull/10238 Test-Case:
Expected result: A console log with "7.4.0.v20180627024922".