added width feature / more test cases

This commit is contained in:
Niels 2015-02-10 19:50:26 +01:00
parent 4cd341d4db
commit 8c6bb04d10
3 changed files with 78 additions and 15 deletions

View file

@ -1264,14 +1264,20 @@ class basic_json
/// serialize to stream /// serialize to stream
friend std::ostream& operator<<(std::ostream& o, const basic_json& j) friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
{ {
o << j.dump(); // read width member and use it as indentation parameter if nonzero
const int indentation = (o.width() == 0) ? -1 : o.width();
o << j.dump(indentation);
return o; return o;
} }
/// serialize to stream /// serialize to stream
friend std::ostream& operator>>(const basic_json& j, std::ostream& o) friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
{ {
o << j.dump(); // read width member and use it as indentation parameter if nonzero
const int indentation = (o.width() == 0) ? -1 : o.width();
o << j.dump(indentation);
return o; return o;
} }
@ -1315,24 +1321,28 @@ class basic_json
{ {
return "null"; return "null";
} }
case (value_t::object): case (value_t::object):
{ {
return "object"; return "object";
} }
case (value_t::array): case (value_t::array):
{ {
return "array"; return "array";
} }
case (value_t::string): case (value_t::string):
{ {
return "string"; return "string";
} }
case (value_t::boolean): case (value_t::boolean):
{ {
return "boolean"; return "boolean";
} }
case (value_t::number_integer):
case (value_t::number_float): default:
{ {
return "number"; return "number";
} }

View file

@ -1264,14 +1264,20 @@ class basic_json
/// serialize to stream /// serialize to stream
friend std::ostream& operator<<(std::ostream& o, const basic_json& j) friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
{ {
o << j.dump(); // read width member and use it as indentation parameter if nonzero
const int indentation = (o.width() == 0) ? -1 : o.width();
o << j.dump(indentation);
return o; return o;
} }
/// serialize to stream /// serialize to stream
friend std::ostream& operator>>(const basic_json& j, std::ostream& o) friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
{ {
o << j.dump(); // read width member and use it as indentation parameter if nonzero
const int indentation = (o.width() == 0) ? -1 : o.width();
o << j.dump(indentation);
return o; return o;
} }

View file

