json.hh (1049B)
1 #ifndef json_hh_INCLUDED 2 #define json_hh_INCLUDED 3 4 #include "hash_map.hh" 5 #include "string.hh" 6 #include "string_utils.hh" 7 #include "value.hh" 8 9 namespace Kakoune 10 { 11 12 using JsonArray = Vector<Value>; 13 using JsonObject = HashMap<String, Value>; 14 15 String to_json(int i); 16 String to_json(bool b); 17 String to_json(StringView str); 18 19 template<typename T> 20 String to_json(ArrayView<const T> array) 21 { 22 return "[" + join(array | transform([](auto&& elem) { return to_json(elem); }), ", ") + "]"; 23 } 24 25 template<typename T, MemoryDomain D> 26 String to_json(const Vector<T, D>& vec) { return to_json(ArrayView<const T>{vec}); } 27 28 template<typename K, typename V, MemoryDomain D> 29 String to_json(const HashMap<K, V, D>& map) 30 { 31 return "{" + join(map | transform([](auto&& i) { return format("{}: {}", to_json(i.key), to_json(i.value)); }), 32 ',', false) + "}"; 33 } 34 35 struct JsonResult { Value value; const char* new_pos; }; 36 37 JsonResult parse_json(const char* pos, const char* end); 38 JsonResult parse_json(StringView json); 39 40 } 41 42 #endif // json_hh_INCLUDED