Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to the Boost JSON parser eventually? #688

Open
stephengtuggy opened this issue May 28, 2022 · 1 comment · May be fixed by #923
Open

Migrate to the Boost JSON parser eventually? #688

stephengtuggy opened this issue May 28, 2022 · 1 comment · May be fixed by #923

Comments

@stephengtuggy
Copy link
Contributor

@royfalk okay - WFM; but let's file an issue to migrate to the Boost one once we support the appropriate Boost version.

Originally posted by @BenjamenMeyer in #686 (comment)

@stephengtuggy stephengtuggy changed the title Migrate to the Boost JSON parser eventually Migrate to the Boost JSON parser eventually? May 28, 2022
@BenjamenMeyer BenjamenMeyer added this to the Backlog milestone May 31, 2022
@royfalk
Copy link
Contributor

royfalk commented Nov 28, 2024

@BenjamenMeyer

I think it's time. We've had JSON support for more than two years now.

Boost JSON was released in 1.75 on December 2020.

Boost JSON parsing:

const std::string json_text;
boost::json::value json_value = boost::json::parse(json_text);
boost::json::object root = json_value.get_object();

std::string string_key = boost::json::value_to<std::string>(root.at("string_key"));
int int_key = boost::json::value_to<int>(root.at("int_key"));
double double_key = boost::json::value_to<double>(root.at("double_key"));
bool bool_key = boost::json::value_to<bool>(root.at("bool_key"));

Singleson parsing:

const std::string json_text;
std::vector<std::string> units = json::parsing::parse_array(json_text.c_str());
   
// Iterate over root
for (const std::string &unit_text : units) {
        json::jobject unit = json::jobject::parse(unit_text);
        std::map<std::string, std::string> unit_attributes;

        for (const std::string &key : keys) {
            // For some reason, parser adds quotes
            if(unit.has_key(key)) {
                const std::string attribute = unit.get(key);
                const std::string stripped_attribute = attribute.substr(1, attribute.size() - 2);
                unit_attributes[key] = stripped_attribute;
                ...

Two things to note in the code comparison above:

  1. Singleson has this thing with strings, where it adds quotes inside the string, requiring an extra step.
  2. We don't have support for anything other than string in JSON. We have to use std::stoi, std:stob, etc. to parse stuff.

@royfalk royfalk linked a pull request Dec 3, 2024 that will close this issue
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants