#pragma once // Spec: SPEC.md FR-MOD-01 (tolerant decoding). Helpers over nlohmann::json. #include #include #include #include namespace dp { using Json = nlohmann::json; class JsonRead { public: // Missing key, null or wrong type yields nullopt (unknown-key tolerant, same optionality as Swift). template static std::optional opt(const Json& j, const char* key) { if (!j.is_object()) return std::nullopt; auto it = j.find(key); if (it == j.end() || it->is_null()) return std::nullopt; try { return it->get(); } catch (const Json::exception&) { return std::nullopt; } } template static T get(const Json& j, const char* key, T fallback = T{}) { auto v = opt(j, key); return v ? std::move(*v) : std::move(fallback); } static const Json* child(const Json& j, const char* key) { if (!j.is_object()) return nullptr; auto it = j.find(key); if (it == j.end() || it->is_null()) return nullptr; return &*it; } template static std::vector list(const Json& j, const char* key, F&& decode) { std::vector out; const Json* arr = child(j, key); if (!arr || !arr->is_array()) return out; out.reserve(arr->size()); for (const auto& item : *arr) out.push_back(decode(item)); return out; } }; } // namespace dp