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.

34 lines
832 B

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