11std::string escapeJson(
const std::string &
s) {
13 out.reserve(
s.size() + 8);
16 case '\\': out +=
"\\\\";
break;
17 case '"': out +=
"\\\"";
break;
18 case '\n': out +=
"\\n";
break;
19 case '\r': out +=
"\\r";
break;
20 case '\t': out +=
"\\t";
break;
21 default: out +=
c;
break;
30 std::ostringstream oss;
32 <<
",\"algorithm\":\"" << escapeJson(grid.
getMeta(
"algorithm",
"")) <<
"\",\"cells\":[";
33 const auto &cells = grid.
cells();
34 for (
size_t i = 0; i < cells.size(); ++i) {
38 oss <<
"],\"semantics\":[";
39 for (
size_t i = 0; i < cells.size(); ++i) {
43 oss <<
"],\"objects\":[";
46 oss <<
"{\"name\":\"" << escapeJson(grid.
getObjectName(i)) <<
"\",\"type\":\""
61 std::ofstream ofs(path, std::ios::binary);
63 if (
error) *
error =
"failed to open json path: " + path;
68 if (
error) *
error =
"failed to write json path: " + path;
Intermediate 2D generation result. cells store semantic ids (see Semantic.h), not tile GIDs — convert...
std::string getObjectType(int i) const
float getObjectWidth(int i) const
float getObjectY(int i) const
float getObjectX(int i) const
int getObjectCount() const
int getObjectGid(int i) const
float getObjectHeight(int i) const
std::string getObjectName(int i) const
std::string getMeta(const std::string &key, const std::string &defaultValue) const
const std::vector< uint32_t > & cells() const
bool writeGridJson(const Grid2D &grid, const std::string &path, std::string *error)
const char * semanticName(uint32_t id)
std::string gridToJson(const Grid2D &grid)
Compact JSON for debugging / reload workflows (not full Tiled).