JsonData.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { register } = require("../util/serialization");
  7. class JsonData {
  8. constructor(data) {
  9. this._buffer = undefined;
  10. this._data = undefined;
  11. if (Buffer.isBuffer(data)) {
  12. this._buffer = data;
  13. } else {
  14. this._data = data;
  15. }
  16. }
  17. get() {
  18. if (this._data === undefined && this._buffer !== undefined) {
  19. this._data = JSON.parse(this._buffer.toString());
  20. }
  21. return this._data;
  22. }
  23. updateHash(hash) {
  24. if (this._buffer === undefined && this._data !== undefined) {
  25. this._buffer = Buffer.from(JSON.stringify(this._data));
  26. }
  27. if (this._buffer) return hash.update(this._buffer);
  28. }
  29. }
  30. register(JsonData, "webpack/lib/json/JsonData", null, {
  31. serialize(obj, { write }) {
  32. if (obj._buffer === undefined && obj._data !== undefined) {
  33. obj._buffer = Buffer.from(JSON.stringify(obj._data));
  34. }
  35. write(obj._buffer);
  36. },
  37. deserialize({ read }) {
  38. return new JsonData(read());
  39. }
  40. });
  41. module.exports = JsonData;