! @brief add an object to an array Appends the given element @a val to the end of the JSON value. If the function is called on a JSON null value, an empty array is created before appending @a val. @param[in] val the value to add to the JSON array @throw type_error.308 when called on a type other than JSON array or null; example: `"cannot use push_back() with number"`
| 5449 | @since version 1.0.0 |
| 5450 | */ |
| 5451 | void push_back(basic_json&& val) |
| 5452 | { |
| 5453 | // push_back only works for null objects or arrays |
| 5454 | if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) |
| 5455 | { |
| 5456 | JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); |
| 5457 | } |
| 5458 | |
| 5459 | // transform null object into an array |
| 5460 | if (is_null()) |
| 5461 | { |
| 5462 | m_type = value_t::array; |
| 5463 | m_value = value_t::array; |
| 5464 | assert_invariant(); |
| 5465 | } |
| 5466 | |
| 5467 | // add element to array (move semantics) |
| 5468 | const auto old_capacity = m_value.array->capacity(); |
| 5469 | m_value.array->push_back(std::move(val)); |
| 5470 | set_parent(m_value.array->back(), old_capacity); |
| 5471 | // if val is moved from, basic_json move constructor marks it null so we do not call the destructor |
| 5472 | } |
| 5473 | |
| 5474 | /*! |
| 5475 | @brief add an object to an array |
no test coverage detected