Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions include/picojson/picojson.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ extern "C" {
#pragma warning(disable : 4244) // conversion from int to char
#pragma warning(disable : 4127) // conditional expression is constant
#pragma warning(disable : 4702) // unreachable code
#pragma warning(disable : 4706) // assignment within conditional expression
#else
#define SNPRINTF snprintf
#endif
Expand All @@ -129,7 +130,7 @@ enum {
#endif
};

enum { INDENT_WIDTH = 2 };
enum { INDENT_WIDTH = 2, DEFAULT_MAX_DEPTHS = 100 };

struct null {};

Expand Down Expand Up @@ -383,7 +384,7 @@ GET(array, *u_.array_)
GET(object, *u_.object_)
#ifdef PICOJSON_USE_INT64
GET(double,
(type_ == int64_type && (const_cast<value *>(this)->type_ = number_type, const_cast<value *>(this)->u_.number_ = u_.int64_),
(type_ == int64_type && (const_cast<value *>(this)->type_ = number_type, (const_cast<value *>(this)->u_.number_ = u_.int64_)),
u_.number_))
GET(int64_t, u_.int64_)
#else
Expand Down Expand Up @@ -838,7 +839,7 @@ template <typename Context, typename Iter> inline bool _parse_object(Context &ct
return false;
}
if (in.expect('}')) {
return true;
return ctx.parse_object_stop();
}
do {
std::string key;
Expand All @@ -849,7 +850,7 @@ template <typename Context, typename Iter> inline bool _parse_object(Context &ct
return false;
}
} while (in.expect(','));
return in.expect('}');
return in.expect('}') && ctx.parse_object_stop();
}

template <typename Iter> inline std::string _parse_number(input<Iter> &in) {
Expand Down Expand Up @@ -965,9 +966,10 @@ class deny_parse_context {
class default_parse_context {
protected:
value *out_;
size_t depths_;

public:
default_parse_context(value *out) : out_(out) {
default_parse_context(value *out, size_t depths = DEFAULT_MAX_DEPTHS) : out_(out), depths_(depths) {
}
bool set_null() {
*out_ = value();
Expand All @@ -992,42 +994,55 @@ class default_parse_context {
return _parse_string(out_->get<std::string>(), in);
}
bool parse_array_start() {
if (depths_ == 0)
return false;
--depths_;
*out_ = value(array_type, false);
return true;
}
template <typename Iter> bool parse_array_item(input<Iter> &in, size_t) {
array &a = out_->get<array>();
a.push_back(value());
default_parse_context ctx(&a.back());
default_parse_context ctx(&a.back(), depths_);
return _parse(ctx, in);
}
bool parse_array_stop(size_t) {
++depths_;
return true;
}
bool parse_object_start() {
if (depths_ == 0)
return false;
*out_ = value(object_type, false);
return true;
}
template <typename Iter> bool parse_object_item(input<Iter> &in, const std::string &key) {
object &o = out_->get<object>();
default_parse_context ctx(&o[key]);
default_parse_context ctx(&o[key], depths_);
return _parse(ctx, in);
}
bool parse_object_stop() {
++depths_;
return true;
}

private:
default_parse_context(const default_parse_context &);
default_parse_context &operator=(const default_parse_context &);
};

class null_parse_context {
protected:
size_t depths_;

public:
struct dummy_str {
void push_back(int) {
}
};

public:
null_parse_context() {
null_parse_context(size_t depths = DEFAULT_MAX_DEPTHS) : depths_(depths) {
}
bool set_null() {
return true;
Expand All @@ -1048,20 +1063,31 @@ class null_parse_context {
return _parse_string(s, in);
}
bool parse_array_start() {
if (depths_ == 0)
return false;
--depths_;
return true;
}
template <typename Iter> bool parse_array_item(input<Iter> &in, size_t) {
return _parse(*this, in);
}
bool parse_array_stop(size_t) {
++depths_;
return true;
}
bool parse_object_start() {
if (depths_ == 0)
return false;
--depths_;
return true;
}
template <typename Iter> bool parse_object_item(input<Iter> &in, const std::string &) {
++depths_;
return _parse(*this, in);
}
bool parse_object_stop() {
return true;
}

private:
null_parse_context(const null_parse_context &);
Expand Down