Skip to content

Conversation

@HavardNJ-Laerdal
Copy link

@HavardNJ-Laerdal HavardNJ-Laerdal commented Nov 20, 2025

Current version throws for an unknown enum if the enum is considered small and handled with if-else

    inline void from_json(const json & j, Extrasystole & x) {
        if (j == "coupled_pvc") x = Extrasystole::CoupledPvc;
         ...
        else { throw std::runtime_error("Input JSON does not conform to schema!"); }

For a large enum implemented with map, the throw is missing

    inline void from_json(const json & j, BasicRhythm & x) {
        static std::unordered_map<std::string, BasicRhythm> enumValues {
            {"sinus", BasicRhythm::Sinus},
            ...
        };
        auto iter = enumValues.find(j.get<std::string>());
        if (iter != enumValues.end()) {
            x = iter->second;
        }
}

resulting in the property being a valid value which is inconsistent with the json.

This change adds the throw at the end of the large enum.

        ....
        if (iter != enumValues.end()) {
            x = iter->second;
        }
        else { throw std::runtime_error("Input JSON does not conform to schema!"); }

@HavardNJ-Laerdal HavardNJ-Laerdal changed the title Fix: throw error for both small and large enum in C++ Fix: throw error for undefined enum for both small and large enum in C++ Nov 20, 2025
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.

1 participant