0

I recently found out that nlohmann/json runs on C++ 11. Otherwise it gives some syntax error due to a deprecated line from using a higher C++ version.

bazel-out/k8-opt/bin/examples/collaboration_station/_virtual_includes/manipulation_station/drake/examples/manipulation_station/include/json.hpp:2149:30: error: redundant redeclaration of 'constexpr' static data member 'nlohmann::detail::static_const<T>::value' [-Werror=deprecated]
 constexpr T static_const<T>::value;
                              ^~~~~
bazel-out/k8-opt/bin/examples/collaboration_station/_virtual_includes/manipulation_station/drake/examples/manipulation_station/include/json.hpp:2145:24: note: previous declaration of 'nlohmann::detail::static_const<T>::value'
     static constexpr T value{};

I was able to run a normal c++ file with nlohmann by using the key term

bazel run --cxxopt='-std=c++11

However, if I incorporate drake into my example, it does not run (most likely due to a low C++ version)

In order to test out nlohmann/json, I took their json.hpp file located under json/single_include/nlohmann/json.hpp on their github page. I made sure to #include the json file.

Is there an easy way to incorporate nlohmann/json or any other type of json reader into drake?

Yuki
  • 139
  • 6
  • 1
    I'm compiling using C++17 with drake and it seems to work fine for me. Can you post more specifically about the compile error you are facing and how you're using drake? – cjds Jul 11 '20 at 06:24
  • 1
    The core of the error is what I posted above. I seem to have found an external file called json under tools/workspace I decided to use that instead of nlohmann and it works for now. – Yuki Jul 13 '20 at 18:54
  • Sorry for the late follow-up, but glad you were able to resolve it! Would you have time to post your resolution as a self-answer? – Eric Cousineau Jul 16 '20 at 00:44
  • Yes, I just posted. Hope it helps. Hopefully Drake can use Json files more in the future. – Yuki Jul 17 '20 at 21:01

1 Answers1

0

Although this didn't solve the nlohmann problem, this is an alternative to parse a json file in Drake. If you ever find it annoying to recompile your c++ file every time you change a variable, using a json file may be useful.

In your BUILD.bazel file under deps, write

"@jsoncpp",

Then in your .cc file, include the followings:

#include <fstream>
#include <jsoncpp/json/json.h>
#include "drake/common/find_resource.h"

Now write your json parsing function

Json::Value json_parser(){
  Json::Reader reader;
  Json::Value root;
  drake::log()->info("json parser activated.");
  
  // User does not have to specify /home/your_name/
  std::ifstream myfile(FindResourceOrThrow("drake/path_to_file/config.json"));
  myfile >> root;
  drake::log()->info("reading json config file completed.");
  return root;

  // Example of accessing **outside** of this function
  // Json::Value root = json_parser();
  // cout << root["name"].asString() << endl;
}

For this example, the name of the json file is "config.json". Feel free to change it to whatever you want.

Yuki
  • 139
  • 6