MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::detail::LimitedJsonParseContext Class Reference

#include <JsonUtils.hpp>

Collaboration diagram for mxl::lib::detail::LimitedJsonParseContext:

Public Member Functions

 LimitedJsonParseContext (LimitedJsonParseContext const &)=delete
 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 that are nested if depth == limits.maxDepth.
LimitedJsonParseContext & operator= (LimitedJsonParseContext const &)=delete
template<typename Iter>
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 container length.
bool parse_array_start ()
 Start parsing an array. Called by picojson. Throws an error if already nested to the configured limit.
bool parse_array_stop (std::size_t)
template<typename Iter>
bool parse_object_item (picojson::input< Iter > &in, std::string const &key)
bool parse_object_start ()
template<typename Iter>
bool parse_string (picojson::input< Iter > &in)
 Start parsing a string. Called by picojson, delegates to picojsons internal string parser.
bool set_bool (bool value)
 Set the current value to the boolean value value. Called by picojson when parsing.
bool set_null ()
 Set the current value to null. Called by picojson when parsing.
bool set_number (double value)
 Set the current value to a numerical value value. Called by picojson when parsing.

Private Member Functions

bool checkDepth ()

Private Attributes

std::size_t _depth
 Number of arrays and objects that enclose the value this context parses.
std::string * _error
JsonLimits const * _limits
picojson::value * _out

Detailed Description

picojson parse context that builds a value like picojson::default_parse_context and stops when a limit in JsonLimits is exceeded or an object has a duplicate key. The reason is stored in the shared error string.

Definition at line 36 of file JsonUtils.hpp.

Constructor & Destructor Documentation

◆ LimitedJsonParseContext() [1/2]

mxl::lib::detail::LimitedJsonParseContext::LimitedJsonParseContext ( picojson::value * out,
JsonLimits const & limits,
std::string & error,
std::size_t depth )
inlinenoexcept

Construct a new LimitedJsonParseContext for a value, with given limits. Refuses to parse any contexts that are nested if depth == limits.maxDepth.

Definition at line 42 of file JsonUtils.hpp.

43 : _out{out}
44 , _limits{&limits}
45 , _error{&error}
46 , _depth{depth}
47 {}
std::size_t _depth
Number of arrays and objects that enclose the value this context parses.

◆ LimitedJsonParseContext() [2/2]

mxl::lib::detail::LimitedJsonParseContext::LimitedJsonParseContext ( LimitedJsonParseContext const & )
delete

Member Function Documentation

◆ checkDepth()

bool mxl::lib::detail::LimitedJsonParseContext::checkDepth ( )
inlineprivate

Check if any further nesting would exceed the limit.

Definition at line 163 of file JsonUtils.hpp.

164 {
165 if (_depth >= _limits->maxDepth)
166 {
167 *_error = fmt::format("JSON nesting is deeper than {} levels.", _limits->maxDepth);
168 return false;
169 }
170 return true;
171 }

◆ operator=()

LimitedJsonParseContext & mxl::lib::detail::LimitedJsonParseContext::operator= ( LimitedJsonParseContext const & )
delete

◆ parse_array_item()

template<typename Iter>
bool mxl::lib::detail::LimitedJsonParseContext::parse_array_item ( picojson::input< Iter > & in,
std::size_t index )
inline

Parse an array item. Called by picojson. Throws an error if index exceeded the configured maximum container length.

Definition at line 109 of file JsonUtils.hpp.

110 {
111 if (index >= _limits->maxContainerSize)
112 {
113 *_error = fmt::format("JSON array has more than {} items.", _limits->maxContainerSize);
114 return false;
115 }
116 auto& array = _out->get<picojson::array>();
117 array.emplace_back();
118 auto child = LimitedJsonParseContext{&array.back(), *_limits, *_error, _depth + 1U};
119 return picojson::_parse(child, in);
120 }
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...
Definition JsonUtils.hpp:42
uint64_t index

◆ parse_array_start()

bool mxl::lib::detail::LimitedJsonParseContext::parse_array_start ( )
inline

Start parsing an array. Called by picojson. Throws an error if already nested to the configured limit.

Definition at line 97 of file JsonUtils.hpp.

