/* TessesFramework a library to make C++ easier for me, used in CrossLang: https://git.tesses.org/tesses50/crosslang Copyright (C) 2026 Mike Nolan This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #pragma once #include #include #include #include #include #include namespace Tesses::Framework::Serialization::Json { class JArray; class JObject; using JUndefined = std::monostate; using JToken = std::variant; class JOItem; class JArray { std::vector items; public: JArray(); JArray(std::initializer_list items); std::vector &GetVector(); void Add(JToken item); void RemoveAt(size_t index); size_t Count(); void SetAt(size_t index, JToken item); JToken GetAt(size_t index); void Clear(); std::vector::iterator begin(); std::vector::iterator end(); }; class JObject { std::map map; public: JObject(); JObject(std::initializer_list items); void SetValue(std::string key, JToken item); template bool TryGetValueAsType(std::string key, T &value) { auto obj = GetValue(key); if (std::holds_alternative(obj)) { value = std::get(obj); return true; } return false; } JToken GetValue(std::string key); void Remove(std::string key); std::map &GetMap(); std::map::iterator begin(); std::map::iterator end(); }; template bool TryGetJToken(JToken json, T &item) { if (std::holds_alternative(json)) { item = std::get(json); return true; } return false; } class JOItem { public: JOItem(); JOItem(std::string key, JToken value); std::string key; JToken value; }; class Json { static std::string tab(std::string str); public: static JToken Decode(std::string str); static std::string Encode(JToken tkn, bool indent = true); static JArray DocDecode(std::string str); static std::string DocEncode(JArray array, bool indent = true); }; } // namespace Tesses::Framework::Serialization::Json