Skip to content

Commit

Permalink
🔨 deprecated j << istream / j >> ostream functions #367
Browse files Browse the repository at this point in the history
The implementation is non-standard. Deprecation allows a simpler API in
the future without removing any features.
  • Loading branch information
nlohmann committed Mar 28, 2017
1 parent b4dbebf commit c2e80a7
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 69 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ doctest:
# -Wno-documentation-unknown-command: code uses user-defined commands like @complexity
# -Wno-exit-time-destructors: warning in Catch code
# -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-range-loop-analysis: iterator_wrapper tests tests "for(const auto i...)"
pedantic_clang:
Expand All @@ -59,13 +60,15 @@ pedantic_clang:
-Wno-documentation-unknown-command \
-Wno-exit-time-destructors \
-Wno-keyword-macro \
-Wno-deprecated-declarations \
-Wno-weak-vtables \
-Wno-range-loop-analysis"

# calling GCC with most warnings
pedantic_gcc:
$(MAKE) json_unit CXX=g++ CXXFLAGS="\
-std=c++11 \
-Wno-deprecated-declarations \
-Werror \
-Wall -Wpedantic -Wextra \
-Walloca \
Expand Down
66 changes: 42 additions & 24 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ SOFTWARE.
#pragma GCC diagnostic ignored "-Wdocumentation"
#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
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION)
#define JSON_THROW(exception) throw exception
Expand Down Expand Up @@ -217,8 +226,8 @@ class parse_error : public exception
const size_t byte;

private:
parse_error(int id, size_t byte_, const char* what_arg)
: exception(id, what_arg), byte(byte_)
parse_error(int id_, size_t byte_, const char* what_arg)
: exception(id_, what_arg), byte(byte_)
{}
};

Expand Down Expand Up @@ -256,8 +265,8 @@ class invalid_iterator : public exception
}

private:
invalid_iterator(int id, const char* what_arg)
: exception(id, what_arg)
invalid_iterator(int id_, const char* what_arg)
: exception(id_, what_arg)
{}
};

Expand Down Expand Up @@ -295,8 +304,8 @@ class type_error : public exception
}

private:
type_error(int id, const char* what_arg)
: exception(id, what_arg)
type_error(int id_, const char* what_arg)
: exception(id_, what_arg)
{}
};

Expand Down Expand Up @@ -326,8 +335,8 @@ class out_of_range : public exception
}

private:
out_of_range(int id, const char* what_arg)
: exception(id, what_arg)
out_of_range(int id_, const char* what_arg)
: exception(id_, what_arg)
{}
};

Expand All @@ -352,8 +361,8 @@ class other_error : public exception
}

private:
other_error(int id, const char* what_arg)
: exception(id, what_arg)
other_error(int id_, const char* what_arg)
: exception(id_, what_arg)
{}
};

Expand Down Expand Up @@ -7083,8 +7092,12 @@ class basic_json

