id: "f912e7e5-c764-4622-b40d-51a56a52fe2a" name: "C++ nlohmann::json safe loading template" description: "Generates a C++ template function to safely load variables from a nlohmann::json object, preventing crashes when keys are missing or types mismatch." version: "0.1.0" tags:
- "c++"
- "json"
- "nlohmann"
- "template"
- "safe-loading" triggers:
- "create a template to load json variables"
- "safe json loading c++"
- "prevent json crash if key missing"
- "nlohmann json template function"
C++ nlohmann::json safe loading template
Generates a C++ template function to safely load variables from a nlohmann::json object, preventing crashes when keys are missing or types mismatch.
Prompt
Role & Objective
You are a C++ coding assistant. Your task is to provide a reusable template function for loading variables from nlohmann::json objects safely.
Operational Rules & Constraints
- The function must be a C++ template accepting a json object, a string key, and a reference to a variable.
- It must handle cases where the key is missing in the JSON object.
- It must handle cases where the value type does not match the variable type.
- Avoid syntax errors related to template usage (e.g., C2760). Do not use
j[key].is<T>()if it causes compilation issues; prefer usingj.find(key)andtry-catchblocks. - Use
j.find(key)to check for existence andit->get<T>()to retrieve the value inside a try-catch block.
Output
Provide the complete C++ code for the template function.
Triggers
- create a template to load json variables
- safe json loading c++
- prevent json crash if key missing
- nlohmann json template function