1#ifndef LIBFILEZILLA_JSON_HEADER
2#define LIBFILEZILLA_JSON_HEADER
32class FZ_PUBLIC_SYMBOL json final
35 json()
noexcept =
default;
36 json(json
const&) =
default;
37 json(json &&)
noexcept =
default;
43 return static_cast<json_type>(value_.index());
56 template<
typename T, std::enable_if_t<std::is_
integral_v<
typename std::decay_t<T>>,
int> = 0>
58 auto v = number_value_o<T>();
65 template<
typename T, std::enable_if_t<std::is_
integral_v<
typename std::decay_t<T>>,
int> = 0>
66 std::optional<T> number_value_o()
const {
67 bool constexpr is_signed = std::is_signed_v<typename std::decay_t<T>>;
68 if constexpr (is_signed) {
69 std::optional<int64_t> tmp = number_value_integer_s();
71 if (tmp >= std::numeric_limits<T>::min() && tmp <= std::numeric_limits<T>::max()) {
72 return static_cast<T
>(*tmp);
77 std::optional<uint64_t> tmp = number_value_integer_u();
79 if (tmp <= std::numeric_limits<T>::max()) {
80 return static_cast<T
>(*tmp);
89 template<
typename T, std::enable_if_t<std::is_
floating_po
int_v<
typename std::decay_t<T>>,
int> = 0>
91 return static_cast<T
>(number_value_double());
98 void erase(std::string
const& name);
126 template<
typename Bool, std::enable_if_t<std::is_same_v<
bool,
typename std::decay_t<Bool>>,
int> = 0>
133 template<
typename T, std::enable_if_t<std::is_
integral_v<T> && !std::is_same_v<
bool,
typename std::decay_t<T>>,
int> = 0>
135 value_.emplace<std::size_t(json_type::number)>(
fz::to_string(n));
153 explicit operator
bool()
const {
return type() != json_type::none; }
155 bool has_non_null_value()
const {
161 bool is_array()
const {
return type() == fz::json_type::array; }
162 bool is_number()
const {
return type() == fz::json_type::number; }
163 bool is_boolean()
const {
return type() == fz::json_type::boolean; }
170 std::string
to_string(
bool pretty =
false,
size_t depth = 0)
const;
179 void to_string(std::string & ret,
bool pretty =
false,
size_t depth = 0)
const;
185 static json
parse(std::string_view
const& v,
size_t max_depth = 20);
191 void to_string_impl(std::string & ret,
bool pretty =
false,
size_t depth = 0)
const;
193 std::optional<int64_t> number_value_integer_s()
const;
194 std::optional<uint64_t> number_value_integer_u()
const;
195 double number_value_double()
const;
197 bool FZ_PRIVATE_SYMBOL check_type(
json_type t);
198 void FZ_PRIVATE_SYMBOL set_type(
json_type t);
200 static json FZ_PRIVATE_SYMBOL
parse(
char const*& p,
char const* end,
size_t max_depth);
202 typedef std::variant<
205 std::map<std::string, json, std::less<>>,
214template <
bool isconst>
215struct json_array_iterator final {
216 using json_ref_t = std::conditional_t<isconst, json const&, json &>;
220 json_array_iterator(json_ref_t j)
233 json_ref_t operator*()
const
238 bool operator!=(json_array_iterator::sentinel
const&)
const
240 return idx_ < json_.children();
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:28
json parser/builder
Definition json.hpp:33
static json parse(std::string_view const &v, size_t max_depth=20)
Parses JSON structure from input.
json & operator=(T n)
Sets type to number and assigns value.
Definition json.hpp:134
json & operator=(std::string_view const &v)
Sets type to string and assigns value.
std::string to_string(bool pretty=false, size_t depth=0) const
Serializes JSON structure.
json & operator[](size_t i)
Returns reference to the child value with the given index.
std::string string_value() const
Returns string, number and boolean values as string.
json const & operator[](std::string const &name) const
If object, get the value with the given name. Returns none if not object or name doesn't exist.
json & operator=(std::wstring_view const &v)
Sets type to string and assigns value.
Definition json.hpp:146
void erase(std::string const &name)
If object value, deletes child value with given name.
json & operator=(Bool b)
Sets type to boolean and assigns value.
Definition json.hpp:127
size_t children() const
For arrays and objects, returns the number of elements.
T number_value(T errorval={}) const
Returns number and string values as the passed integer type.
Definition json.hpp:57
void to_string(std::string &ret, bool pretty=false, size_t depth=0) const
Serializes JSON structure.
json & operator[](std::string const &name)
Returns reference to the child value with the given name.
T number_value() const
Returns number and string values as the passed floating point type.
Definition json.hpp:90
std::wstring wstring_value() const
Returns string, number and boolean values as wstring.
Definition json.hpp:50
json(json_type t)
Explicitly creates a value of a specific type, mainly needed for null objects.
bool bool_value() const
Returns boolean and string values as bool.
json const & operator[](size_t i) const
If array, get the value with the given index. Returns none if not array or index doesn't exist.
type
Definition logger.hpp:16
The namespace used by libfilezilla.
Definition apply.hpp:17
std::wstring to_wstring_from_utf8(std::string_view const &in)
Converts from std::string in UTF-8 into std::wstring.
std::string to_utf8(std::string_view const &in)
Converts from std::string in native encoding into std::string in UTF-8.
std::string to_string(std::wstring_view const &in)
Converts from std::wstring into std::string in system encoding.
json_type
Types of JSON values.
Definition json.hpp:18
@ null
Not a JSON value.
Definition json.hpp:20
@ object
The explicit null value.
Definition json.hpp:21
String types and assorted functions.