15#include <fmt/format.h>
16#include <picojson/wrapper.h>
55 *
_out = picojson::value{};
62 *
_out = picojson::value{value};
66#ifdef PICOJSON_USE_INT64
68 bool set_int64(std::int64_t value)
70 *
_out = picojson::value{value};
79 if (!std::isfinite(value))
81 *
_error =
"JSON number is out of range.";
84 *
_out = picojson::value{value};
89 template<
typename Iter>
92 *
_out = picojson::value{picojson::string_type,
false};
93 return picojson::_parse_string(
_out->get<std::string>(), in);
103 *
_out = picojson::value{picojson::array_type,
false};
108 template<
typename Iter>
113 *
_error = fmt::format(
"JSON array has more than {} items.",
_limits->maxContainerSize);
116 auto& array =
_out->get<picojson::array>();
117 array.emplace_back();
119 return picojson::_parse(child, in);
135 *
_out = picojson::value{picojson::object_type,
false};
141 template<
typename Iter>
144 auto&
object =
_out->get<picojson::object>();
147 *
_error = fmt::format(
"JSON object has more than {} members.",
_limits->maxContainerSize);
151 auto const [it, inserted] =
object.try_emplace(key);
154 *
_error = fmt::format(
"JSON object has the key '{}' more than once.", key);
158 return picojson::_parse(child, in);
167 *
_error = fmt::format(
"JSON nesting is deeper than {} levels.",
_limits->maxDepth);
195 if (text.size() > limits.maxInputSize)
197 return fmt::format(
"JSON input has {} bytes, the limit is {}.", text.size(), limits.maxInputSize);
200 auto limitError = std::string{};
201 auto syntaxError = std::string{};
203 auto const end = picojson::_parse(ctx, text.begin(), text.end(), &syntaxError);
204 if (!limitError.empty())
208 if (!syntaxError.empty())
213 auto const isWhitespace = [](
char c)
noexcept
215 return (c ==
' ') || (c ==
'\t') || (c ==
'\n') || (c ==
'\r');
217 if (!std::all_of(end, text.end(), isWhitespace))
219 return "Unexpected content after the JSON value.";
235 template<std::
integral T>
238 if (!value.is<
double>())
240 throw std::invalid_argument{fmt::format(
"'{}' must be a number.", name)};
243 auto const number = value.get<
double>();
247 auto const upper = std::ldexp(1.0, std::numeric_limits<T>::digits);
248 auto const lower = std::numeric_limits<T>::is_signed ? -upper : 0.0;
249 if ((std::trunc(number) != number) || (number < lower) || (number >= upper))
251 throw std::invalid_argument{fmt::format(
252 "'{}' must be a whole number between {} and {}, got {}.", name, std::numeric_limits<T>::min(), std::numeric_limits<T>::max(), number)};
254 return static_cast<T
>(number);
269 template<std::
integral T>
272 if (!value.is<std::string>())
274 throw std::invalid_argument{fmt::format(
"'{}' must be a string.", name)};
LimitedJsonParseContext(picojson::value *out, JsonLimits const &limits, std::string &error, std::size_t depth) noexcept
Construct a new LimitedJsonParseContext for a value, with given limits. Refuses to parse any contexts...
bool parse_array_stop(std::size_t)
bool parse_array_start()
Start parsing an array. Called by picojson. Throws an error if already nested to the configured limit...
bool set_null()
Set the current value to null. Called by picojson when parsing.
bool parse_array_item(picojson::input< Iter > &in, std::size_t index)
Parse an array item. Called by picojson. Throws an error if index exceeded the configured maximum con...
LimitedJsonParseContext(LimitedJsonParseContext const &)=delete
JsonLimits const * _limits
bool parse_object_item(picojson::input< Iter > &in, std::string const &key)
bool set_number(double value)
Set the current value to a numerical value value. Called by picojson when parsing.
std::size_t _depth
Number of arrays and objects that enclose the value this context parses.
bool parse_object_start()
bool set_bool(bool value)
Set the current value to the boolean value value. Called by picojson when parsing.
LimitedJsonParseContext & operator=(LimitedJsonParseContext const &)=delete
bool parse_string(picojson::input< Iter > &in)
Start parsing a string. Called by picojson, delegates to picojsons internal string parser.
std::size_t size() const noexcept
Returns the total number of writers that are registered in the DomainWatcher.
T stringToInteger(std::string_view text, std::string_view name)
T jsonStringToInteger(picojson::value const &value, std::string_view name)
T jsonToInteger(picojson::value const &value, std::string_view name)
std::string parseJson(picojson::value &out, std::string_view text, JsonLimits const &limits={})
Limits applied by parseJson().
std::size_t maxDepth
Largest number of nested arrays and objects. picojson parses recursively, so this also bounds stack u...
std::size_t maxContainerSize
Largest number of items in one array or members in one object.
std::size_t maxInputSize
Largest accepted input in bytes. This also bounds the length of any string in the document.