Skip to content

Commit

Permalink
Add object_range_type and array_range_type
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Jan 31, 2024
1 parent 32b0474 commit c10ca9e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
4 changes: 4 additions & 0 deletions doc/ref/corelib/basic_json.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ Member type |Definition
`const_object_iterator`|A const [RandomAccessIterator](http://en.cppreference.com/w/cpp/concept/RandomAccessIterator) to const [key_value_type](json/key_value.md)
`array_iterator`|A [RandomAccessIterator](http://en.cppreference.com/w/cpp/concept/RandomAccessIterator) to `basic_json`
`const_array_iterator`|A const [RandomAccessIterator](http://en.cppreference.com/w/cpp/concept/RandomAccessIterator) to `const basic_json`
`object_range_type`|range<object_iterator,const_array_iterator> (since 0.173.3)
`const_object_range_type`|range<const_object_iterator,const_array_iterator> (since 0.173.3)
`array_range_type`|range<array_iterator,const_array_iterator> (since 0.173.3)
`const_array_range_type`|range<const_array_iterator,const_array_iterator> (since 0.173.3)
`proxy_type`|proxy<basic_json>. The `proxy_type` class supports conversion to `basic_json&`.
### Static member functions
Expand Down
4 changes: 2 additions & 2 deletions doc/ref/corelib/json/array_range.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
### jsoncons::basic_json::array_range

```cpp
range<array_iterator,const_array_iterator> array_range();
range<const_array_iterator,const_array_iterator> array_range() const;
array_range_type array_range();
const_array_range_type array_range() const;
```
Returns a [range](range.md) that supports a range-based for loop over the elements of a `json` array
Throws `std::domain_error` if not an array.
Expand Down
4 changes: 2 additions & 2 deletions doc/ref/corelib/json/object_range.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
### jsoncons::basic_json::object_range

```c++
range<object_iterator,const_object_iterator> object_range();
range<const_object_iterator,const_object_iterator> object_range() const;
object_range_type object_range();
const_object_range_type object_range() const;
```
Returns a [range](range.md) that supports a range-based for loop over the key-value pairs of a `basic_json` object
Throws `std::domain_error` if not an object.
Expand Down
51 changes: 28 additions & 23 deletions include/jsoncons/basic_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,12 @@ namespace jsoncons {
using object_iterator = typename object_iterator_typedefs<policy_type,key_type,basic_json>::object_iterator_type;
using const_object_iterator = typename object_iterator_typedefs<policy_type,key_type,basic_json>::const_object_iterator_type;
using array_iterator = typename array_iterator_typedefs<policy_type,key_type,basic_json>::array_iterator_type;
using const_array_iterator = typename array_iterator_typedefs<policy_type,key_type,basic_json>::const_array_iterator_type;
using const_array_iterator = typename array_iterator_typedefs<policy_type,key_type,basic_json>::const_array_iterator_type;

using object_range_type = range<object_iterator, const_object_iterator>;
using const_object_range_type = range<const_object_iterator, const_object_iterator>;
using array_range_type = range<array_iterator, const_array_iterator>;
using const_array_range_type = range<const_array_iterator, const_array_iterator>;

private:

Expand Down Expand Up @@ -1304,22 +1309,22 @@ namespace jsoncons {
return evaluate();
}

range<object_iterator, const_object_iterator> object_range()
object_range_type object_range()
{
return evaluate().object_range();
}

range<const_object_iterator, const_object_iterator> object_range() const
const_object_range_type object_range() const
{
return evaluate().object_range();
}

range<array_iterator, const_array_iterator> array_range()
array_range_type array_range()
{
return evaluate().array_range();
}

range<const_array_iterator, const_array_iterator> array_range() const
const_array_range_type array_range() const
{
return evaluate().array_range();
}
Expand Down Expand Up @@ -2074,25 +2079,25 @@ namespace jsoncons {
}

JSONCONS_DEPRECATED_MSG("Instead, use object_range()")
range<object_iterator, const_object_iterator> members()
object_range_type members()
{
return evaluate().object_range();
}

JSONCONS_DEPRECATED_MSG("Instead, use object_range()")
range<const_object_iterator, const_object_iterator> members() const
const_object_range_type members() const
{
return evaluate().object_range();
}

JSONCONS_DEPRECATED_MSG("Instead, use array_range()")
range<array_iterator, const_array_iterator> elements()
array_range_type elements()
{
return evaluate().array_range();
}

JSONCONS_DEPRECATED_MSG("Instead, use array_range()")
range<const_array_iterator, const_array_iterator> elements() const
const_array_range_type elements() const
{
return evaluate().array_range();
}
Expand Down Expand Up @@ -5967,25 +5972,25 @@ namespace jsoncons {
}

JSONCONS_DEPRECATED_MSG("Instead, use object_range()")
range<object_iterator, const_object_iterator> members()
object_range_type members()
{
return object_range();
}

JSONCONS_DEPRECATED_MSG("Instead, use object_range()")
range<const_object_iterator, const_object_iterator> members() const
const_object_range_type members() const
{
return object_range();
}

JSONCONS_DEPRECATED_MSG("Instead, use array_range()")
range<array_iterator, const_array_iterator> elements()
array_range_type elements()
{
return array_range();
}

JSONCONS_DEPRECATED_MSG("Instead, use array_range()")
range<const_array_iterator, const_array_iterator> elements() const
const_array_range_type elements() const
{
return array_range();
}
Expand All @@ -5997,28 +6002,28 @@ namespace jsoncons {
}
#endif

range<object_iterator, const_object_iterator> object_range()
object_range_type object_range()
{
switch (storage_kind())
{
case json_storage_kind::empty_object_value:
return range<object_iterator, const_object_iterator>(object_iterator(), object_iterator());
return object_range_type(object_iterator(), object_iterator());
case json_storage_kind::object_value:
return range<object_iterator, const_object_iterator>(object_iterator(cast<object_storage>().value().begin()),
return object_range_type(object_iterator(cast<object_storage>().value().begin()),
object_iterator(cast<object_storage>().value().end()));
default:
JSONCONS_THROW(json_runtime_error<std::domain_error>("Not an object"));
}
}

range<const_object_iterator, const_object_iterator> object_range() const
const_object_range_type object_range() const
{
switch (storage_kind())
{
case json_storage_kind::empty_object_value:
return range<const_object_iterator, const_object_iterator>(const_object_iterator(), const_object_iterator());
return const_object_range_type(const_object_iterator(), const_object_iterator());
case json_storage_kind::object_value:
return range<const_object_iterator, const_object_iterator>(const_object_iterator(cast<object_storage>().value().begin()),
return const_object_range_type(const_object_iterator(cast<object_storage>().value().begin()),
const_object_iterator(cast<object_storage>().value().end()));
case json_storage_kind::json_const_pointer:
return cast<json_const_pointer_storage>().value()->object_range();
Expand All @@ -6027,24 +6032,24 @@ namespace jsoncons {
}
}

range<array_iterator, const_array_iterator> array_range()
array_range_type array_range()
{
switch (storage_kind())
{
case json_storage_kind::array_value:
return range<array_iterator, const_array_iterator>(cast<array_storage>().value().begin(),
return array_range_type(cast<array_storage>().value().begin(),
cast<array_storage>().value().end());
default:
JSONCONS_THROW(json_runtime_error<std::domain_error>("Not an array"));
}
}

range<const_array_iterator, const_array_iterator> array_range() const
const_array_range_type array_range() const
{
switch (storage_kind())
{
case json_storage_kind::array_value:
return range<const_array_iterator, const_array_iterator>(cast<array_storage>().value().begin(),
return const_array_range_type(cast<array_storage>().value().begin(),
cast<array_storage>().value().end());
case json_storage_kind::json_const_pointer:
return cast<json_const_pointer_storage>().value()->array_range();
Expand Down

0 comments on commit c10ca9e

Please sign in to comment.