ワンクリックで
meojson
meojson C++ JSON 库使用指南。用于 MaaFramework C++ 项目中的 JSON 解析、序列化、MEO_JSONIZATION、json::value 操作、ext::jsonization 自定义类型支持,以及 Custom 参数解析。
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
meojson C++ JSON 库使用指南。用于 MaaFramework C++ 项目中的 JSON 解析、序列化、MEO_JSONIZATION、json::value 操作、ext::jsonization 自定义类型支持,以及 Custom 参数解析。
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
调试和优化 MaaFramework Pipeline JSON。用于对照 schema 验证配置,检测缺失引用、循环依赖、孤立节点、命名与行为不匹配、ROI/阈值问题,并给出可靠性、性能和可维护性改进建议。触发词包括 debug pipeline、validate pipeline、optimize pipeline、pipeline error。
MaaFramework Pipeline JSON 编写指南。用于编写、修改或审查 Pipeline JSON,设计节点流程,使用 TemplateMatch/OCR/ColorMatch/Custom 识别或 Click/Swipe/Custom 动作,并检查 MaaFramework 项目的通用 Pipeline 可靠性。
MDA 项目专属开发指南。用于在 MDA 中编写或审查 MaaFramework Pipeline、Project Interface 任务、locale、go-service 组件时补充项目结构、业务域、命名习惯和现有任务选项示例。与通用 pipeline-guide、pipeline-debug、generate-pi-task、go-service-guide 配合使用。
分析 Windows 崩溃转储文件(.dmp),诊断 MaaFramework / MXU 项目及其依赖项的崩溃。用于 issue 日志包或附件中发现 .dmp 文件,或用户要求分析 DMP / 崩溃转储时。
根据 MaaFramework Pipeline 定义生成或更新 Project Interface 任务 JSON。用于创建、编辑或扩展 assets/tasks/ 下的任务文件,将 Pipeline 节点映射为用户可配置选项(switch/select/checkbox/input)并绑定 pipeline_override。
MaaFramework C++ 日志宏用法指南。用于写 C++ MaaFramework 扩展、Custom 组件或工具代码时使用 LogInfo/LogError/LogWarn/LogDebug/LogTrace、VAR、容器输出和自定义类型日志。
| name | meojson |
| description | meojson C++ JSON 库使用指南。用于 MaaFramework C++ 项目中的 JSON 解析、序列化、MEO_JSONIZATION、json::value 操作、ext::jsonization 自定义类型支持,以及 Custom 参数解析。 |
meojson 是 MaaFramework 依赖中常用的 header-only C++ JSON 库。通常通过以下头文件引入:
#include <meojson/json.hpp>
| 类型 | 说明 |
|---|---|
json::value | 通用 JSON 值:null / bool / number / string / array / object |
json::array | JSON 数组,包装 std::vector<json::value> |
json::object | JSON 对象,包装 std::map<std::string, json::value> |
auto opt = json::parse(str); // std::optional<json::value>
auto file = json::open(path); // 从文件读取
auto jsonc = json::parsec(str); // JSONC,支持注释
推荐安全模式:
template <typename T>
T ParseParam(const char* text)
{
if (text == nullptr || std::strlen(text) == 0) {
return T {};
}
auto opt = json::parse(text);
if (!opt) {
LogError << "failed to parse json param" << VAR(text);
return T {};
}
T result {};
if (!result.from_json(*opt)) {
LogError << "failed to deserialize json param" << VAR(text);
return T {};
}
return result;
}
避免:
json::parse(str).value_or(json::object {}).as<T>();
这会静默吞掉 parse 失败,并可能在 required 字段缺失时抛异常。
json::value v1 = 42;
json::value v2 = "hello";
json::value v3 = true;
json::value v4 = nullptr;
json::array arr { 1, 2, "three" };
json::object obj {
{ "key1", "value1" },
{ "key2", 42 },
};
std::vector<int> vec = { 1, 2, 3 };
json::value v5 = vec;
std::map<std::string, int> map = { { "a", 1 } };
json::value v6 = map;
v.is_null();
v.is_boolean();
v.is_number();
v.is_string();
v.is_array();
v.is_object();
v.is<int>();
v.as_string();
v.as_string_view();
v.as_integer();
v.as_double();
v.as_boolean();
v.as_array();
v.as_object();
v.as<T>();
as_*() / as<T>() 类型不匹配时可能抛异常,只在确定数据合法时使用。
auto name = v.find<std::string>("name");
if (name) {
// use *name
}
std::string label = v.get("label", "default");
int count = v.get("config", "count", 0);
if (v.exists("key")) {
// ...
}
v.dumps(); // 紧凑字符串
v.dumps(4); // 4 空格缩进
v.format(); // pretty print
写回 MaaFramework detail 的常见模式:
template <typename T>
void WriteJsonDetail(MaaStringBuffer* out_detail, const T& payload)
{
if (out_detail == nullptr) {
return;
}
const std::string json_text = json::value(payload).dumps();
MaaStringBufferSet(out_detail, json_text.c_str());
}
json::value merged = obj1 | obj2; // 右侧覆盖同名键
obj1 |= obj2;
MEO_JSONIZATION(fields...) 生成 to_json()、check_json()、from_json()。
struct LocateOutput {
int status = 0;
std::string message;
std::string map_name;
int x = 0;
int y = 0;
MEO_JSONIZATION(status, message, MEO_OPT map_name, MEO_OPT x, MEO_OPT y)
};
序列化:
json::value j = data;
反序列化推荐:
LocateOutput data {};
if (!data.from_json(j)) {
LogError << "failed to deserialize" << VAR(j);
}
默认字段都是 required。用 MEO_OPT 标记可选字段,缺失时保留默认值:
struct Options {
double threshold = 0.7;
bool enabled = true;
MEO_JSONIZATION(MEO_OPT threshold, MEO_OPT enabled)
};
每个可选字段都需要自己的 MEO_OPT。
当 JSON key 与 C++ 字段名不同,使用 MEO_KEY:
struct TemplateParam {
std::vector<std::string> template_;
MEO_JSONIZATION(MEO_KEY("template") template_)
};
与 MEO_OPT 组合时顺序通常为:
MEO_OPT MEO_KEY("default") default_
| 宏 | 生成内容 |
|---|---|
MEO_TOJSON(...) | to_json() |
MEO_FROMJSON(...) | from_json() |
MEO_CHECKJSON(...) | check_json() |
MEO_JSONIZATION(...) | 全部三个 |
对不拥有源码的类型,特化 json::ext::jsonization<T>:
namespace json::ext {
template <>
class jsonization<MyRect> {
public:
json::value to_json(const MyRect& rect) const
{
return json::array { rect.x, rect.y, rect.width, rect.height };
}
bool check_json(const json::value& value) const
{
return value.is<std::vector<int>>() && value.as_array().size() == 4;
}
bool from_json(const json::value& value, MyRect& rect) const
{
auto arr = value.as<std::vector<int>>();
rect = MyRect { arr[0], arr[1], arr[2], arr[3] };
return true;
}
};
}
enum class MyEnum {
A,
B,
C,
MEOJSON_ENUM_RANGE(A, C)
};
json::value j = MyEnum::B;
MyEnum e = j.as<MyEnum>();
json::parse 返回 std::optional,必须检查。as_*() / as<T>() 类型不匹配时可能抛异常。.value_or(...).as<T>() 静默吞解析错误。char / wchar_t 构造被禁用,使用 std::string 或整数。ext::jsonization 位于 json::ext 命名空间。MEO_OPT。MEO_KEY 与 MEO_OPT 组合时注意顺序。详细 API 见 reference.md。