You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_parser.js 650B

123456789101112131415161718192021222324252627282930
  1. if (global.GENTLY) require = GENTLY.hijack(require);
  2. var Buffer = require('buffer').Buffer;
  3. function JSONParser(parent) {
  4. this.parent = parent;
  5. this.chunks = [];
  6. this.bytesWritten = 0;
  7. }
  8. exports.JSONParser = JSONParser;
  9. JSONParser.prototype.write = function(buffer) {
  10. this.bytesWritten += buffer.length;
  11. this.chunks.push(buffer);
  12. return buffer.length;
  13. };
  14. JSONParser.prototype.end = function() {
  15. try {
  16. var fields = JSON.parse(Buffer.concat(this.chunks));
  17. for (var field in fields) {
  18. this.onField(field, fields[field]);
  19. }
  20. } catch (e) {
  21. this.parent.emit('error', e);
  22. }
  23. this.data = null;
  24. this.onEnd();
  25. };