! @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"`
| 19404 | @since version 1.0.0 |
| 19405 | */ |
| 19406 | void push_back(basic_json&& val) |
| 19407 | { |
| 19408 | // push_back only works for null objects or arrays |
| 19409 | if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_array()))) |
| 19410 | { |
| 19411 | JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()))); |
| 19412 | } |
| 19413 | |
| 19414 | // transform null object into an array |
| 19415 | if (is_null()) |
| 19416 | { |
| 19417 | m_type = value_t::array; |
| 19418 | m_value = value_t::array; |
| 19419 | assert_invariant(); |
| 19420 | } |
| 19421 | |
| 19422 | // add element to array (move semantics) |
| 19423 | m_value.array->push_back(std::move(val)); |
| 19424 | // invalidate object: mark it null so we do not call the destructor |
| 19425 | // cppcheck-suppress accessMoved |
| 19426 | val.m_type = value_t::null; |
| 19427 | } |
| 19428 | |
| 19429 | /*! |
| 19430 | @brief add an object to an array |
no test coverage detected