@ -7,6 +7,7 @@ using nlohmann::json;
#include <array> #include <array>
#include <deque> #include <deque>
#include <forward_list> #include <forward_list>
#include <iomanip>
#include <list> #include <list>
#include <map> #include <map>
#include <set> #include <set>
@ -1045,10 +1046,12 @@ TEST_CASE("object inspection")
{ {
json j {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, {"null", nullptr}, {"string", "Hello world"} }; json j {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, {"null", nullptr}, {"string", "Hello world"} };
SECTION("no indent") SECTION("no indent / indent=-1")
{ {
CHECK(j.dump() == CHECK(j.dump() ==
"{\"array\":[1,2,3,4],\"boolean\":false,\"null\":null,\"number\":42,\"object\":{},\"string\":\"Hello world\"}"); "{\"array\":[1,2,3,4],\"boolean\":false,\"null\":null,\"number\":42,\"object\":{},\"string\":\"Hello world\"}");
CHECK(j.dump() == j.dump(-1));
} }
SECTION("indent=0") SECTION("indent=0")
@ -2152,12 +2155,25 @@ TEST_CASE("element access")
SECTION("access within bounds") SECTION("access within bounds")
{ {
CHECK(j["integer"] == json(1)); CHECK(j["integer"] == json(1));
CHECK(j[json::object_t::key_type("integer")] == j["integer"]);
CHECK(j["boolean"] == json(true)); CHECK(j["boolean"] == json(true));
CHECK(j[json::object_t::key_type("boolean")] == j["boolean"]);
CHECK(j["null"] == json(nullptr)); CHECK(j["null"] == json(nullptr));
CHECK(j[json::object_t::key_type("null")] == j["null"]);
CHECK(j["string"] == json("hello world")); CHECK(j["string"] == json("hello world"));
CHECK(j[json::object_t::key_type("string")] == j["string"]);
CHECK(j["floating"] == json(42.23)); CHECK(j["floating"] == json(42.23));
CHECK(j[json::object_t::key_type("floating")] == j["floating"]);
CHECK(j["object"] == json(json::object())); CHECK(j["object"] == json(json::object()));
CHECK(j[json::object_t::key_type("object")] == j["object"]);
CHECK(j["array"] == json({1, 2, 3})); CHECK(j["array"] == json({1, 2, 3}));
CHECK(j[json::object_t::key_type("array")] == j["array"]);
} }
SECTION("access on non-object type") SECTION("access on non-object type")
@ -2166,36 +2182,42 @@ TEST_CASE("element access")
{ {
json j_nonobject(json::value_t::null); json j_nonobject(json::value_t::null);
CHECK_THROWS_AS(j_nonobject["foo"], std::runtime_error); CHECK_THROWS_AS(j_nonobject["foo"], std::runtime_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], std::runtime_error);
} }
SECTION("boolean") SECTION("boolean")
{ {
json j_nonobject(json::value_t::boolean); json j_nonobject(json::value_t::boolean);
CHECK_THROWS_AS(j_nonobject["foo"], std::runtime_error); CHECK_THROWS_AS(j_nonobject["foo"], std::runtime_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], std::runtime_error);
} }
SECTION("string") SECTION("string")
{ {
json j_nonobject(json::value_t::string); json j_nonobject(json::value_t::string);
CHECK_THROWS_AS(j_nonobject["foo"], std::runtime_error); CHECK_THROWS_AS(j_nonobject["foo"], std::runtime_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], std::runtime_error);
} }
SECTION("array") SECTION("array")
{ {
json j_nonobject(json::value_t::array); json j_nonobject(json::value_t::array);
CHECK_THROWS_AS(j_nonobject["foo"], std::runtime_error); CHECK_THROWS_AS(j_nonobject["foo"], std::runtime_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], std::runtime_error);
} }
SECTION("number (integer)") SECTION("number (integer)")
{ {
json j_nonobject(json::value_t::number_integer); json j_nonobject(json::value_t::number_integer);
CHECK_THROWS_AS(j_nonobject["foo"], std::runtime_error); CHECK_THROWS_AS(j_nonobject["foo"], std::runtime_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], std::runtime_error);
} }
SECTION("number (floating-point)") SECTION("number (floating-point)")
{ {
json j_nonobject(json::value_t::number_float); json j_nonobject(json::value_t::number_float);
CHECK_THROWS_AS(j_nonobject["foo"], std::runtime_error); CHECK_THROWS_AS(j_nonobject["foo"], std::runtime_error);
CHECK_THROWS_AS(j_nonobject[json::object_t::key_type("foo")], std::runtime_error);
} }
} }
} }
@ -3826,18 +3848,43 @@ TEST_CASE("serialization")
{ {
SECTION("operator<<") SECTION("operator<<")
{ {
std::stringstream ss; SECTION("no given width")
json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; {
ss << j; std::stringstream ss;
CHECK(ss.str() == "[\"foo\",1,2,3,false,{\"one\":1}]"); json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
ss << j;
CHECK(ss.str() == "[\"foo\",1,2,3,false,{\"one\":1}]");
}
SECTION("given width")
{
std::stringstream ss;
json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
ss << std::setw(4) << j;
CHECK(ss.str() ==
"[\n \"foo\",\n 1,\n 2,\n 3,\n false,\n {\n \"one\": 1\n }\n]");
}
} }
SECTION("operator>>") SECTION("operator>>")
{ {
std::stringstream ss; SECTION("no given width")
json j = {"foo", 1, 2, 3, false, {{"one", 1}}}; {
j >> ss; std::stringstream ss;
CHECK(ss.str() == "[\"foo\",1,2,3,false,{\"one\":1}]"); json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
j >> ss;
CHECK(ss.str() == "[\"foo\",1,2,3,false,{\"one\":1}]");
}
SECTION("given width")
{
std::stringstream ss;
json j = {"foo", 1, 2, 3, false, {{"one", 1}}};
ss.width(4);
j >> ss;
CHECK(ss.str() ==
"[\n \"foo\",\n 1,\n 2,\n 3,\n false,\n {\n \"one\": 1\n }\n]");
}
} }
} }