diff --git a/docs/begin.md b/docs/begin.md new file mode 100644 index 00000000..3730402d --- /dev/null +++ b/docs/begin.md @@ -0,0 +1,41 @@ +```cpp +iterator begin(); +const_iterator begin() const; +const_iterator cbegin() const; +``` + +## Description + +Returns an iterator to the first value in the JSON container. If the JSON container is empty, the returned iterator will be equal to [`end()`](https://github.com/nlohmann/json/wiki/nlohmann::basicjson::end). + +![illustration of iterators](http://upload.cppreference.com/mwiki/images/1/1b/range-begin-end.svg) + +## Parameters + +None. + +## Return value + +Iterator to the first value. Note the return value its deferencabilty depends on the different value types: + +| value type | deferenceable | description | +| ---------- | ------------- | ----------- | +| null | no | `null` has no value, always equal to [`end()`](https://github.com/nlohmann/json/wiki/nlohmann::basicjson::end) | +| boolean | yes | iterator to the boolean value | +| string | yes | iterator to the string value | +| number | yes | iterator to the number value | +| object | only if object is not empty | iterator to the begin of the object | +| array | only if array is not empty | iterator to the begin of the array | + +## Complexity + +Constant, as long as `ArrayType` and `ObjectType` satisfy the [Container concept](http://en.cppreference.com/w/cpp/concept/Container). + +## Exceptions + +None. The function's noexcept-specification is `noexcept`. + +## See also + +- [**end**, **cend**](https://github.com/nlohmann/json/wiki/nlohmann::basicjson::end)
+ returns an iterator to the end diff --git a/mkdocs.yml b/mkdocs.yml index 71972cb3..29eba859 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -1,3 +1,7 @@ site_name: JSON for Modern C++ -#theme: readthedocs +theme: readthedocs +#theme: bootstrap +#theme: yeti + +markdown_extensions: [fenced_code] diff --git a/src/json.hpp b/src/json.hpp index 2057f6e4..a9da694c 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -156,7 +156,7 @@ class basic_json /// returns the allocator associated with the container - inline allocator_type get_allocator() const + inline static allocator_type get_allocator() { return allocator_type(); } @@ -578,6 +578,14 @@ class basic_json return basic_json(l, false, value_t::object); } + /// construct an array with count copies of given value + inline basic_json(size_type count, const basic_json& other) + : m_type(value_t::array) + { + AllocatorType alloc; + m_value.array = alloc.allocate(1); + alloc.construct(m_value.array, count, other); + } /////////////////////////////////////// // other constructors and destructor // diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 94848617..58d80d9d 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -156,7 +156,7 @@ class basic_json /// returns the allocator associated with the container - inline allocator_type get_allocator() const + inline static allocator_type get_allocator() { return allocator_type(); } @@ -578,6 +578,14 @@ class basic_json return basic_json(l, false, value_t::object); } + /// construct an array with count copies of given value + inline basic_json(size_type count, const basic_json& other) + : m_type(value_t::array) + { + AllocatorType alloc; + m_value.array = alloc.allocate(1); + alloc.construct(m_value.array, count, other); + } /////////////////////////////////////// // other constructors and destructor // diff --git a/test/unit.cpp b/test/unit.cpp index 6c2aec6d..e251f776 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -902,6 +902,17 @@ TEST_CASE("constructors") } } } + + SECTION("create an array of n copies of a given value") + { + json v = {1, "foo", 34.23, {1, 2, 3}, {{"A", 1}, {"B", 2}}}; + json arr(3, v); + CHECK(arr.size() == 3); + for (auto& x : arr) + { + CHECK(x == v); + } + } } TEST_CASE("other constructors and destructor") @@ -7762,6 +7773,56 @@ TEST_CASE("algorithms") TEST_CASE("concepts") { + SECTION("container requirements for json") + { + // X: container class: json + // T: type of objects: json + // a, b: values of type X: json + + // TABLE 96 - Container Requirements + + // X::value_type must return T + CHECK((std::is_same::value)); + + // X::reference must return lvalue of T + CHECK((std::is_same::value)); + + // X::const_reference must return const lvalue of T + CHECK((std::is_same::value)); + + // X::iterator must return iterator whose value_type is T + CHECK((std::is_same::value)); + // X::iterator must meet the forward iterator requirements + CHECK((std::is_base_of::iterator_category>::value)); + // X::iterator must be convertible to X::const_iterator + CHECK((std::is_convertible::value)); + + // X::const_iterator must return iterator whose value_type is T + CHECK((std::is_same::value)); + // X::const_iterator must meet the forward iterator requirements + CHECK((std::is_base_of::iterator_category>::value)); + + // X::difference_type must return a signed integer + CHECK((std::is_signed::value)); + // X::difference_type must be identical to X::iterator::difference_type + CHECK((std::is_same::value)); + // X::difference_type must be identical to X::const_iterator::difference_type + CHECK((std::is_same::value)); + + // X::size_type must return an unsigned integer + CHECK((std::is_unsigned::value)); + // X::size_type can represent any non-negative value of X::difference_type + + // the expression "X u" has the post-condition "u.empty()" + { + json u; + CHECK(u.empty()); + } + + // the expression "X()" has the post-condition "X().empty()" + CHECK(json().empty()); + } + SECTION("class json") { SECTION("DefaultConstructible")