view::const_iterator view::find(stdx::string_view key) const { bson_t b; if (!bson_init_static(&b, _data, _length)) { return cend(); } bson_iter_t iter; // Logically, a default constructed string_view represents the // empty string just as does string_view(""), but they have, // potentially, different represntations, the former having .data // returning nullptr though the latter probably does not. But the // C functions like strncmp below can't be called with nullptr. If // we were called with a string_data such that its .data() member // returns nullptr, then, barring undefined behavior, its length // is known to be zero, and it is equivalent to the empty string, // an instance of which we reset it to. if (key.data() == nullptr) { key = ""; } if (!bson_iter_init_find_w_len(&iter, &b, key.data(), static_cast<int>(key.size()))) { return cend(); } return const_iterator(element( _data, static_cast<uint32_t>(_length), bson_iter_offset(&iter), bson_iter_key_len(&iter))); }
view::const_iterator view::find(stdx::string_view key) const { bson_t b; bson_iter_t iter; if (!bson_init_static(&b, _data, _length)) { return cend(); } if (!bson_iter_init(&iter, &b)) { return cend(); } if (key.empty()) { return cend(); } while (bson_iter_next(&iter)) { const char* ikey = bson_iter_key(&iter); if (0 == strncmp(key.data(), ikey, key.size())) { return const_iterator(element(iter.raw, iter.len, iter.off)); } } return cend(); }
document::value from_json(stdx::string_view json) { bson_error_t error; bson_t* result = bson_new_from_json(reinterpret_cast<const uint8_t*>(json.data()), json.size(), &error); if (!result) throw exception(error_code::k_json_parse_failure, error.message); std::uint32_t length; std::uint8_t* buf = bson_destroy_with_steal(result, true, &length); return document::value{buf, length, bson_free_deleter}; }
stdx::optional<document::value> from_json(stdx::string_view json) { bson_error_t error; bson_t* result = bson_new_from_json(reinterpret_cast<const uint8_t*>(json.data()), json.size(), &error); if (!result) return stdx::nullopt; std::uint32_t length; std::uint8_t* buf = bson_destroy_with_steal(result, true, &length); return document::value{buf, length, bson_free_deleter}; }