98 {
99 if (!checkDepth())
100 {
101 return false;
102 }
103 *_out = picojson::value{picojson::array_type, false};
104 return true;
105 }

◆ parse_array_stop()

bool mxl::lib::detail::LimitedJsonParseContext::parse_array_stop ( std::size_t )
inline

Stop parsing an array. Called by picojson.

Definition at line 123 of file JsonUtils.hpp.

124 {
125 return true;
126 }

◆ parse_object_item()

template<typename Iter>
bool mxl::lib::detail::LimitedJsonParseContext::parse_object_item ( picojson::input< Iter > & in,
std::string const & key )
inline

Parse and item in an object. Called by picojson. Throws an error if there is a duplicate key, of if the number of items in this object already exceeds the limit.

Definition at line 142 of file JsonUtils.hpp.

143 {
144 auto& object = _out->get<picojson::object>();
145 if (object.size() >= _limits->maxContainerSize)
146 {
147 *_error = fmt::format("JSON object has more than {} members.", _limits->maxContainerSize);
148 return false;
149 }
150 // picojson keeps the last of several equal keys. Reject them, because other parsers may keep the first.
151 auto const [it, inserted] = object.try_emplace(key);
152 if (!inserted)
153 {
154 *_error = fmt::format("JSON object has the key '{}' more than once.", key);
155 return false;
156 }
157 auto child = LimitedJsonParseContext{&it->second, *_limits, *_error, _depth + 1U};
158 return picojson::_parse(child, in);
159 }
std::size_t size() const noexcept
Returns the total number of writers that are registered in the DomainWatcher.

◆ parse_object_start()

bool mxl::lib::detail::LimitedJsonParseContext::parse_object_start ( )
inline

Start parsing an object. Throws an error if the maximum configured object depth is already reached.

Definition at line 129 of file JsonUtils.hpp.

130 {
131 if (!checkDepth())
132 {
133 return false;
134 }
135 *_out = picojson::value{picojson::object_type, false};
136 return true;
137 }

◆ parse_string()

template<typename Iter>
bool mxl::lib::detail::LimitedJsonParseContext::parse_string ( picojson::input< Iter > & in)
inline

Start parsing a string. Called by picojson, delegates to picojsons internal string parser.

Definition at line 90 of file JsonUtils.hpp.

91 {
92 *_out = picojson::value{picojson::string_type, false};
93 return picojson::_parse_string(_out->get<std::string>(), in);
94 }

◆ set_bool()

bool mxl::lib::detail::LimitedJsonParseContext::set_bool ( bool value)
inline

Set the current value to the boolean value value. Called by picojson when parsing.

Definition at line 60 of file JsonUtils.hpp.

61 {
62 *_out = picojson::value{value};
63 return true;
64 }

◆ set_null()

bool mxl::lib::detail::LimitedJsonParseContext::set_null ( )
inline

Set the current value to null. Called by picojson when parsing.

Definition at line 53 of file JsonUtils.hpp.

54 {
55 *_out = picojson::value{};
56 return true;
57 }

◆ set_number()

bool mxl::lib::detail::LimitedJsonParseContext::set_number ( double value)
inline

Set the current value to a numerical value value. Called by picojson when parsing.

Definition at line 76 of file JsonUtils.hpp.

77 {
78 // picojson::value throws std::overflow_error for these. Report them as a parse error instead.
79 if (!std::isfinite(value))
80 {
81 *_error = "JSON number is out of range.";
82 return false;
83 }
84 *_out = picojson::value{value};
85 return true;
86 }

Member Data Documentation

◆ _depth

std::size_t mxl::lib::detail::LimitedJsonParseContext::_depth
private

Number of arrays and objects that enclose the value this context parses.

Definition at line 178 of file JsonUtils.hpp.

◆ _error

std::string* mxl::lib::detail::LimitedJsonParseContext::_error
private

Definition at line 176 of file JsonUtils.hpp.

◆ _limits

JsonLimits const* mxl::lib::detail::LimitedJsonParseContext::_limits
private

Definition at line 175 of file JsonUtils.hpp.

◆ _out

picojson::value* mxl::lib::detail::LimitedJsonParseContext::_out
private

Definition at line 174 of file JsonUtils.hpp.


The documentation for this class was generated from the following file: