diff --git a/src/json.hpp b/src/json.hpp index dfa7e97b..5d3808f7 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -1,3 +1,12 @@ +/*! +@file +@copyright The code is licensed under the MIT License + , + Copyright (c) 2013-2015 Niels Lohmann. +@author Niels Lohmann +@see https://github.com/nlohmann/json +*/ + #ifndef _NLOHMANN_JSON #define _NLOHMANN_JSON @@ -1026,14 +1035,12 @@ class basic_json m_value.object->insert(value); } - /* /// add an object to an object inline reference operator+=(const typename object_t::value_type& value) { push_back(value); return operator[](value.first); } - */ /// swaps the contents inline void swap(reference other) noexcept diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 24a9d45c..c6c076ae 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -1,3 +1,12 @@ +/*! +@file +@copyright The code is licensed under the MIT License + , + Copyright (c) 2013-2015 Niels Lohmann. +@author Niels Lohmann +@see https://github.com/nlohmann/json +*/ + #ifndef _NLOHMANN_JSON #define _NLOHMANN_JSON @@ -1026,14 +1035,12 @@ class basic_json m_value.object->insert(value); } - /* /// add an object to an object inline reference operator+=(const typename object_t::value_type& value) { push_back(value); return operator[](value.first); } - */ /// swaps the contents inline void swap(reference other) noexcept diff --git a/test/unit.cpp b/test/unit.cpp index d6942326..053f36a1 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -1,3 +1,12 @@ +/*! +@file +@copyright The code is licensed under the MIT License + , + Copyright (c) 2013-2015 Niels Lohmann. +@author Niels Lohmann +@see https://github.com/nlohmann/json +*/ + #define CATCH_CONFIG_MAIN #include "catch.hpp" @@ -3553,6 +3562,16 @@ TEST_CASE("modifiers") } } } + + SECTION("to object") + { + json j(json::value_t::object); + j.push_back(json::object_t::value_type({"one", 1})); + j.push_back(json::object_t::value_type({"two", 2})); + CHECK(j.size() == 2); + CHECK(j["one"] == json(1)); + CHECK(j["two"] == json(2)); + } } SECTION("operator+=") @@ -3614,6 +3633,16 @@ TEST_CASE("modifiers") } } } + + SECTION("to object") + { + json j(json::value_t::object); + j += json::object_t::value_type({"one", 1}); + j += json::object_t::value_type({"two", 2}); + CHECK(j.size() == 2); + CHECK(j["one"] == json(1)); + CHECK(j["two"] == json(2)); + } } SECTION("swap()") @@ -5073,11 +5102,23 @@ TEST_CASE("parser class") CHECK(json::parser("0.999").parse() == json(0.999)); CHECK(json::parser("128.5").parse() == json(128.5)); } + + SECTION("with exponent") + { + CHECK(json::parser("-128.5E3").parse() == json(-128.5E3)); + CHECK(json::parser("-128.5E-3").parse() == json(-128.5E-3)); + } } SECTION("invalid numbers") { CHECK_THROWS_AS(json::parser("01").parse(), std::invalid_argument); + CHECK_THROWS_AS(json::parser("--1").parse(), std::invalid_argument); + CHECK_THROWS_AS(json::parser("1.").parse(), std::invalid_argument); + CHECK_THROWS_AS(json::parser("1E").parse(), std::invalid_argument); + CHECK_THROWS_AS(json::parser("1E-").parse(), std::invalid_argument); + CHECK_THROWS_AS(json::parser("1.E1").parse(), std::invalid_argument); + CHECK_THROWS_AS(json::parser("-1E").parse(), std::invalid_argument); } } } @@ -5119,8 +5160,11 @@ TEST_CASE("parser class") CHECK_THROWS_AS(json::parser("fals").parse(), std::invalid_argument); // unexpected end of array + CHECK_THROWS_AS(json::parser("[").parse(), std::invalid_argument); + CHECK_THROWS_AS(json::parser("[1").parse(), std::invalid_argument); CHECK_THROWS_AS(json::parser("[1,").parse(), std::invalid_argument); CHECK_THROWS_AS(json::parser("[1,]").parse(), std::invalid_argument); + CHECK_THROWS_AS(json::parser("]").parse(), std::invalid_argument); // unexpected end of object CHECK_THROWS_AS(json::parser("{").parse(), std::invalid_argument); @@ -5128,6 +5172,7 @@ TEST_CASE("parser class") CHECK_THROWS_AS(json::parser("{\"foo\":").parse(), std::invalid_argument); CHECK_THROWS_AS(json::parser("{\"foo\":}").parse(), std::invalid_argument); CHECK_THROWS_AS(json::parser("{\"foo\":1,}").parse(), std::invalid_argument); + CHECK_THROWS_AS(json::parser("}").parse(), std::invalid_argument); // unexpected end of string CHECK_THROWS_AS(json::parser("\"").parse(), std::invalid_argument);