🔨 deprecated j << istream / j >> ostream functions #367

The implementation is non-standard. Deprecation allows a simpler API in
the future without removing any features.
This commit is contained in:
Niels Lohmann 2017-03-29 00:39:47 +02:00
parent b4dbebffcd
commit c2e80a72d7
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
6 changed files with 110 additions and 69 deletions

View file

@ -49,6 +49,7 @@ doctest:
# -Wno-documentation-unknown-command: code uses user-defined commands like @complexity # -Wno-documentation-unknown-command: code uses user-defined commands like @complexity
# -Wno-exit-time-destructors: warning in Catch code # -Wno-exit-time-destructors: warning in Catch code
# -Wno-keyword-macro: unit-tests use "#define private public" # -Wno-keyword-macro: unit-tests use "#define private public"
# -Wno-deprecated-declarations: the library deprecated some functions
# -Wno-weak-vtables: exception class is defined inline, but has virtual method # -Wno-weak-vtables: exception class is defined inline, but has virtual method
# -Wno-range-loop-analysis: iterator_wrapper tests tests "for(const auto i...)" # -Wno-range-loop-analysis: iterator_wrapper tests tests "for(const auto i...)"
pedantic_clang: pedantic_clang:
@ -59,6 +60,7 @@ pedantic_clang:
-Wno-documentation-unknown-command \ -Wno-documentation-unknown-command \
-Wno-exit-time-destructors \ -Wno-exit-time-destructors \
-Wno-keyword-macro \ -Wno-keyword-macro \
-Wno-deprecated-declarations \
-Wno-weak-vtables \ -Wno-weak-vtables \
-Wno-range-loop-analysis" -Wno-range-loop-analysis"
@ -66,6 +68,7 @@ pedantic_clang:
pedantic_gcc: pedantic_gcc:
$(MAKE) json_unit CXX=g++ CXXFLAGS="\ $(MAKE) json_unit CXX=g++ CXXFLAGS="\
-std=c++11 \ -std=c++11 \
-Wno-deprecated-declarations \
-Werror \ -Werror \
-Wall -Wpedantic -Wextra \ -Wall -Wpedantic -Wextra \
-Walloca \ -Walloca \

View file

