Source: JSONReader.js

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var Guacamole = Guacamole || {};
  20. /**
  21. * A reader which automatically handles the given input stream, assembling all
  22. * received blobs into a JavaScript object by appending them to each other, in
  23. * order, and decoding the result as JSON. Note that this object will overwrite
  24. * any installed event handlers on the given Guacamole.InputStream.
  25. *
  26. * @constructor
  27. * @param {Guacamole.InputStream} stream
  28. * The stream that JSON will be read from.
  29. */
  30. Guacamole.JSONReader = function guacamoleJSONReader(stream) {
  31. /**
  32. * Reference to this Guacamole.JSONReader.
  33. *
  34. * @private
  35. * @type {Guacamole.JSONReader}
  36. */
  37. var guacReader = this;
  38. /**
  39. * Wrapped Guacamole.StringReader.
  40. *
  41. * @private
  42. * @type {Guacamole.StringReader}
  43. */
  44. var stringReader = new Guacamole.StringReader(stream);
  45. /**
  46. * All JSON read thus far.
  47. *
  48. * @private
  49. * @type {String}
  50. */
  51. var json = '';
  52. /**
  53. * Returns the current length of this Guacamole.JSONReader, in characters.
  54. *
  55. * @return {Number}
  56. * The current length of this Guacamole.JSONReader.
  57. */
  58. this.getLength = function getLength() {
  59. return json.length;
  60. };
  61. /**
  62. * Returns the contents of this Guacamole.JSONReader as a JavaScript
  63. * object.
  64. *
  65. * @return {Object}
  66. * The contents of this Guacamole.JSONReader, as parsed from the JSON
  67. * contents of the input stream.
  68. */
  69. this.getJSON = function getJSON() {
  70. return JSON.parse(json);
  71. };
  72. // Append all received text
  73. stringReader.ontext = function ontext(text) {
  74. // Append received text
  75. json += text;
  76. // Call handler, if present
  77. if (guacReader.onprogress)
  78. guacReader.onprogress(text.length);
  79. };
  80. // Simply call onend when end received
  81. stringReader.onend = function onend() {
  82. if (guacReader.onend)
  83. guacReader.onend();
  84. };
  85. /**
  86. * Fired once for every blob of data received.
  87. *
  88. * @event
  89. * @param {Number} length
  90. * The number of characters received.
  91. */
  92. this.onprogress = null;
  93. /**
  94. * Fired once this stream is finished and no further data will be written.
  95. *
  96. * @event
  97. */
  98. this.onend = null;
  99. };