/*!
@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)
{
return o << j;
Expand Down Expand Up @@ -7357,6 +7370,20 @@ class basic_json
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
Expand All @@ -7383,16 +7410,6 @@ class basic_json
@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)
{
j = parser(i).parse();
Expand Down Expand Up @@ -8574,7 +8591,7 @@ class basic_json
case 0x7f: // UTF-8 string (indefinite length)
{
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);
result += s;
Expand Down Expand Up @@ -8670,7 +8687,7 @@ class basic_json
case 0x9f: // array (indefinite length)
{
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));
}
Expand Down Expand Up @@ -8775,7 +8792,7 @@ class basic_json
case 0xbf: // map (indefinite length)
{
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);
std::string key = from_cbor_internal(v, idx);
Expand Down Expand Up @@ -13874,5 +13891,6 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std
#undef JSON_CATCH
#undef JSON_THROW
#undef JSON_TRY
#undef JSON_DEPRECATED

#endif
66 changes: 42 additions & 24 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ SOFTWARE.
#pragma GCC diagnostic ignored "-Wdocumentation"
#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
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION)
#define JSON_THROW(exception) throw exception
Expand Down Expand Up @@ -217,8 +226,8 @@ class parse_error : public exception
const size_t byte;

private:
parse_error(int id, size_t byte_, const char* what_arg)
: exception(id, what_arg), byte(byte_)
parse_error(int id_, size_t byte_, const char* what_arg)
: exception(id_, what_arg), byte(byte_)
{}
};

Expand Down Expand Up @@ -256,8 +265,8 @@ class invalid_iterator : public exception
}

private:
invalid_iterator(int id, const char* what_arg)
: exception(id, what_arg)
invalid_iterator(int id_, const char* what_arg)
: exception(id_, what_arg)
{}
};

Expand Down Expand Up @@ -295,8 +304,8 @@ class type_error : public exception
}

private:
type_error(int id, const char* what_arg)
: exception(id, what_arg)
type_error(int id_, const char* what_arg)
: exception(id_, what_arg)
{}
};

Expand Down Expand Up @@ -326,8 +335,8 @@ class out_of_range : public exception
}

private:
out_of_range(int id, const char* what_arg)
: exception(id, what_arg)
out_of_range(int id_, const char* what_arg)
: exception(id_, what_arg)
{}
};

Expand All @@ -352,8 +361,8 @@ class other_error : public exception
}

private:
other_error(int id, const char* what_arg)
: exception(id, what_arg)
other_error(int id_, const char* what_arg)
: exception(id_, what_arg)
{}
};

Expand Down Expand Up @@ -7083,8 +7092,12 @@ class basic_json

/*!
@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)
{
return o << j;
Expand Down Expand Up @@ -7357,6 +7370,20 @@ class basic_json
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

Expand All @@ -7383,16 +7410,6 @@ class basic_json

@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)
{
j = parser(i).parse();
Expand Down Expand Up @@ -8574,7 +8591,7 @@ class basic_json
case 0x7f: // UTF-8 string (indefinite length)
{
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);
result += s;
Expand Down Expand Up @@ -8670,7 +8687,7 @@ class basic_json
case 0x9f: // array (indefinite length)
{
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));
}
Expand Down Expand Up @@ -8775,7 +8792,7 @@ class basic_json
case 0xbf: // map (indefinite length)
{
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);
std::string key = from_cbor_internal(v, idx);
Expand Down Expand Up @@ -12907,5 +12924,6 @@ inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std
#undef JSON_CATCH
#undef JSON_THROW
#undef JSON_TRY
#undef JSON_DEPRECATED

#endif
12 changes: 7 additions & 5 deletions test/src/unit-regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ TEST_CASE("regression tests")
{
json a = {1, 2, 3};
json::reverse_iterator rit = ++a.rbegin();
CHECK(*rit == json(2));
CHECK(rit.value() == json(2));
}
{
json a = {1, 2, 3};
Expand Down Expand Up @@ -540,7 +542,7 @@ TEST_CASE("regression tests")
CAPTURE(filename);
json j;
std::ifstream f(filename);
CHECK_NOTHROW(j << f);
CHECK_NOTHROW(f >> j);
}
}

Expand All @@ -556,7 +558,7 @@ TEST_CASE("regression tests")
CAPTURE(filename);
json j;
std::ifstream f(filename);
CHECK_NOTHROW(j << f);
CHECK_NOTHROW(f >> j);
}
}

Expand Down Expand Up @@ -586,14 +588,14 @@ TEST_CASE("regression tests")
std::stringstream ss;
json j;
ss << "123";
CHECK_NOTHROW(j << ss);
CHECK_NOTHROW(ss >> j);

// see https://github.com/nlohmann/json/issues/367#issuecomment-262841893:
// ss is not at EOF; this yielded an error before the fix
// (threw basic_string::append). No, it should just throw
// a parse error because of the EOF.
CHECK_THROWS_AS(j << ss, json::parse_error);
CHECK_THROWS_WITH(j << ss,
CHECK_THROWS_AS(ss >> j, json::parse_error);
CHECK_THROWS_WITH(ss >> j,
"[json.exception.parse_error.101] parse error at 1: parse error - unexpected end of input");
}

Expand Down
Loading

0 comments on commit c2e80a7

Please sign in to comment.