Skip to content

Commit

Permalink
Use boost for JSON parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
royfalk committed Dec 2, 2024
1 parent a86672e commit cc621ff
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 1,875 deletions.
76 changes: 23 additions & 53 deletions engine/CMakeLists.txt

Large diffs are not rendered by default.

14 changes: 0 additions & 14 deletions engine/src/cmd/carrier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,13 @@
#include "vs_logging.h"
#include "vega_cast_utils.h"

#include "json.h"

// TODO: find out where this is and maybe refactor
extern int SelectDockPort(Unit *, Unit *parent);
extern void SwitchUnits(Unit *, Unit *);
extern void abletodock(int dock);

// Replace with std:sto* here and at unit_csv.cpp
static double stof(const string &inp, double def = 0) {
if (inp.length() != 0) {
return XMLSupport::parse_float(inp);
}
return def;
}

static int stoi(const string &inp, int def = 0) {
if (inp.length() != 0) {
return XMLSupport::parse_int(inp);
}
return def;
}

// TODO: probably replace with a lambda expression
class CatCompare {
Expand Down
39 changes: 25 additions & 14 deletions engine/src/cmd/controls_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
#include <vector>
#include <string>
#include <map>
#include <boost/json.hpp>

#include "json.h"
#include "drawable.h"
#include "vs_globals.h"
#include "configxml.h"
#include "resource/json_utils.h"

#include "gui/staticdisplay.h"
#include "gui/newbutton.h"
Expand All @@ -55,24 +56,34 @@ std::map<std::string, std::map<std::string, std::string>> parseControlsJSON(VSFi

std::map<std::string, std::map<std::string, std::string>> controls_map;

std::vector<std::string> controls_json = json::parsing::parse_array(json_text.c_str());
// Iterate over root
for (const std::string &control_text : controls_json) {
json::jobject control_json = json::jobject::parse(control_text);
boost::json::value json_value = boost::json::parse(json_text);
boost::json::array root_array = json_value.get_array();

for(boost::json::value& control_value : root_array) {
boost::json::object control = control_value.get_object();
std::map<std::string, std::string> control_attributes;

for (const std::string &key : keys) {
// For some reason, parser adds quotes
if(control_json.has_key(key)) {
const std::string attribute = control_json.get(key);
const std::string stripped_attribute = attribute.substr(1, attribute.size() - 2);
control_attributes[key] = stripped_attribute;
}
}
if(!control.if_contains(key)) {
continue;
}

// Worked in singleson because it saw everything as string.
if(key == "multiline") {
bool multiline = JsonGetWithDefault(control, key, false);
if(multiline) {
control_attributes["multiline"] = "true";
} else {
control_attributes["multiline"] = "false";
}
} else {
const std::string attribute = JsonGetStringWithDefault(control, key, "");
control_attributes[key] = attribute;
}
}

controls_map[control_attributes["name"]] = control_attributes;
}
}

return controls_map;
}
Expand Down
Loading

0 comments on commit cc621ff

Please sign in to comment.