Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem reading JSON files #93

Closed
KyleJune opened this issue May 3, 2015 · 3 comments
Closed

Problem reading JSON files #93

KyleJune opened this issue May 3, 2015 · 3 comments
Labels

Comments

@KyleJune
Copy link

KyleJune commented May 3, 2015

When I read from a JSON file created by right clicking a folder and selecting add new item, the file cannot be parsed after it's contents are read.

The only JSON files I can parse are the ones that I created before starting to use Visual Studio. In the below picture, root is able to be parsed, but multipart throws the error. I know it's not the code because I replaced root by copying it's contents, deleting it, then readding root using add new item, then pasting the exact same code that worked before in it, and then it started throwing the same error as multipart.

error

When I do:

JSON.parse(content);

where content is the result of reading from the json file. It throws the following syntax error:

SyntaxError: Unexpected token 

I tried just printing out the content using console.log before parsing the JSON and it looks like what's happening is that a "?" is being appended in front of the files contents for some reason.

@mousetraps
Copy link
Contributor

The token being referenced is actually the UTF-8 BOM, and Node has decided that fs.readFile is not going to strip the BOM markers, hence the error you are seeing. There are a couple workarounds described in this StackOverflow answer, and in VS you can change the encoding of the file by File > Advanced Save Options, and selecting Unicode (UTF-8 without signature) as the encoding.
image

That said, I wouldn't take issue with changing the default of the json files in the "new item" dialog to avoid additional confusion.

@KyleJune
Copy link
Author

KyleJune commented May 3, 2015

In VS, I didn't see Advanced Save Options in my File menu. I decided to just change

JSON.parse(content);

to

JSON.parse(content.replace(/^\uFEFF/, ''));

Since I wont have control over how future users choose to save their JSON files.

@mousetraps
Copy link
Contributor

Okay, closing this issue as external for now, and we can revisit if more people complain. I'm on the fence about changing the default as I'd prefer a potential issue like this to be visible rather than hidden.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants