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

yaml string type not reserved & empty array and map not reserved #7

Open
SplitGemini opened this issue Mar 17, 2023 · 2 comments
Open

Comments

@SplitGemini
Copy link

yaml string type not reserved

Types of numeric strings in yaml will be erased after conversion.

number_string: "123456"

after convertion:

{
	"number_string": 123456
}

empty array and map not reserved

empty_array: []

after convertion:

{
	"empty_array": null
}
@SplitGemini
Copy link
Author

yaml string type not reserved

fix by modify function at tojson.hpp#L75

inline nlohmann::json parse_scalar(const YAML::Node &node) {
    int         i;
    double      d;
    bool        b;
    std::string s;

    // string tag will be !
    if (node.Tag() != "!") {
        if (YAML::convert<int>::decode(node, i))
            return i;
        if (YAML::convert<double>::decode(node, d))
            return d;
        if (YAML::convert<bool>::decode(node, b))
            return b;
    }
    if (YAML::convert<std::string>::decode(node, s))
        return s;

    return nullptr;
}

empty array and map not reserved

fix by modify function at tojson.hpp#L90

inline nlohmann::json yaml2json(const YAML::Node &root) {
    nlohmann::json j{};

    switch (root.Type()) {
        case YAML::NodeType::Null:
            break;
        case YAML::NodeType::Scalar:
            return parse_scalar(root);
        case YAML::NodeType::Sequence:
            j = nlohmann::json::array();
            for (auto &&node : root)
                j.emplace_back(yaml2json(node));
            break;
        case YAML::NodeType::Map:
            j = nlohmann::json::object();
            for (auto &&it : root)
                j[it.first.as<std::string>()] = yaml2json(it.second);
            break;
        default:
            break;
    }
    return j;
}

@mircodz
Copy link
Owner

mircodz commented Mar 23, 2023

Feel free to open a PR request, we can get this fix merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants