Unverified Commit bf1da3c6 authored by Sébastien Villemot's avatar Sébastien Villemot
Browse files

JSON output: fix logic in “variable_mapping” that could lead to invalid JSON formatting

Ref. dynare#1755
parent ec779235
......@@ -6037,17 +6037,24 @@ void
DynamicModel::writeJsonVariableMapping(ostream &output) const
{
output << R"("variable_mapping":[)" << endl;
int ii = 0;
int end_idx_map = static_cast<int>(variableMapping.size()-1);
for (const auto &variable : variableMapping)
{
output << R"({"name": ")" << symbol_table.getName(variable.first) << R"(", "equations":[)";
int it = 0;
int end_idx_eq = static_cast<int>(variable.second.size())-1;
for (const auto &equation : variable.second)
if (auto tmp = equation_tags.getTagValueByEqnAndKey(equation, "name"); !tmp.empty())
output << R"(")" << tmp << (it++ == end_idx_eq ? R"("])" : R"(", )");
output << (ii++ == end_idx_map ? R"(})" : R"(},)") << endl;
for (auto it = variableMapping.begin(); it != variableMapping.end(); ++it)
{
if (it != variableMapping.begin())
output << ", ";
auto [var, eqs] = *it;
output << R"({"name": ")" << symbol_table.getName(var) << R"(", "equations":[)";
bool first_eq = true;
for (auto it2 = eqs.begin(); it2 != eqs.end(); ++it2)
if (auto tmp = equation_tags.getTagValueByEqnAndKey(*it2, "name");
!tmp.empty())
{
if (first_eq)
first_eq = false;
else
output << ", ";
output << '"' << tmp << '"';
}
output << "]}" << endl;
}
output << "]";
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment