UNPKG

1.13 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.isJsonObject = void 0;
4const stringify_js_1 = require("../stringify.js");
5/**
6 * @name isJsonObject
7 * @summary Tests for a valid JSON `object`.
8 * @description
9 * Checks to see if the input value is a valid JSON object.
10 * It returns false if the input is JSON parsable, but not an Javascript object.
11 * @example
12 * <BR>
13 *
14 * ```javascript
15 * import { isJsonObject } from '@polkadot/util';
16 *
17 * isJsonObject({}); // => true
18 * isJsonObject({
19 * "Test": "1234",
20 * "NestedTest": {
21 * "Test": "5678"
22 * }
23 * }); // => true
24 * isJsonObject(1234); // JSON parsable, but not an object => false
25 * isJsonObject(null); // JSON parsable, but not an object => false
26 * isJsonObject('not an object'); // => false
27 * ```
28 */
29function isJsonObject(value) {
30 const str = typeof value !== 'string'
31 ? (0, stringify_js_1.stringify)(value)
32 : value;
33 try {
34 const obj = JSON.parse(str);
35 return typeof obj === 'object' && obj !== null;
36 }
37 catch {
38 return false;
39 }
40}
41exports.isJsonObject = isJsonObject;