! @brief inserts element Inserts element @a val before iterator @a pos. @param[in] pos iterator before which the content will be inserted; may be the end() iterator @param[in] val element to insert @return iterator pointing to the inserted @a val. @throw type_error.309 if called on JSON values other than arrays; example: `"cannot use insert() with string"` @t
| 17829 | @since version 1.0.0 |
| 17830 | */ |
| 17831 | iterator insert(const_iterator pos, const basic_json& val) |
| 17832 | { |
| 17833 | // insert only works for arrays |
| 17834 | if (JSON_LIKELY(is_array())) |
| 17835 | { |
| 17836 | // check if iterator pos fits to this JSON value |
| 17837 | if (JSON_UNLIKELY(pos.m_object != this)) |
| 17838 | { |
| 17839 | JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value")); |
| 17840 | } |
| 17841 | |
| 17842 | // insert to array and return iterator |
| 17843 | return insert_iterator(pos, val); |
| 17844 | } |
| 17845 | |
| 17846 | JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()))); |
| 17847 | } |
| 17848 | |
| 17849 | /*! |
| 17850 | @brief inserts element |