Created by: muttleyxd
PR checklist
-
Read the contribution guidelines. -
Ran the shell script under ./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
,./bin/openapi3/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first. -
Filed the PR against the correct branch: master
,4.1.x
,5.0.x
. Default:master
. -
Copied the technical committee to review the pull request if your PR is targeting a particular programming language. @ravinikam @stkrwork @etherealjoy @MartinDelille
Description of the PR
While generated sample downloads nlohmann-json from GitHub, it could also be provided by operating system. Debian 10 Buster provides nlohmann-json3-dev package with 3.5.0 version https://packages.debian.org/buster/nlohmann-json3-dev.
contains
was added in 3.6.0 https://github.com/nlohmann/json/releases/tag/v3.6.0
This change makes generated code compatible with 3.5.0.
Here's the code I tested it with:
#include <nlohmann/json.hpp>
#include <iostream>
void check_with_find(char const * const key, nlohmann::json const &json)
{
if (json.find(key) != json.end())
std::cout << "find: " << key << " exists!\n";
else
std::cout << "find: " << key << " does not exist!\n";
}
void check_with_contains(char const * const key, nlohmann::json const &json)
{
if (json.contains(key))
std::cout << "contains: " << key << " exists!\n";
else
std::cout << "contains: " << key << " does not exist!\n";
}
int main()
{
constexpr char const * existing_key = "hello";
constexpr char const * not_existing_key = "there";
nlohmann::json json;
json[existing_key] = "general";
check_with_find(existing_key, json);
check_with_find(not_existing_key, json);
check_with_contains(existing_key, json);
check_with_contains(not_existing_key, json);
}
Output:
$ g++ -std=c++17 -Wall -Wextra -pedantic json-find.cpp -o json-find
$ ./json-find
find: hello exists!
find: there does not exist!
contains: hello exists!
contains: there does not exist!
contains
is implemented as:
template<typename KeyT>
bool contains(KeyT&& key) const
{
return is_object() and m_value.object->find(std::forward<KeyT>(key)) != m_value.object->end();
}
So it's almost the same code