跳到正文
Skillpedia
通用编程75k · #2907

json-safety

它能做什么

这个技能检查从 API、浏览器存储或用户那里读取 JSON 的代码,确保解析错误得到处理。它面向处理外部数据的开发者。

适用场景

  • ✓应用在收到格式错误的 API 响应后崩溃的时候。
  • ✓浏览器存储的值损坏,导致页面出错的时候。
  • ✓你的代码在未检查数据结构的情况下就使用解析后的数据的时候。

技能链接

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/json-safety/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/json-safety/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请帮我把 JSON 解析写得安全,以防数据格式错误吗?”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会找出 JSON.parse 调用,检查每个调用是否都包裹了错误处理,并在使用结果前验证其结构。对于外部数据,它可以建议使用数据结构校验库。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Parse JSON safely with error handling

JSON.parse() on invalid input throws a SyntaxError that will crash your application if uncaught. API responses, localStorage values, and user-provided data can all be malformed. Safe parsing with validation catches these errors at the point of parsing, not deep in your application logic when a missing property causes an unexpected error.

Quick Reference

  • JSON.parse() throws a SyntaxError on invalid input — always use try/catch
  • Validate the shape of parsed data before accessing its properties
  • JSON.stringify() can return undefined for values that can't be serialized
  • Use a type-safe parser (Zod, Valibot) for data from external sources

Check

Find all JSON.parse() calls in this file and check whether each one is wrapped in try/catch and whether the result is validated before use.

Fix

Add try/catch error handling to all JSON.parse() calls and add shape validation to protect against unexpected data structures.

Explain

Explain why JSON.parse can throw, what safe JSON parsing looks like, and when to use schema validation libraries.

Code Review

Review scripts, client components, and browser execution paths related to Parse JSON safely with error handling. Flag exact imports, event handlers, runtime side effects, or blocking operations that violate the rule, and state how the change should be verified in the browser.


For full implementation details, code examples, and framework-specific guidance, see references/rule.md.

Rule page: https://frontendchecklist.io/rules/javascript/json-safety

常见问题

JSON 解析失败会怎样?

这个技能说,JSON.parse 会抛出语法错误,如果没有被捕获,就会导致应用崩溃。

为什么要验证解析后的数据?

这个技能说,这样可以在解析的那一刻发现意外的结构,而不是等到之后才出错。

文中提到了哪些库?

这个技能提到 Zod 和 Valibot,它们是用于外部数据的类型安全校验器。

详情

作者的原始描述

Use when reviewing scripts, client components, bundles, or runtime behavior related to Parse JSON safely with error handling. Inspect both source code and the browser execution path so fixes target the real bottleneck or bug.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#2907
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能