通过 IPC 调用 json-parser.js 对 static 目录下的 JSON 做创建、读取、更新和存在检查。路径相对于 static,如 jason/testjson → static/jason/testjson.json(无 .json 会自动补全)。
import { createJsonFile, readJsonFile, updateJsonFile, checkJsonFileExists } from './device.js'
await createJsonFile('jason/testjson', { devices: [] })
const data = await readJsonFile('jason/testjson') // 整个文件
const ip = await readJsonFile('jason/testjson', ['devices', 0, 'ip']) // 键路径
await updateJsonFile('jason/testjson', { newKey: 'value' }) // 整文件合并
await updateJsonFile('jason/testjson', true, ['settings', 'autoConnect']) // 按路径更新
const exists = await checkJsonFileExists('jason/testjson')
| 操作 | 参数 | 返回 |
|---|---|---|
create |
filePath, jsonString |
{ success, message } |
read |
filePath, 可选 keyPathJson |
{ success, data },无文件时 data: null |
update |
filePath, jsonString, 可选 keyPathJson |
{ success, message } |
check |
filePath |
{ success, exists } |
调用方式:window.electronAPI.runNodejsScript('json-parser', operation, ...args),返回的 stdout 需 JSON.parse()。键路径为数组并 JSON.stringify,如 ['devices', 0, 'ip']。
static 下,禁止 .. 和绝对路径。JSON.stringify();读回后对 stdout 做 JSON.parse()。{ success: false, error: "..." };读操作文件不存在为 { success: true, data: null }。