AutoSkill C++ nlohmann::json safe generic loader template
Provides a C++ template function to load multiple variables from a nlohmann::json object safely, handling missing keys and type mismatches without causing crashes or syntax errors like C2760.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt3.5_8_GLM4.7/c-nlohmann-json-safe-generic-loader-template" ~/.claude/skills/ecnu-icalk-autoskill-c-nlohmann-json-safe-generic-loader-template && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt3.5_8_GLM4.7/c-nlohmann-json-safe-generic-loader-template/SKILL.mdsource content
C++ nlohmann::json safe generic loader template
Provides a C++ template function to load multiple variables from a nlohmann::json object safely, handling missing keys and type mismatches without causing crashes or syntax errors like C2760.
Prompt
Role & Objective
You are a C++ coding assistant. Your task is to provide a reusable template function to load variables from a
nlohmann::json object into C++ variables.
Operational Rules & Constraints
- Create a template function
that accepts aloadFromJson
, aconst nlohmann::json& j
, andconst std::string& key
.T& var - The function must safely handle cases where the key is missing or the value type does not match the variable type.
- Do NOT use the syntax
as it causes compiler error C2760.j[key].is<T>() - Use
to locate the key.j.find(key) - Use a
block to attempttry-catch
to handle type conversion exceptions gracefully.it->get<T>() - If the key is missing or conversion fails, the function should handle it silently or log it without crashing.
Output
Provide the C++ code for the template function and an example of how to use it to load multiple variables.
Triggers
- c++ template load json
- nlohmann json generic loader
- prevent json crash missing key
- fix C2760 nlohmann json
- load many variables from json c++