Created
March 23, 2011 22:04
-
-
Save mattrobenolt/884133 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import simplejson as json | |
try: | |
import cPickle as pickle | |
except ImportError: | |
import pickle | |
def main(argv): | |
try: | |
if argv[0] == '--loads': | |
json.dump(pickle.load(sys.stdin), sys.stdout) | |
elif argv[0] == '--dumps': | |
pickle.dump(json.load(sys.stdin), sys.stdout) | |
except: | |
sys.stdout.write('-1') | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var spawn = require('child_process').spawn; | |
module.exports.loads = function loads(pickle, callback) | |
{ | |
var convert = spawn('python', ['convert.py', '--loads']), | |
stdout_buffer = []; | |
convert.stdout.on('data', function(data) | |
{ | |
stdout_buffer.push(data); | |
}); | |
convert.on('exit', function(code) | |
{ | |
var data = stdout_buffer.join(''); | |
callback( | |
data === '-1' | |
? false | |
: JSON.parse(data.toString()) | |
); | |
}); | |
convert.stdin.write(pickle); | |
convert.stdin.end(); | |
}; | |
module.exports.dumps = function dumps(raw, callback) | |
{ | |
var convert = spawn('python', ['convert.py', '--dumps']), | |
stdout_buffer = []; | |
convert.stdout.on('data', function(data) | |
{ | |
stdout_buffer.push(data); | |
}); | |
convert.on('exit', function(code) | |
{ | |
var data = stdout_buffer.join(''); | |
callback( | |
data === '-1' | |
? false | |
: data | |
); | |
}); | |
convert.stdin.write(JSON.stringify(raw)); | |
convert.stdin.end(); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pickle = require('./pickle'); | |
pickle.dumps({"test": true}, function(data) | |
{ | |
console.log(data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment