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

Does not load json objects with escaped characters #7

Closed
adamweeks opened this issue Apr 13, 2015 · 6 comments
Closed

Does not load json objects with escaped characters #7

adamweeks opened this issue Apr 13, 2015 · 6 comments

Comments

@adamweeks
Copy link

Currently, on version 0.2.3, when loading a json fixture that contains an escaped character, it will not load the fixture and returns an empty array.

For example, loading myFixtureBroken.json:

{ "myLink":"<a href=\"link.url\">Link</a>"}

returns Array[0].

This can be worked around by escaping the escapes though:

{ "myLink":"<a href=\\\"link.url\\\">Link</a>"}

It will load the object fine.

@robomalo
Copy link

Confirmed.

Escaping escapes doesn't work for me though... just getting empty [].

@sidferreira
Copy link

For me some json are loaded and some aren't. Still trying to figure out a pattern... Seem that there's any sort of validation with weird rules...

@billtrik
Copy link
Owner

Hello everyone. Please if you come across a non loading json fixture, please post it here as @adamweeks has already done. The problem should be the escaping as pointed out. Let's see where that leads us

@nzakas
Copy link
Collaborator

nzakas commented May 18, 2015

This may be an issue with using html2js to load the JSON file which, as best I can tell, html2js was never intended to do. Here's what I think is happening:

html2js injects file contents into a string that is then dumped into the page (https://github.com/karma-runner/karma-html2js-preprocessor/blob/master/lib/html2js.js#L6). So if your for contains this:

{"foo":"\"bar\""}

Then it outputs:

window.__html__['foo.json'] = '{"foo":"\"bar\""}';

The \", because it's embedded in a string, is interpreted as just " per JavaScript escaping rules, so the actual string content is this:

{"foo":""bar""}

Of course, that is not valid JSON, so we get an explosion.

So, unless we can get html2js to change what they are doing, or we use a different preprocessor to load JSON files, there's no real way to fix this on our end. There's just no way we can know which quotes were meant to be escaped and which were not.

@billtrik
Copy link
Owner

@everyone please install version 0.2.5 and follow updated instructions regarding the setup of karma-json-fixtures-preprocessor plugin.

@adamweeks
Copy link
Author

Awesome! Thanks @billtrik!

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

No branches or pull requests

5 participants