[x265] [PATCH] Update json11 to v1.0.0 release
Nomis101 🐝
Nomis101 at web.de
Sat Jan 26 14:05:46 CET 2019
# HG changeset patch
# User Nomis101 <Nomis101 at web.de>
# Date 1548440484 -3600
# Fri Jan 25 19:21:24 2019 +0100
# Node ID 0ed9b73b07b21ffdd0d5c12ab7c2ac782ce4c3c9
# Parent ae085e5cd8a2cdb161eb5bf54ba9019c3d873506
Update json11 to v1.0.0 release
This will fix Issue #373 (and maybe some other issues too).
diff --git a/source/dynamicHDR10/json11/json11.cpp
b/source/dynamicHDR10/json11/json11.cpp
--- a/source/dynamicHDR10/json11/json11.cpp
+++ b/source/dynamicHDR10/json11/json11.cpp
@@ -21,38 +21,41 @@
#include "json11.h"
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <limits>
-#if _MSC_VER
-#pragma warning(disable: 4510) //const member cannot be default initialized
-#pragma warning(disable: 4512) //assignment operator could not be generated
-#pragma warning(disable: 4610) //const member cannot be default initialized
-#endif
-
namespace json11 {
static const int max_depth = 200;
using std::string;
using std::vector;
using std::map;
using std::make_shared;
using std::initializer_list;
using std::move;
+/* Helper for representing null - just a do-nothing struct, plus comparison
+ * operators so the helpers in JsonValue work. We can't use nullptr_t
because
+ * it may not be orderable.
+ */
+struct NullStruct {
+ bool operator==(NullStruct) const { return true; }
+ bool operator<(NullStruct) const { return false; }
+};
+
/* * * * * * * * * * * * * * * * * * * *
* Serialization
*/
-static void dump(std::nullptr_t, string &out) {
+static void dump(NullStruct, string &out) {
out += "null";
}
static void dump(double value, string &out) {
if (std::isfinite(value)) {
char buf[32];
snprintf(buf, sizeof buf, "%.17g", value);
out += buf;
@@ -209,19 +212,19 @@ public:
class JsonObject final : public Value<Json::OBJECT, Json::object> {
const Json::object &object_items() const override { return m_value; }
const Json & operator[](const string &key) const override;
public:
explicit JsonObject(const Json::object &value) : Value(value) {}
explicit JsonObject(Json::object &&value) : Value(move(value)) {}
};
-class JsonNull final : public Value<Json::NUL, std::nullptr_t> {
+class JsonNull final : public Value<Json::NUL, NullStruct> {
public:
- JsonNull() : Value(nullptr) {}
+ JsonNull() : Value({}) {}
};
/* * * * * * * * * * * * * * * * * * * *
* Static globals - static-init-safe
*/
struct Statics {
const std::shared_ptr<JsonValue> null = make_shared<JsonNull>();
const std::shared_ptr<JsonValue> t = make_shared<JsonBoolean>(true);
@@ -292,23 +295,27 @@ const Json & JsonArray::operator[] (size
else return m_value[i];
}
/* * * * * * * * * * * * * * * * * * * *
* Comparison
*/
bool Json::operator== (const Json &other) const {
+ if (m_ptr == other.m_ptr)
+ return true;
if (m_ptr->type() != other.m_ptr->type())
return false;
return m_ptr->equals(other.m_ptr.get());
}
bool Json::operator< (const Json &other) const {
+ if (m_ptr == other.m_ptr)
+ return false;
if (m_ptr->type() != other.m_ptr->type())
return m_ptr->type() < other.m_ptr->type();
return m_ptr->less(other.m_ptr.get());
}
/* * * * * * * * * * * * * * * * * * * *
* Parsing
@@ -376,44 +383,37 @@ struct JsonParser final {
*
* Advance comments (c-style inline and multiline).
*/
bool consume_comment() {
bool comment_found = false;
if (str[i] == '/') {
i++;
if (i == str.size())
- return fail("unexpected end of input inside comment", false);
+ return fail("unexpected end of input after start of comment",
false);
if (str[i] == '/') { // inline comment
i++;
- if (i == str.size())
- return fail("unexpected end of input inside inline
comment", false);
- // advance until next line
- while (str[i] != '\n') {
+ // advance until next line, or end of input
+ while (i < str.size() && str[i] != '\n') {
i++;
- if (i == str.size())
- return fail("unexpected end of input inside inline
comment", false);
}
comment_found = true;
}
else if (str[i] == '*') { // multiline comment
i++;
if (i > str.size()-2)
return fail("unexpected end of input inside multi-line
comment", false);
// advance until closing tokens
while (!(str[i] == '*' && str[i+1] == '/')) {
i++;
if (i > str.size()-2)
return fail(
"unexpected end of input inside multi-line comment",
false);
}
i += 2;
- if (i == str.size())
- return fail(
- "unexpected end of input inside multi-line comment", false);
comment_found = true;
}
else
return fail("malformed comment", false);
}
return comment_found;
}
@@ -422,31 +422,33 @@ struct JsonParser final {
* Advance until the current character is non-whitespace and
non-comment.
*/
void consume_garbage() {
consume_whitespace();
if(strategy == JsonParse::COMMENTS) {
bool comment_found = false;
do {
comment_found = consume_comment();
+ if (failed) return;
consume_whitespace();
}
while(comment_found);
}
}
/* get_next_token()
*
* Return the next non-whitespace character. If the end of the
input is reached,
* flag an error and return 0.
*/
char get_next_token() {
consume_garbage();
+ if (failed) return (char)0;
if (i == str.size())
- return fail("unexpected end of input", '0');
+ return fail("unexpected end of input", (char)0);
return str[i++];
}
/* encode_utf8(pt, out)
*
* Encode pt as UTF-8 and add it to out.
*/
@@ -473,17 +475,17 @@ struct JsonParser final {
/* parse_string()
*
* Parse a string, starting at the current position.
*/
string parse_string() {
string out;
long last_escaped_codepoint = -1;
- for (;;) {
+ while (true) {
if (i == str.size())
return fail("unexpected end of input in string", "");
char ch = str[i++];
if (ch == '"') {
encode_utf8(last_escaped_codepoint, out);
return out;
@@ -510,17 +512,17 @@ struct JsonParser final {
// Extract 4-byte escape sequence
string esc = str.substr(i, 4);
// Explicitly check length of the substring. The
following loop
// relies on std::string returning the terminating NUL when
// accessing str[length]. Checking here reduces
brittleness.
if (esc.length() < 4) {
return fail("bad \\u escape: " + esc, "");
}
- for (int j = 0; j < 4; j++) {
+ for (size_t j = 0; j < 4; j++) {
if (!in_range(esc[j], 'a', 'f') &&
!in_range(esc[j], 'A', 'F')
&& !in_range(esc[j], '0', '9'))
return fail("bad \\u escape: " + esc, "");
}
long codepoint = strtol(esc.data(), nullptr, 16);
// JSON specifies that characters outside the BMP shall
be encoded as a pair
@@ -666,17 +668,17 @@ struct JsonParser final {
return parse_string();
if (ch == '{') {
map<string, Json> data;
ch = get_next_token();
if (ch == '}')
return data;
- for (;;) {
+ while (1) {
if (ch != '"')
return fail("expected '\"' in object, got " + esc(ch));
string key = parse_string();
if (failed)
return Json();
ch = get_next_token();
@@ -699,17 +701,17 @@ struct JsonParser final {
}
if (ch == '[') {
vector<Json> data;
ch = get_next_token();
if (ch == ']')
return data;
- for (;;) {
+ while (1) {
i--;
data.push_back(parse_json(depth + 1));
if (failed)
return Json();
ch = get_next_token();
if (ch == ']')
break;
@@ -728,36 +730,42 @@ struct JsonParser final {
}//namespace {
Json Json::parse(const string &in, string &err, JsonParse strategy) {
JsonParser parser { in, 0, err, false, strategy };
Json result = parser.parse_json(0);
// Check for any trailing garbage
parser.consume_garbage();
+ if (parser.failed)
+ return Json();
if (parser.i != in.size())
return parser.fail("unexpected trailing " + esc(in[parser.i]));
return result;
}
// Documented in json11.hpp
vector<Json> Json::parse_multi(const string &in,
std::string::size_type &parser_stop_pos,
string &err,
JsonParse strategy) {
JsonParser parser { in, 0, err, false, strategy };
parser_stop_pos = 0;
vector<Json> json_vec;
while (parser.i != in.size() && !parser.failed) {
json_vec.push_back(parser.parse_json(0));
+ if (parser.failed)
+ break;
+
// Check for another object
parser.consume_garbage();
- if (!parser.failed)
- parser_stop_pos = parser.i;
+ if (parser.failed)
+ break;
+ parser_stop_pos = parser.i;
}
return json_vec;
}
/* * * * * * * * * * * * * * * * * * * *
* Shape-checking
*/
@@ -771,16 +779,10 @@ bool Json::has_shape(const shape & types
if ((*this)[item.first].type() != item.second) {
err = "bad type for " + item.first + " in " + dump();
return false;
}
}
return true;
}
-//void Json::add(std::string &key, std::string value)
-//{
-// (*m_ptr)[key] = value;
-//}
} // namespace json11
-
-
diff --git a/source/dynamicHDR10/json11/json11.h
b/source/dynamicHDR10/json11/json11.h
--- a/source/dynamicHDR10/json11/json11.h
+++ b/source/dynamicHDR10/json11/json11.h
@@ -71,17 +71,17 @@
namespace json11 {
enum JsonParse {
STANDARD, COMMENTS
};
class JsonValue;
-class Json final{ //final
+class Json final {
public:
// Types
enum Type {
NUL, NUMBER, BOOL, STRING, ARRAY, OBJECT
};
// Array and object typedefs
typedef std::vector<Json> array;
@@ -95,32 +95,31 @@ public:
Json(bool value); // BOOL
Json(const std::string &value); // STRING
Json(std::string &&value); // STRING
Json(const char * value); // STRING
Json(const array &values); // ARRAY
Json(array &&values); // ARRAY
Json(const object &values); // OBJECT
Json(object &&values); // OBJECT
- void add(std::string &key, std::string value);
// Implicit constructor: anything with a to_json() function.
template <class T, class = decltype(&T::to_json)>
Json(const T & t) : Json(t.to_json()) {}
// Implicit constructor: map-like objects (std::map,
std::unordered_map, etc)
template <class M, typename std::enable_if<
- std::is_constructible<std::string, typename M::key_type>::value
- && std::is_constructible<Json, typename M::mapped_type>::value,
+ std::is_constructible<std::string,
decltype(std::declval<M>().begin()->first)>::value
+ && std::is_constructible<Json,
decltype(std::declval<M>().begin()->second)>::value,
int>::type = 0>
Json(const M & m) : Json(object(m.begin(), m.end())) {}
// Implicit constructor: vector-like objects (std::list,
std::vector, std::set, etc)
template <class V, typename std::enable_if<
- std::is_constructible<Json, typename V::value_type>::value,
+ std::is_constructible<Json,
decltype(*std::declval<V>().begin())>::value,
int>::type = 0>
Json(const V & v) : Json(array(v.begin(), v.end())) {}
// This prevents Json(some_pointer) from accidentally producing a
bool. Use
// Json(bool(some_pointer)) if that behavior is desired.
Json(void *) = delete;
// Accessors
@@ -128,17 +127,16 @@ public:
bool is_null() const { return type() == NUL; }
bool is_number() const { return type() == NUMBER; }
bool is_bool() const { return type() == BOOL; }
bool is_string() const { return type() == STRING; }
bool is_array() const { return type() == ARRAY; }
bool is_object() const { return type() == OBJECT; }
-
// Return the enclosed value if this is a number, 0 otherwise. Note
that json11 does not
// distinguish between integer and non-integer numbers -
number_value() and int_value()
// can both be applied to a NUMBER-typed object.
double number_value() const;
int int_value() const;
// Return the enclosed value if this is a boolean, false otherwise.
bool bool_value() const;
@@ -224,12 +222,11 @@ protected:
virtual int int_value() const;
virtual bool bool_value() const;
virtual const std::string &string_value() const;
virtual const Json::array &array_items() const;
virtual const Json &operator[](size_t i) const;
virtual const Json::object &object_items() const;
virtual const Json &operator[](const std::string &key) const;
virtual ~JsonValue() {}
-
};
} // namespace json11
More information about the x265-devel
mailing list