Skip to content

Instantly share code, notes, and snippets.

@millermedeiros
Created September 30, 2011 21:25
Show Gist options
  • Save millermedeiros/1255010 to your computer and use it in GitHub Desktop.
Save millermedeiros/1255010 to your computer and use it in GitHub Desktop.
RequireJS plugin to load JSON files
/*!
* RequireJS plugin for loading JSON files
* - depends on Text plugin and it was HEAVILY "inspired" by it as well.
*
* IMPORTANT: it only works on r.js optimizer after version 0.26+ 20011/09/27
*
* @author Miller Medeiros
* @version 0.0.1 (2011/06/10)
* Released under the WTFPL <http://sam.zoy.org/wtfpl/>
*/
define(['text'], function(text){
var jsonParse = (typeof JSON !== 'undefined' && typeof JSON.parse === 'function')? JSON.parse : function(val){
return eval('('+ val +')'); //quick and dirty
},
buildMap = {};
//API
return {
load : function(name, req, onLoad, config) {
text.get(req.toUrl(name), function(data){
if (config.isBuild) {
buildMap[name] = data;
onLoad(data);
} else {
onLoad(jsonParse(data));
}
});
},
//write method based on RequireJS official text plugin by James Burke
//https://github.com/jrburke/requirejs/blob/master/text.js
write : function(pluginName, moduleName, write){
if(moduleName in buildMap){
var content = buildMap[moduleName];
write('define("'+ pluginName +'!'+ moduleName +'", function(){ return '+ content +';});\n');
}
}
};
});
@millermedeiros
Copy link
Author

I know that a plugin for JSON is kinda stupid since if you have the files locally you can just wrap them into a define call and if it is on your server you can just load it as text and parse the JSON data, but you get the idea..

just to save the example I used on this bug report and as further reference.

@wjcrowcroft
Copy link

Weird coincidence - this is exactly what I was looking for this morning, found it via google, right after reading your blog post about AMD/CJS. Nice work dude!

I don't actually think it's stupid - as long as it doesn't cause a performance hit, it will save a whole bunch of shitty code like this:

try {
    // parseJSON
} catch (e) {
    // blah
}

And when you're working with a big team of backend developers, this is a definitely a good solution, saves having to explain AMD format to them and why suddenly they need to expose their APIs as javascript files instead of pure JSON.

(PS. trying it out in our app now. Will post on that pull request in a little while)

@millermedeiros
Copy link
Author

exactly, that was the reason why I created it, I prefer to keep the files with the .json extension and calling $.parseJSON() all the time is boring... Another benefit is that we can use JSONLint to validate it or even JSON Schema if needed.

I have a few more plugins on my gists, need to create a blog post linking to all of them...

@wjcrowcroft
Copy link

Yeah definitely! Maybe make a repository for AMD plugins so they're all in one place?

@millermedeiros
Copy link
Author

moved all my plugins to a new repository: https://github.com/millermedeiros/requirejs-plugins

@andrew-luhring
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment