const iterators for web::json::value

Dec 16, 2013 at 9:37 PM
Edited Dec 16, 2013 at 9:57 PM
I believe it would be very helpful to use range-based for loops over const json::values.
void check_jv_array(const json::value& jv)
{
    if (!jv.is_array())
        return;

    for (const auto& jv_pair : jv)
    {
         //check element...
    }
}
The implementation (<= 1.4) does not support this.

I believe all that is required is to add the const_iterator versions of .begin()/.end(). For example:
/// <summary>
/// Gets the beginning iterator element for a composite value.
/// </summary>
/// <returns>An iterator to the beginning 
///                         of the JSON value.</returns>
iterator begin();
inline json::value::const_iterator json::value::begin() const
{
    return m_value->elements().begin();
}

/// <summary>
/// Gets the end iterator element for a composite value.
/// </summary>
/// <returns>An iterator to the element past the end 
///                  of the JSON value.</returns>
iterator end();
inline json::value::const_iterator json::value::end() const
{
    return m_value->elements().end();
}
Coordinator
Dec 17, 2013 at 3:05 AM
Hi evangineer,

Yes you are correct! We have cbegin()/cend() APIs but to support range based for over const values we need to add a const overload for begin/end. For our next 1.5 release we actually have some changes planned to help improve performance and easier iteration over json values. I'll make sure we work in both non-const and const range based for loops.

Thanks for taking the time to report the issue.
Steve