JsonData.js 842 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. }
  24. register(JsonData, "webpack/lib/json/JsonData", null, {
  25. serialize(obj, { write }) {
  26. if (obj._buffer === undefined && obj._data !== undefined) {
  27. obj._buffer = Buffer.from(JSON.stringify(obj._data));
  28. }
  29. write(obj._buffer);
  30. },
  31. deserialize({ read }) {
  32. return new JsonData(read());
  33. }
  34. });
  35. module.exports = JsonData;