For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/proposal-json-tryParse.md.
  • 简体中文
  • JSON.tryParse ?

    提案概览
    提案速览

    该提案引入 JSON.tryParse 方法,在解析失败时返回 undefined 而不是抛出异常,简化了 JSON.parse 周围的 try-catch 模式。该提案已提交给 TC39,但于 2023 年 11 月被拒绝。提案包含调用现有 JSON.parse(带可选 reviver)的规范伪代码,并引用 URL.canParse 作为优雅处理解析失败的先例。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    JSON.tryParse

    于 2023 年 11 月被 tc39 拒绝

    要解决的问题

    try { return JSON.parse(str) } catch { return undefined }

    先前设计

    URL.canParse

    规范

    JSON.parse ( _text_ [ , _reviver_ ] )
    
    1. 让 _result_ 为 Call(%JSON.parse%, *null*, << _text_, _reviver_ >> ) 的补全记录。
    1. 如果 _result_ 是一个突然终止的补全,返回 *undefined*
    1. 返回 _result_