Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !3306

[C++][Pistache] Add compatibility for nlohmann-json 3.5.0

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/muttleyxd/pistache-compatibility-json-35 into master Jul 08, 2019
  • Overview 0
  • Commits 2
  • Pipelines 0
  • Changes 9

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

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/muttleyxd/pistache-compatibility-json-35