载入中...
搜索中...
未找到
McpSquirrelJson.hpp
浏览该文件的文档.
1#pragma once
2
3// Squirrel -> compact JSON conversion shared by the embedded MCP server and the
4// script tool registry. Kept in one place so a value produced by a scene kit and
5// a value returned by a project tool serialize identically.
6
8
9#include <squirrel.h>
10
11#include <string>
12
13namespace eve::dev {
14
22constexpr int kSqJsonMaxDepth = 16;
23
25inline std::string sqValueToJson(HSQUIRRELVM vm, SQInteger idx, int depth = 0);
26
34inline std::string sqKeyToJson(HSQUIRRELVM vm, SQInteger idx) {
35 if (idx < 0) idx = sq_gettop(vm) + idx + 1;
36 if (sq_gettype(vm, idx) == OT_STRING) {
37 const SQChar* value = nullptr;
38 sq_getstring(vm, idx, &value);
39 return std::string("\"") + mcpJsonEscape(value ? value : "") + "\"";
40 }
41 const std::string text = sqValueToJson(vm, idx, 0);
42 if (text.empty() || text.front() != '"') return "\"" + mcpJsonEscape(text) + "\"";
43 return text;
44}
45
53inline std::string sqValueToJson(HSQUIRRELVM vm, SQInteger idx, int depth) {
54 if (idx < 0) idx = sq_gettop(vm) + idx + 1; // normalize relative -> absolute
55 if (depth > kSqJsonMaxDepth) return "null";
56 switch (sq_gettype(vm, idx)) {
57 case OT_NULL: return "null";
58 case OT_BOOL: {
59 SQBool value = SQFalse;
60 sq_getbool(vm, idx, &value);
61 return value ? "true" : "false";
62 }
63 case OT_INTEGER: {
64 SQInteger value = 0;
65 sq_getinteger(vm, idx, &value);
66 return std::to_string(static_cast<long long>(value));
67 }
68 case OT_FLOAT: {
69 SQFloat value = 0;
70 sq_getfloat(vm, idx, &value);
71 char buffer[64];
72 std::snprintf(buffer, sizeof(buffer), "%g", static_cast<double>(value));
73 return buffer;
74 }
75 case OT_STRING: {
76 const SQChar* value = nullptr;
77 sq_getstring(vm, idx, &value);
78 return std::string("\"") + mcpJsonEscape(value ? value : "") + "\"";
79 }
80 case OT_ARRAY: {
81 std::string out = "[";
82 bool first = true;
83 sq_pushnull(vm);
84 while (SQ_SUCCEEDED(sq_next(vm, idx))) {
85 if (!first) out += ",";
86 first = false;
87 out += sqValueToJson(vm, -1, depth + 1);
88 sq_pop(vm, 2);
89 }
90 sq_pop(vm, 1);
91 out += "]";
92 return out;
93 }
94 case OT_TABLE: {
95 std::string out = "{";
96 bool first = true;
97 sq_pushnull(vm);
98 while (SQ_SUCCEEDED(sq_next(vm, idx))) {
99 if (!first) out += ",";
100 first = false;
101 out += sqKeyToJson(vm, -2);
102 out += ":";
103 out += sqValueToJson(vm, -1, depth + 1);
104 sq_pop(vm, 2);
105 }
106 sq_pop(vm, 1);
107 out += "}";
108 return out;
109 }
110 default: return "\"<unserializable>\"";
111 }
112}
113
114} // namespace eve::dev
double value
struct SQVM * HSQUIRRELVM
HSQUIRRELVM vm
Definition ECS.cpp:20
std::int32_t first
std::string text
std::unique_ptr< gpgpu::GpuBuffer > buffer
Definition OnnxGpgpu.cpp:26
int idx
std::uint32_t depth
constexpr int kSqJsonMaxDepth
Maximum container nesting serialized from a script value.
std::string mcpJsonEscape(const std::string &value)
Escape a raw string for embedding inside a JSON string literal.
Definition McpJson.hpp:30
std::string sqKeyToJson(HSQUIRRELVM vm, SQInteger idx)
Serialize a Squirrel table key as a JSON string literal.
std::string sqValueToJson(HSQUIRRELVM vm, SQInteger idx, int depth=0)
Forward declaration: the key serializer falls back to the value serializer.