moved class into "nlohmann" namespace

- fixed issue #9
- also removed std::mutex member variable
- also added “std::” prefix to size_t variables
This commit is contained in:
Niels 2015-01-06 18:41:24 +01:00
parent 4f0afbbe64
commit a53c878c81
5 changed files with 26 additions and 36 deletions

View file

@ -30,6 +30,9 @@ All you need to do is add
```cpp ```cpp
#include "json.h" #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). 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).

View file

@ -2,7 +2,7 @@
int main() int main()
{ {
json j; nlohmann::json j;
j << std::cin; j << std::cin;
return 0; return 0;
} }

View file

@ -12,17 +12,12 @@
#include "json.h" #include "json.h"
#include <cctype> // std::isdigit, std::isspace #include <cctype> // std::isdigit, std::isspace
#include <cstddef> // size_t #include <cstddef> // std::size_t
#include <stdexcept> // std::runtime_error #include <stdexcept> // std::runtime_error
#include <utility> // std::swap, std::move #include <utility> // std::swap, std::move
namespace nlohmann
//////////////////// {
// STATIC MEMBERS //
////////////////////
std::mutex json::token_;
/////////////////////////////////// ///////////////////////////////////
// CONSTRUCTORS OF UNION "value" // // 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()); throw std::runtime_error("cannot add element to " + type_name());
} }
std::lock_guard<std::mutex> lg(token_);
// transform null object into an array // transform null object into an array
if (type_ == value_type::null) 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()); throw std::runtime_error("cannot add element to " + type_name());
} }
std::lock_guard<std::mutex> lg(token_);
// transform null object into an array // transform null object into an array
if (type_ == value_type::null) if (type_ == value_type::null)
{ {
@ -804,10 +795,8 @@ json& json::operator[](const int index)
std::to_string(index) + " to " + type_name()); std::to_string(index) + " to " + type_name());
} }
std::lock_guard<std::mutex> lg(token_);
// return reference to element from array at given index // return reference to element from array at given index
return (*value_.array)[static_cast<size_t>(index)]; return (*value_.array)[static_cast<std::size_t>(index)];
} }
/*! /*!
@ -836,7 +825,7 @@ const json& json::operator[](const int index) const
} }
// return element from array at given index // return element from array at given index
return (*value_.array)[static_cast<size_t>(index)]; return (*value_.array)[static_cast<std::size_t>(index)];
} }
/*! /*!
@ -865,10 +854,8 @@ json& json::at(const int index)
std::to_string(index) + " to " + type_name()); std::to_string(index) + " to " + type_name());
} }
std::lock_guard<std::mutex> lg(token_);
// return reference to element from array at given index // return reference to element from array at given index
return value_.array->at(static_cast<size_t>(index)); return value_.array->at(static_cast<std::size_t>(index));
} }
/*! /*!
@ -898,7 +885,7 @@ const json& json::at(const int index) const
} }
// return element from array at given index // return element from array at given index
return value_.array->at(static_cast<size_t>(index)); return value_.array->at(static_cast<std::size_t>(index));
} }
/*! /*!
@ -924,8 +911,6 @@ key.
*/ */
json& json::operator[](const char* key) json& json::operator[](const char* key)
{ {
std::lock_guard<std::mutex> lg(token_);
// implicitly convert null to object // implicitly convert null to object
if (type_ == value_type::null) if (type_ == value_type::null)
{ {
@ -1006,8 +991,6 @@ key.
*/ */
json& json::at(const char* key) json& json::at(const char* key)
{ {
std::lock_guard<std::mutex> lg(token_);
// this function operator only works for objects // this function operator only works for objects
if (type_ != value_type::object) 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. @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_) 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 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 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 @param s a string representation of a JSON object
@return 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);
} }

View file

@ -14,10 +14,12 @@
#include <initializer_list> // std::initializer_list #include <initializer_list> // std::initializer_list
#include <iostream> // std::istream, std::ostream #include <iostream> // std::istream, std::ostream
#include <map> // std::map #include <map> // std::map
#include <mutex> // std::mutex
#include <string> // std::string #include <string> // std::string
#include <vector> // std::vector #include <vector> // std::vector
namespace nlohmann
{
/*! /*!
@brief JSON for Modern C++ @brief JSON for Modern C++
@ -274,7 +276,7 @@ class json
const json& at(const char*) const; const json& at(const char*) const;
/// return the number of stored values /// return the number of stored values
size_t size() const noexcept; std::size_t size() const noexcept;
/// checks whether object is empty /// checks whether object is empty
bool empty() const noexcept; bool empty() const noexcept;
/// removes all elements from compounds and resets values to default /// removes all elements from compounds and resets values to default
@ -317,10 +319,6 @@ class json
/// the payload /// the payload
value value_ {}; value value_ {};
private:
/// mutex to guard payload
static std::mutex token_;
public: public:
/// an iterator /// an iterator
class iterator class iterator
@ -431,9 +429,11 @@ class json
/// the current character /// the current character
char current_ {}; char current_ {};
/// the position inside the input buffer /// 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 /// 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);

View file

@ -3,6 +3,8 @@
#include "json.h" #include "json.h"
using json = nlohmann::json;
TEST_CASE("array") TEST_CASE("array")
{ {
SECTION("Basics") SECTION("Basics")