@ -78,6 +78,15 @@ SOFTWARE.
#pragma GCC diagnostic ignored "-Wdocumentation" #pragma GCC diagnostic ignored "-Wdocumentation"
#endif #endif
// allow for portable deprecation warnings
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
#define JSON_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define JSON_DEPRECATED __declspec(deprecated)
#else
#define JSON_DEPRECATED
#endif
// allow to disable exceptions // allow to disable exceptions
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION) #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION)
#define JSON_THROW(exception) throw exception #define JSON_THROW(exception) throw exception
@ -217,8 +226,8 @@ class parse_error : public exception
const size_t byte; const size_t byte;
private: private:
parse_error(int id, size_t byte_, const char* what_arg) parse_error(int id_, size_t byte_, const char* what_arg)
: exception(id, what_arg), byte(byte_) : exception(id_, what_arg), byte(byte_)
{} {}
}; };
@ -256,8 +265,8 @@ class invalid_iterator : public exception
} }
private: private:
invalid_iterator(int id, const char* what_arg) invalid_iterator(int id_, const char* what_arg)
: exception(id, what_arg) : exception(id_, what_arg)
{} {}
}; };
@ -295,8 +304,8 @@ class type_error : public exception
} }
private: private:
type_error(int id, const char* what_arg) type_error(int id_, const char* what_arg)
: exception(id, what_arg) : exception(id_, what_arg)
{} {}
}; };
@ -326,8 +335,8 @@ class out_of_range : public exception
} }
private: private:
out_of_range(int id, const char* what_arg) out_of_range(int id_, const char* what_arg)
: exception(id, what_arg) : exception(id_, what_arg)
{} {}
}; };
@ -352,8 +361,8 @@ class other_error : public exception
} }
private: private:
other_error(int id, const char* what_arg) other_error(int id_, const char* what_arg)
: exception(id, what_arg) : exception(id_, what_arg)
{} {}
}; };
@ -7083,8 +7092,12 @@ class basic_json
/*! /*!
@brief serialize to stream @brief serialize to stream
@copydoc operator<<(std::ostream&, const basic_json&) @deprecated This stream operator is deprecated and will be removed in a
future version of the library. Please use
@ref std::ostream& operator<<(std::ostream&, const basic_json&)
instead; that is, replace calls like `j >> o;` with `o << j;`.
*/ */
JSON_DEPRECATED
friend std::ostream& operator>>(const basic_json& j, std::ostream& o) friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
{ {
return o << j; return o << j;
@ -7357,6 +7370,20 @@ class basic_json
return parse(std::begin(c), std::end(c), cb); return parse(std::begin(c), std::end(c), cb);
} }
/*!
@brief deserialize from stream
@deprecated This stream operator is deprecated and will be removed in a
future version of the library. Please use
@ref std::istream& operator>>(std::istream&, basic_json&)
instead; that is, replace calls like `j << i;` with `i >> j;`.
*/
JSON_DEPRECATED
friend std::istream& operator<<(basic_json& j, std::istream& i)
{
j = parser(i).parse();
return i;
}
/*! /*!
@brief deserialize from stream @brief deserialize from stream
@ -7383,16 +7410,6 @@ class basic_json
@since version 1.0.0 @since version 1.0.0
*/ */
friend std::istream& operator<<(basic_json& j, std::istream& i)
{
j = parser(i).parse();
return i;
}
/*!
@brief deserialize from stream
@copydoc operator<<(basic_json&, std::istream&)
*/
friend std::istream& operator>>(std::istream& i, basic_json& j) friend std::istream& operator>>(std::istream& i, basic_json& j)
{ {
j = parser(i).parse(); j = parser(i).parse();
@ -8574,7 +8591,7 @@ class basic_json
case 0x7f: // UTF-8 string (indefinite length) case 0x7f: // UTF-8 string (indefinite length)
{ {
std::string result; std::string result;
while (check_length(v.size(), 1, idx), v[idx] != 0xff) while (static_cast<void>(check_length(v.size(), 1, idx)), v[idx] != 0xff)
{ {
string_t s = from_cbor_internal(v, idx); string_t s = from_cbor_internal(v, idx);
result += s; result += s;
@ -8670,7 +8687,7 @@ class basic_json
case 0x9f: // array (indefinite length) case 0x9f: // array (indefinite length)
{ {
basic_json result = value_t::array; basic_json result = value_t::array;
while (check_length(v.size(), 1, idx), v[idx] != 0xff) while (static_cast<void>(check_length(v.size(), 1, idx)), v[idx] != 0xff)
{ {
result.push_back(from_cbor_internal(v, idx)); result.push_back(from_cbor_internal(v, idx));
} }
@ -8775,7 +8792,7 @@ class basic_json
case 0xbf: // map (indefinite length) case 0xbf: // map (indefinite length)
{ {
basic_json result = value_t::object; basic_json result = value_t::object;
while (check_length(v.size(), 1, idx), v[idx] != 0xff) while (static_cast<void>(check_length(v.size(), 1, idx)), v[idx] != 0xff)
{ {
cbor_expect_string(v, idx); cbor_expect_string(v, idx);
std::string key = from_cbor_internal(v, idx); std::string key = from_cbor_internal(v, idx);
@ -13874,5 +13891,6 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std
#undef JSON_CATCH #undef JSON_CATCH
#undef JSON_THROW #undef JSON_THROW
#undef JSON_TRY #undef JSON_TRY
#undef JSON_DEPRECATED
#endif #endif

View file

@ -78,6 +78,15 @@ SOFTWARE.
#pragma GCC diagnostic ignored "-Wdocumentation" #pragma GCC diagnostic ignored "-Wdocumentation"
#endif #endif
// allow for portable deprecation warnings
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
#define JSON_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define JSON_DEPRECATED __declspec(deprecated)
#else
#define JSON_DEPRECATED
#endif
// allow to disable exceptions // allow to disable exceptions
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION) #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION)
#define JSON_THROW(exception) throw exception #define JSON_THROW(exception) throw exception
@ -217,8 +226,8 @@ class parse_error : public exception
const size_t byte; const size_t byte;
private: private:
parse_error(int id, size_t byte_, const char* what_arg) parse_error(int id_, size_t byte_, const char* what_arg)
: exception(id, what_arg), byte(byte_) : exception(id_, what_arg), byte(byte_)
{} {}
}; };
@ -256,8 +265,8 @@ class invalid_iterator : public exception
} }
private: private:
invalid_iterator(int id, const char* what_arg) invalid_iterator(int id_, const char* what_arg)
: exception(id, what_arg) : exception(id_, what_arg)
{} {}
}; };
@ -295,8 +304,8 @@ class type_error : public exception
} }
private: private:
type_error(int id, const char* what_arg) type_error(int id_, const char* what_arg)
: exception(id, what_arg) : exception(id_, what_arg)
{} {}
}; };
@ -326,8 +335,8 @@ class out_of_range : public exception
} }
private: private:
out_of_range(int id, const char* what_arg) out_of_range(int id_, const char* what_arg)
: exception(id, what_arg) : exception(id_, what_arg)
{} {}
}; };
@ -352,8 +361,8 @@ class other_error : public exception
} }
private: private:
other_error(int id, const char* what_arg) other_error(int id_, const char* what_arg)
: exception(id, what_arg) : exception(id_, what_arg)
{} {}
}; };
@ -7083,8 +7092,12 @@ class basic_json
/*! /*!
@brief serialize to stream @brief serialize to stream
@copydoc operator<<(std::ostream&, const basic_json&) @deprecated This stream operator is deprecated and will be removed in a
future version of the library. Please use
@ref std::ostream& operator<<(std::ostream&, const basic_json&)
instead; that is, replace calls like `j >> o;` with `o << j;`.
*/ */
JSON_DEPRECATED
friend std::ostream& operator>>(const basic_json& j, std::ostream& o) friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
{ {
return o << j; return o << j;
@ -7357,6 +7370,20 @@ class basic_json
return parse(std::begin(c), std::end(c), cb); return parse(std::begin(c), std::end(c), cb);
} }
/*!
@brief deserialize from stream
@deprecated This stream operator is deprecated and will be removed in a
future version of the library. Please use
@ref std::istream& operator>>(std::istream&, basic_json&)
instead; that is, replace calls like `j << i;` with `i >> j;`.
*/
JSON_DEPRECATED
friend std::istream& operator<<(basic_json& j, std::istream& i)
{
j = parser(i).parse();
return i;
}
/*! /*!
@brief deserialize from stream @brief deserialize from stream
@ -7383,16 +7410,6 @@ class basic_json
@since version 1.0.0 @since version 1.0.0
*/ */
friend std::istream& operator<<(basic_json& j, std::istream& i)
{
j = parser(i).parse();
return i;
}
/*!
@brief deserialize from stream
@copydoc operator<<(basic_json&, std::istream&)
*/
friend std::istream& operator>>(std::istream& i, basic_json& j) friend std::istream& operator>>(std::istream& i, basic_json& j)
{ {
j = parser(i).parse(); j = parser(i).parse();
@ -8574,7 +8591,7 @@ class basic_json
case 0x7f: // UTF-8 string (indefinite length) case 0x7f: // UTF-8 string (indefinite length)
{ {
std::string result; std::string result;
while (check_length(v.size(), 1, idx), v[idx] != 0xff) while (static_cast<void>(check_length(v.size(), 1, idx)), v[idx] != 0xff)
{ {
string_t s = from_cbor_internal(v, idx); string_t s = from_cbor_internal(v, idx);
result += s; result += s;
@ -8670,7 +8687,7 @@ class basic_json
case 0x9f: // array (indefinite length) case 0x9f: // array (indefinite length)
{ {
basic_json result = value_t::array; basic_json result = value_t::array;
while (check_length(v.size(), 1, idx), v[idx] != 0xff) while (static_cast<void>(check_length(v.size(), 1, idx)), v[idx] != 0xff)
{ {
result.push_back(from_cbor_internal(v, idx)); result.push_back(from_cbor_internal(v, idx));
} }
@ -8775,7 +8792,7 @@ class basic_json
case 0xbf: // map (indefinite length) case 0xbf: // map (indefinite length)
{ {
basic_json result = value_t::object; basic_json result = value_t::object;
while (check_length(v.size(), 1, idx), v[idx] != 0xff) while (static_cast<void>(check_length(v.size(), 1, idx)), v[idx] != 0xff)
{ {
cbor_expect_string(v, idx); cbor_expect_string(v, idx);
std::string key = from_cbor_internal(v, idx); std::string key = from_cbor_internal(v, idx);
@ -12907,5 +12924,6 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std
#undef JSON_CATCH #undef JSON_CATCH
#undef JSON_THROW #undef JSON_THROW
#undef JSON_TRY #undef JSON_TRY
#undef JSON_DEPRECATED
#endif #endif

View file

@ -216,6 +216,8 @@ TEST_CASE("regression tests")
{ {
json a = {1, 2, 3}; json a = {1, 2, 3};
json::reverse_iterator rit = ++a.rbegin(); json::reverse_iterator rit = ++a.rbegin();
CHECK(*rit == json(2));
CHECK(rit.value() == json(2));
} }
{ {
json a = {1, 2, 3}; json a = {1, 2, 3};
@ -540,7 +542,7 @@ TEST_CASE("regression tests")
CAPTURE(filename); CAPTURE(filename);
json j; json j;
std::ifstream f(filename); std::ifstream f(filename);
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
} }
} }
@ -556,7 +558,7 @@ TEST_CASE("regression tests")
CAPTURE(filename); CAPTURE(filename);
json j; json j;
std::ifstream f(filename); std::ifstream f(filename);
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
} }
} }
@ -586,14 +588,14 @@ TEST_CASE("regression tests")
std::stringstream ss; std::stringstream ss;
json j; json j;
ss << "123"; ss << "123";
CHECK_NOTHROW(j << ss); CHECK_NOTHROW(ss >> j);
// see https://github.com/nlohmann/json/issues/367#issuecomment-262841893: // see https://github.com/nlohmann/json/issues/367#issuecomment-262841893:
// ss is not at EOF; this yielded an error before the fix // ss is not at EOF; this yielded an error before the fix
// (threw basic_string::append). No, it should just throw // (threw basic_string::append). No, it should just throw
// a parse error because of the EOF. // a parse error because of the EOF.
CHECK_THROWS_AS(j << ss, json::parse_error); CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(j << ss, CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: parse error - unexpected end of input"); "[json.exception.parse_error.101] parse error at 1: parse error - unexpected end of input");
} }

View file

@ -79,7 +79,7 @@ TEST_CASE("compliance tests from json.org")
CAPTURE(filename); CAPTURE(filename);
json j; json j;
std::ifstream f(filename); std::ifstream f(filename);
CHECK_THROWS_AS(j << f, json::parse_error); CHECK_THROWS_AS(f >> j, json::parse_error);
} }
} }
@ -95,7 +95,7 @@ TEST_CASE("compliance tests from json.org")
CAPTURE(filename); CAPTURE(filename);
json j; json j;
std::ifstream f(filename); std::ifstream f(filename);
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
} }
} }
} }
@ -319,7 +319,7 @@ TEST_CASE("test suite from json-test-suite")
// strings in a JSON array // strings in a JSON array
std::ifstream f("test/data/json_testsuite/sample.json"); std::ifstream f("test/data/json_testsuite/sample.json");
json j; json j;
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
// the array has 3 elements // the array has 3 elements
CHECK(j.size() == 3); CHECK(j.size() == 3);
@ -334,35 +334,35 @@ TEST_CASE("json.org examples")
{ {
std::ifstream f("test/data/json.org/1.json"); std::ifstream f("test/data/json.org/1.json");
json j; json j;
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
} }
SECTION("2.json") SECTION("2.json")
{ {
std::ifstream f("test/data/json.org/2.json"); std::ifstream f("test/data/json.org/2.json");
json j; json j;
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
} }
SECTION("3.json") SECTION("3.json")
{ {
std::ifstream f("test/data/json.org/3.json"); std::ifstream f("test/data/json.org/3.json");
json j; json j;
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
} }
SECTION("4.json") SECTION("4.json")
{ {
std::ifstream f("test/data/json.org/4.json"); std::ifstream f("test/data/json.org/4.json");
json j; json j;
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
} }
SECTION("5.json") SECTION("5.json")
{ {
std::ifstream f("test/data/json.org/5.json"); std::ifstream f("test/data/json.org/5.json");
json j; json j;
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
} }
} }
@ -545,7 +545,7 @@ TEST_CASE("nst's JSONTestSuite")
CAPTURE(filename); CAPTURE(filename);
std::ifstream f(filename); std::ifstream f(filename);
json j; json j;
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
} }
} }
@ -754,7 +754,7 @@ TEST_CASE("nst's JSONTestSuite")
CAPTURE(filename); CAPTURE(filename);
std::ifstream f(filename); std::ifstream f(filename);
json j; json j;
CHECK_THROWS_AS(j << f, json::parse_error); CHECK_THROWS_AS(f >> j, json::parse_error);
} }
} }
@ -777,7 +777,7 @@ TEST_CASE("nst's JSONTestSuite")
CAPTURE(filename); CAPTURE(filename);
std::ifstream f(filename); std::ifstream f(filename);
json j; json j;
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
} }
} }
@ -797,7 +797,7 @@ TEST_CASE("nst's JSONTestSuite")
CAPTURE(filename); CAPTURE(filename);
std::ifstream f(filename); std::ifstream f(filename);
json j; json j;
CHECK_THROWS_AS(j << f, json::out_of_range); CHECK_THROWS_AS(f >> j, json::out_of_range);
} }
} }
@ -824,7 +824,7 @@ TEST_CASE("nst's JSONTestSuite")
CAPTURE(filename); CAPTURE(filename);
std::ifstream f(filename); std::ifstream f(filename);
json j; json j;
CHECK_THROWS_AS(j << f, json::parse_error); CHECK_THROWS_AS(f >> j, json::parse_error);
} }
} }
} }
@ -851,7 +851,7 @@ TEST_CASE("Big List of Naughty Strings")
{ {
std::ifstream f("test/data/big-list-of-naughty-strings/blns.json"); std::ifstream f("test/data/big-list-of-naughty-strings/blns.json");
json j; json j;
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
} }
// check if parsed strings roundtrip // check if parsed strings roundtrip

View file

@ -125,7 +125,7 @@ TEST_CASE("Unicode", "[hide]")
// strings in a JSON array // strings in a JSON array
std::ifstream f("test/data/json_nlohmann_tests/all_unicode.json"); std::ifstream f("test/data/json_nlohmann_tests/all_unicode.json");
json j; json j;
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
// the array has 1112064 + 1 elemnts (a terminating "null" value) // the array has 1112064 + 1 elemnts (a terminating "null" value)
// Note: 1112064 = 0x1FFFFF code points - 2048 invalid values between // Note: 1112064 = 0x1FFFFF code points - 2048 invalid values between
@ -168,7 +168,7 @@ TEST_CASE("Unicode", "[hide]")
// read a file with a UTF-8 BOM // read a file with a UTF-8 BOM
std::ifstream f("test/data/json_nlohmann_tests/bom.json"); std::ifstream f("test/data/json_nlohmann_tests/bom.json");
json j; json j;
CHECK_NOTHROW(j << f); CHECK_NOTHROW(f >> j);
} }
SECTION("error for incomplete/wrong BOM") SECTION("error for incomplete/wrong BOM")