JSON.isRawJSON()

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

JSON.isRawJSON() 静态方法测试一个值是否是 JSON.rawJSON() 返回的对象。

¥The JSON.isRawJSON() static method tests whether a value is an object returned by JSON.rawJSON().

语法

¥Syntax

js
JSON.isRawJSON(value)

参数

¥Parameters

value

要测试的值。

返回值

¥Return value

如果 value 是由 JSON.rawJSON() 创建的,则 true;否则,false

¥true if value is created by JSON.rawJSON(); otherwise, false.

描述

¥Description

当序列化为 JSON 时,"原始 JSON" 对象被视为已经是一段 JSON。此外,由于 JSON.rawJSON() 的工作方式,原始 JSON 保证是语法上有效的 JSON。有关原始 JSON 对象的形状和行为的更多信息,请参阅 JSON.rawJSON()。此方法的存在是为了允许其他序列化库为原始 JSON 对象实现与 JSON.stringify() 类似的行为。

¥"Raw JSON" objects, when serialized to JSON, are treated as if they are already a piece of JSON. Furthermore, because of the way JSON.rawJSON() works, the raw JSON is guaranteed to be syntactically valid JSON. For more information on the shape and behavior of raw JSON objects, see JSON.rawJSON(). This method exists to allow other serialization libraries to implement similar behavior to JSON.stringify() for raw JSON objects.

示例

¥Examples

使用 JSON.isRawJSON()

¥Using JSON.isRawJSON()

下面的例子演示了如何使用 JSON.isRawJSON() 来测试 JSON.rawJSON() 是否返回了一个对象。它实现了一个自定义序列化器,可将数据序列化为类似 YAML 的格式。

¥The following example demonstrates how to use JSON.isRawJSON() to test whether an object was returned by JSON.rawJSON(). It implements a custom serializer that serializes data to a YAML-like format.

js
function mySerializer(value, indent = "") {
  if (typeof value !== "object" || value === null) {
    return JSON.stringify(value);
  }
  if (JSON.isRawJSON(value)) {
    return value.rawJSON;
  }
  const subIndent = `${indent}  `;
  if (Array.isArray(value)) {
    return `- ${value.map((v) => mySerializer(v, subIndent)).join(`\n${indent}- `)}`;
  }
  return Object.entries(value)
    .map(([key, value]) => {
      const subValue = mySerializer(value, subIndent);
      if (subValue.includes("\n")) {
        return `${key}:\n${subIndent}${subValue}`;
      }
      return `${key}: ${subValue}`;
    })
    .join(`\n${indent}`);
}

console.log(
  mySerializer({
    name: "Josh",
    userId: JSON.rawJSON("12345678901234567890"),
    friends: [
      { name: "Alice", userId: JSON.rawJSON("9876543210987654321") },
      { name: "Bob", userId: JSON.rawJSON("56789012345678901234") },
    ],
  }),
);

// name: "Josh"
// userId: 12345678901234567890
// friends:
//   - name: "Alice"
//     userId: 9876543210987654321
//   - name: "Bob"
//     userId: 56789012345678901234

如果在上面的示例中,userId 值不是由 JSON.rawJSON() 创建的,而是直接作为数字传递的,那么由于 JS 浮点精度限制,我们将预先损失精度。

¥If in the above example, the userId values were not created by JSON.rawJSON(), but passed as numbers directly, then we will get loss of precision upfront because of JS floating point precision limitations.

js
console.log(
  mySerializer({
    name: "Josh",
    userId: 12345678901234567890,
    friends: [
      { name: "Alice", userId: 9876543210987654321 },
      { name: "Bob", userId: 56789012345678901234 },
    ],
  }),
);

// name: "Josh"
// userId: 12345678901234567000
// friends:
//   - name: "Alice"
//     userId: 9876543210987655000
//   - name: "Bob"
//     userId: 56789012345678900000

规范

No specification found

No specification data found for javascript.builtins.JSON.isRawJSON.
Check for problems with this page or contribute a missing spec_url to mdn/browser-compat-data. Also make sure the specification is included in w3c/browser-specs.

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看