1 #ifndef LIBFILEZILLA_JSON_HEADER
2 #define LIBFILEZILLA_JSON_HEADER
10 #include <type_traits>
30 class FZ_PUBLIC_SYMBOL
json final
33 json() noexcept =
default;
48 std::string string_value()
const;
57 template<
typename T, std::enable_if_t<std::is_
integral_v<
typename std::decay_t<T>>,
int> = 0>
59 return static_cast<T
>(number_value_integer());
63 template<
typename T, std::enable_if_t<std::is_
floating_po
int_v<
typename std::decay_t<T>>,
int> = 0>
65 return static_cast<T
>(number_value_double());
69 bool bool_value()
const;
72 void erase(std::string
const& name);
75 json const& operator[](std::string
const& name)
const;
83 json& operator[](std::string
const& name);
86 json const& operator[](
size_t i)
const;
94 json& operator[](
size_t i);
97 size_t children()
const;
100 template<
typename Bool, std::enable_if_t<std::is_same_v<
bool,
typename std::decay_t<Bool>>,
int> = 0>
102 type_ = json_type::boolean;
108 template<
typename T, std::enable_if_t<std::is_
integral_v<T> && !std::is_same_v<
bool,
typename std::decay_t<T>>,
int> = 0>
110 type_ = json_type::number;
119 json& operator=(std::string_view
const& v);
129 explicit operator
bool()
const {
return type_ != json_type::none; }
136 std::string
to_string(
bool pretty =
false,
size_t depth = 0)
const;
145 void to_string(std::string & ret,
bool pretty =
false,
size_t depth = 0)
const;
151 static json parse(std::string_view
const& v,
size_t max_depth = 20);
152 static json parse(
fz::buffer const& b,
size_t max_depth = 20);
157 uint64_t number_value_integer()
const;
158 double number_value_double()
const;
163 static json parse(
char const*& p,
char const* end,
size_t max_depth);
165 typedef std::variant<std::string, std::map<std::string, json, std::less<>>, std::vector<json>,
bool> value_type;
170 template <
bool isconst>
172 using json_ref_t = std::conditional_t<isconst, json const&, json &>;
189 json_ref_t operator*()
const
194 bool operator!=(json_array_iterator::sentinel
const&)
const
196 return idx_ < json_.children();
204 inline json_array_iterator<false> begin(json &j) {
return {j}; }
205 inline json_array_iterator<false>::sentinel end(json &) {
return {}; }
207 inline json_array_iterator<true> begin(json
const& j) {
return {j}; }
208 inline json_array_iterator<true>::sentinel end(json
const&) {
return {}; }
json & operator=(T n)
Sets type to number and assigns value.
Definition: json.hpp:109
std::wstring wstring_value() const
Returns string, number and boolean values as wstring.
Definition: json.hpp:51
std::string to_utf8(std::string_view const &in)
Converts from std::string in native encoding into std::string in UTF-8.
json(json_type t)
Explicitly creates a value of a specific type, mainly needed for null objects.
Definition: json.hpp:38
std::wstring to_wstring_from_utf8(std::string_view const &in)
Converts from std::string in UTF-8 into std::wstring.
String types and assorted functions.
json parser/builder
Definition: json.hpp:30
The namespace used by libfilezilla.
Definition: apply.hpp:17
json_type
Types of JSON values.
Definition: json.hpp:16
json & operator=(std::wstring_view const &v)
Sets type to string and assigns value.
Definition: json.hpp:122
The buffer class is a simple buffer where data can be appended at the end and consumed at the front...
Definition: buffer.hpp:26
json & operator=(Bool b)
Sets type to boolean and assigns value.
Definition: json.hpp:101
T number_value() const
Returns number and string values as the passed integer type.
Definition: json.hpp:58
std::string to_string(std::wstring_view const &in)
Converts from std::wstring into std::string in system encoding.