1
0
mirror of https://github.com/nlohmann/json synced 2024-09-20 06:40:13 +00:00

started to implement sequence container

This commit is contained in:
Niels 2015-04-12 12:02:30 +02:00
parent 02f617f083
commit b801adca76
5 changed files with 125 additions and 3 deletions

41
docs/begin.md Normal file
View File

@ -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)<br>
returns an iterator to the end

View File

@ -1,3 +1,7 @@
site_name: JSON for Modern C++
#theme: readthedocs
theme: readthedocs
#theme: bootstrap
#theme: yeti
markdown_extensions: [fenced_code]

View File

@ -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<array_t> alloc;
m_value.array = alloc.allocate(1);
alloc.construct(m_value.array, count, other);
}
///////////////////////////////////////
// other constructors and destructor //

View File

@ -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<array_t> alloc;
m_value.array = alloc.allocate(1);
alloc.construct(m_value.array, count, other);
}
///////////////////////////////////////
// other constructors and destructor //

View File

@ -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<json::value_type, json>::value));
// X::reference must return lvalue of T
CHECK((std::is_same<json::reference, json&>::value));
// X::const_reference must return const lvalue of T
CHECK((std::is_same<json::const_reference, const json&>::value));
// X::iterator must return iterator whose value_type is T
CHECK((std::is_same<json::iterator::value_type, json>::value));
// X::iterator must meet the forward iterator requirements
CHECK((std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<json::iterator>::iterator_category>::value));
// X::iterator must be convertible to X::const_iterator
CHECK((std::is_convertible<json::iterator, json::const_iterator>::value));
// X::const_iterator must return iterator whose value_type is T
CHECK((std::is_same<json::const_iterator::value_type, json>::value));
// X::const_iterator must meet the forward iterator requirements
CHECK((std::is_base_of<std::forward_iterator_tag, typename std::iterator_traits<json::const_iterator>::iterator_category>::value));
// X::difference_type must return a signed integer
CHECK((std::is_signed<json::difference_type>::value));
// X::difference_type must be identical to X::iterator::difference_type
CHECK((std::is_same<json::difference_type, json::iterator::difference_type>::value));
// X::difference_type must be identical to X::const_iterator::difference_type
CHECK((std::is_same<json::difference_type, json::const_iterator::difference_type>::value));
// X::size_type must return an unsigned integer
CHECK((std::is_unsigned<json::size_type>::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")