@metamask/snaps-utils
Version:
A collection of utilities for MetaMask Snaps
34 lines • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getJsonSizeUnsafe = exports.parseJson = void 0;
const utils_1 = require("@metamask/utils");
// TODO: Upstream this to @metamask/utils
/**
* Parse JSON safely.
*
* Does multiple kinds of validation and strips unwanted properties like
* `__proto__` and `constructor`.
*
* @param json - A JSON string to be parsed.
* @returns The parsed JSON object.
* @template Type - The type of the JSON object. The type is not actually
* checked, but it is used to infer the return type.
*/
function parseJson(json) {
return (0, utils_1.getSafeJson)(JSON.parse(json));
}
exports.parseJson = parseJson;
/**
* Get the size of a JSON blob without validating that is valid JSON.
*
* This may sometimes be preferred over `getJsonSize` for performance reasons.
*
* @param value - The JSON value to get the size of.
* @returns The size of the JSON value in bytes.
*/
function getJsonSizeUnsafe(value) {
const json = JSON.stringify(value);
return new TextEncoder().encode(json).byteLength;
}
exports.getJsonSizeUnsafe = getJsonSizeUnsafe;
//# sourceMappingURL=json.cjs.map