diff --git a/README.md b/README.md index 7147b44f..ef6a0c57 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,9 @@ All you need to do is add ```cpp #include "json.h" + +// for convenience +using json = nlohmann::json; ``` to the files you want to use JSON objects. Furthermore, you need to compile the file `json.cc` and link it to your binaries. Do not forget to set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang). diff --git a/benchmark/parse.cc b/benchmark/parse.cc index 4ad5c079..6cbf7596 100644 --- a/benchmark/parse.cc +++ b/benchmark/parse.cc @@ -2,7 +2,7 @@ int main() { - json j; + nlohmann::json j; j << std::cin; return 0; } diff --git a/src/json.cc b/src/json.cc index 0b86a148..31e65078 100644 --- a/src/json.cc +++ b/src/json.cc @@ -12,17 +12,12 @@ #include "json.h" #include // std::isdigit, std::isspace -#include // size_t +#include // std::size_t #include // std::runtime_error #include // std::swap, std::move - -//////////////////// -// STATIC MEMBERS // -//////////////////// - -std::mutex json::token_; - +namespace nlohmann +{ /////////////////////////////////// // CONSTRUCTORS OF UNION "value" // @@ -639,8 +634,6 @@ void json::push_back(const json& o) throw std::runtime_error("cannot add element to " + type_name()); } - std::lock_guard lg(token_); - // transform null object into an array if (type_ == value_type::null) { @@ -676,8 +669,6 @@ void json::push_back(json&& o) throw std::runtime_error("cannot add element to " + type_name()); } - std::lock_guard lg(token_); - // transform null object into an array if (type_ == value_type::null) { @@ -804,10 +795,8 @@ json& json::operator[](const int index) std::to_string(index) + " to " + type_name()); } - std::lock_guard lg(token_); - // return reference to element from array at given index - return (*value_.array)[static_cast(index)]; + return (*value_.array)[static_cast(index)]; } /*! @@ -836,7 +825,7 @@ const json& json::operator[](const int index) const } // return element from array at given index - return (*value_.array)[static_cast(index)]; + return (*value_.array)[static_cast(index)]; } /*! @@ -865,10 +854,8 @@ json& json::at(const int index) std::to_string(index) + " to " + type_name()); } - std::lock_guard lg(token_); - // return reference to element from array at given index - return value_.array->at(static_cast(index)); + return value_.array->at(static_cast(index)); } /*! @@ -898,7 +885,7 @@ const json& json::at(const int index) const } // return element from array at given index - return value_.array->at(static_cast(index)); + return value_.array->at(static_cast(index)); } /*! @@ -924,8 +911,6 @@ key. */ json& json::operator[](const char* key) { - std::lock_guard lg(token_); - // implicitly convert null to object if (type_ == value_type::null) { @@ -1006,8 +991,6 @@ key. */ json& json::at(const char* key) { - std::lock_guard lg(token_); - // this function operator only works for objects if (type_ != value_type::object) { @@ -1061,7 +1044,7 @@ Returns the size of the JSON object. @invariant The size is reported as 0 if and only if empty() would return true. */ -size_t json::size() const noexcept +std::size_t json::size() const noexcept { switch (type_) { @@ -2105,6 +2088,8 @@ void json::parser::expect(const char c) } } +} + /*! This operator implements a user-defined string literal for JSON objects. It can be used by adding \p "_json" to a string literal and returns a JSON object if @@ -2113,7 +2098,7 @@ no parse error occurred. @param s a string representation of a JSON object @return a JSON object */ -json operator "" _json(const char* s, size_t) +nlohmann::json operator "" _json(const char* s, std::size_t) { - return json::parse(s); + return nlohmann::json::parse(s); } diff --git a/src/json.h b/src/json.h index 688d805b..cc7de071 100644 --- a/src/json.h +++ b/src/json.h @@ -14,10 +14,12 @@ #include // std::initializer_list #include // std::istream, std::ostream #include // std::map -#include // std::mutex #include // std::string #include // std::vector +namespace nlohmann +{ + /*! @brief JSON for Modern C++ @@ -274,7 +276,7 @@ class json const json& at(const char*) const; /// return the number of stored values - size_t size() const noexcept; + std::size_t size() const noexcept; /// checks whether object is empty bool empty() const noexcept; /// removes all elements from compounds and resets values to default @@ -317,10 +319,6 @@ class json /// the payload value value_ {}; - private: - /// mutex to guard payload - static std::mutex token_; - public: /// an iterator class iterator @@ -431,9 +429,11 @@ class json /// the current character char current_ {}; /// the position inside the input buffer - size_t pos_ = 0; + std::size_t pos_ = 0; }; }; +} + /// user-defined literal operator to create JSON objects from strings -json operator "" _json(const char*, size_t); +nlohmann::json operator "" _json(const char*, std::size_t); diff --git a/test/json_unit.cc b/test/json_unit.cc index 05ba4c5f..3282b71a 100644 --- a/test/json_unit.cc +++ b/test/json_unit.cc @@ -3,6 +3,8 @@ #include "json.h" +using json = nlohmann::json; + TEST_CASE("array") { SECTION("Basics")