Skip to content

Conversation

@Jarod42
Copy link
Collaborator

@Jarod42 Jarod42 commented Nov 26, 2025

No description provided.

JSON root{ config_file };
if (!root.isOk() || !root.toElement().isArray()) {
wxString content;
if (!FileUtils::ReadFileContent(config_file, content))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run clang-format

SourceFormatterBase::FromJSON(json);
m_command = json["command"].toArrayString();
m_workingDirectory = json["working_directory"].toString();
m_command = JsonUtils::ToArrayString(json["command"]);
Copy link
Owner

@eranif eranif Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might throw an exception, lets wrap it with try / catch block.
I suggest that we add new methods in JsonUtils.

For example:

template <typename T>
concept JsonExtractable = requires(const nlohmann::json& j)
{
    {j.template get<T>()}->std::same_as<T>;
};

template <typename T>
inline std::optional<T> GetValue(const nlohmann::json& json, const std::string& name) requires JsonExtractable<T>
{
    if (!json.contains(name)) {
        return std::nullopt;
    }
    try {
        return json[name].get<T>();
    } catch (const std::exception&) {
        return std::nullopt;
    }
}

We can later use with default value like this:

m_command = wxString::FromUTF8(JsonUtils::GetValue(json, "command").value_or(""));

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed afterward that all methods of JsonItem defaults it return value when "empty", whereas nlohmann throws exception for invalid access.
nlohmann::json::value(key, default_value) exists for that case too.

I have indeed to reworks my migrations.

m_name = json["name"].toString();
m_description = json["description"].toString();
m_shortDescription = json["short_description"].toString();
m_languages = JsonUtils::ToArrayString(json["languages"]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment about exceptions in nlohmann::json

@Jarod42 Jarod42 marked this pull request as draft November 28, 2025 08:52
@Jarod42 Jarod42 force-pushed the codeformatter_json_migration branch from f21ef50 to ddb2917 Compare November 29, 2025 13:11
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

Successfully merging this pull request may close these issues.

2 participants