| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { register } = require("../util/serialization");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 11 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 12 | /** @typedef {import("../util/Hash")} Hash */
|
|---|
| 13 | /** @typedef {import("../util/fs").JsonValue} JsonValue */
|
|---|
| 14 |
|
|---|
| 15 | class JsonData {
|
|---|
| 16 | /**
|
|---|
| 17 | * Creates an instance of JsonData.
|
|---|
| 18 | * @param {Buffer | JsonValue} data JSON data
|
|---|
| 19 | */
|
|---|
| 20 | constructor(data) {
|
|---|
| 21 | /** @type {Buffer | undefined} */
|
|---|
| 22 | this._buffer = undefined;
|
|---|
| 23 | /** @type {JsonValue | undefined} */
|
|---|
| 24 | this._data = undefined;
|
|---|
| 25 | if (Buffer.isBuffer(data)) {
|
|---|
| 26 | this._buffer = data;
|
|---|
| 27 | } else {
|
|---|
| 28 | this._data = data;
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Returns raw JSON data.
|
|---|
| 34 | * @returns {JsonValue | undefined} Raw JSON data
|
|---|
| 35 | */
|
|---|
| 36 | get() {
|
|---|
| 37 | if (this._data === undefined && this._buffer !== undefined) {
|
|---|
| 38 | this._data = JSON.parse(this._buffer.toString());
|
|---|
| 39 | }
|
|---|
| 40 | return this._data;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | /**
|
|---|
| 44 | * Updates the hash with the data contributed by this instance.
|
|---|
| 45 | * @param {Hash} hash hash to be updated
|
|---|
| 46 | * @returns {void} the updated hash
|
|---|
| 47 | */
|
|---|
| 48 | updateHash(hash) {
|
|---|
| 49 | if (this._buffer === undefined && this._data !== undefined) {
|
|---|
| 50 | this._buffer = Buffer.from(JSON.stringify(this._data));
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | if (this._buffer) hash.update(this._buffer);
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | register(JsonData, "webpack/lib/json/JsonData", null, {
|
|---|
| 58 | /**
|
|---|
| 59 | * Serializes this instance into the provided serializer context.
|
|---|
| 60 | * @param {JsonData} obj JSONData object
|
|---|
| 61 | * @param {ObjectSerializerContext} context context
|
|---|
| 62 | */
|
|---|
| 63 | serialize(obj, { write }) {
|
|---|
| 64 | if (obj._buffer === undefined && obj._data !== undefined) {
|
|---|
| 65 | obj._buffer = Buffer.from(JSON.stringify(obj._data));
|
|---|
| 66 | }
|
|---|
| 67 | write(obj._buffer);
|
|---|
| 68 | },
|
|---|
| 69 | /**
|
|---|
| 70 | * Restores this instance from the provided deserializer context.
|
|---|
| 71 | * @param {ObjectDeserializerContext} context context
|
|---|
| 72 | * @returns {JsonData} deserialized JSON data
|
|---|
| 73 | */
|
|---|
| 74 | deserialize({ read }) {
|
|---|
| 75 | return new JsonData(read());
|
|---|
| 76 | }
|
|---|
| 77 | });
|
|---|
| 78 |
|
|---|
| 79 | module.exports = JsonData;
|
|---|