| 19854 | json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {} |
| 19855 | |
| 19856 | void destroy(value_t t) |
| 19857 | { |
| 19858 | if ( |
| 19859 | (t == value_t::object && object == nullptr) || |
| 19860 | (t == value_t::array && array == nullptr) || |
| 19861 | (t == value_t::string && string == nullptr) || |
| 19862 | (t == value_t::binary && binary == nullptr) |
| 19863 | ) |
| 19864 | { |
| 19865 | //not initialized (e.g. due to exception in the ctor) |
| 19866 | return; |
| 19867 | } |
| 19868 | if (t == value_t::array || t == value_t::object) |
| 19869 | { |
| 19870 | // flatten the current json_value to a heap-allocated stack |
| 19871 | std::vector<basic_json> stack; |
| 19872 | |
| 19873 | // move the top-level items to stack |
| 19874 | if (t == value_t::array) |
| 19875 | { |
| 19876 | stack.reserve(array->size()); |
| 19877 | std::move(array->begin(), array->end(), std::back_inserter(stack)); |
| 19878 | } |
| 19879 | else |
| 19880 | { |
| 19881 | stack.reserve(object->size()); |
| 19882 | for (auto&& it : *object) |
| 19883 | { |
| 19884 | stack.push_back(std::move(it.second)); |
| 19885 | } |
| 19886 | } |
| 19887 | |
| 19888 | while (!stack.empty()) |
| 19889 | { |
| 19890 | // move the last item to local variable to be processed |
| 19891 | basic_json current_item(std::move(stack.back())); |
| 19892 | stack.pop_back(); |
| 19893 | |
| 19894 | // if current_item is array/object, move |
| 19895 | // its children to the stack to be processed later |
| 19896 | if (current_item.is_array()) |
| 19897 | { |
| 19898 | std::move(current_item.m_data.m_value.array->begin(), current_item.m_data.m_value.array->end(), std::back_inserter(stack)); |
| 19899 | |
| 19900 | current_item.m_data.m_value.array->clear(); |
| 19901 | } |
| 19902 | else if (current_item.is_object()) |
| 19903 | { |
| 19904 | for (auto&& it : *current_item.m_data.m_value.object) |
| 19905 | { |
| 19906 | stack.push_back(std::move(it.second)); |
| 19907 | } |
| 19908 | |
| 19909 | current_item.m_data.m_value.object->clear(); |
| 19910 | } |
| 19911 | |
| 19912 | // it's now safe that current_item get destructed |
| 19913 | // since it doesn't have any children |