#pragma once #include "./transformer.hpp" namespace lsp::transform { // ==================== JSON 序列化/反序列化 ==================== template std::optional Deserialize(const std::string& json); template std::optional Serialize(const T& obj); // ==================== 转换为 LSPAny ==================== /// 任意类型转换为 LSPAny /// /// 支持的类型: /// - 基本类型:bool, int, double, string, nullptr /// - 容器类型:vector, map, optional /// - LSP 类型:LSPObject, LSPArray, LSPAny /// - 用户结构体(需要 glaze 支持) inline constexpr auto ToLSPAny = [](const auto& value) { return LSPAnyConverter::ToLSPAny(value); }; /// LSPAny 转换为指定类型 /// /// 支持的类型: /// - 基本类型:bool, int, double, string /// - 容器类型:vector, optional /// - LSP 类型:LSPObject, LSPArray /// - 用户结构体(需要 glaze 支持) inline constexpr auto FromLSPAny = [](const auto& input) -> T { using InputType = std::decay_t; protocol::LSPAny any; // 如果是 variant,先提取 if constexpr (std::is_same_v>) any = std::visit([](const auto& v) -> protocol::LSPAny { return v; }, input); // 否则直接用(LSPAny、LSPObject、LSPArray 都能隐式转换) else any = input; return LSPAnyConverter::FromLSPAny(any); }; namespace check { /// 检查是否为 LSPObject bool IsObject(const protocol::LSPAny& any); /// 检查是否为 LSPArray bool IsArray(const protocol::LSPAny& any); /// 检查是否为字符串 bool IsString(const protocol::LSPAny& any); /// 检查是否为数字(integer/uinteger/decimal) bool IsNumber(const protocol::LSPAny& any); /// 检查是否为布尔值 bool IsBool(const protocol::LSPAny& any); /// 检查是否为 null bool IsNull(const protocol::LSPAny& any); } // ==================== 调试工具 ==================== namespace debug { /// 获取 LSPAny 的类型名称字符串 std::string GetTypeName(const protocol::LSPAny& any); /// 将 variant 转换为字符串(用于 ID) std::string GetIdString(const std::variant& id); } } #include "./facade.inl"