How to write a custom debugging helper for nlohmann::basic_json?

Viewed 130

I am faced with the task of writing a simple debug helper for Qt Creator 4.13.1 / Qt 5.12.5 / MSVC 2017 compiler for the C++ JSON implementation nlohmann::basic_json (https://github.com/nlohmann/json).

An object of nlohmann::basic_json can contain the contents of a single JSON data type (null, boolean, number, string, array, object) at a time. There's a dump() member function which can be used to output the current content formatted as a std::string regardless of the current data type. I always want to use this function.

What I've done so far:

  • I've looked at https://doc.qt.io/qtcreator/creator-debugging-helpers.html, as well as at the given example files (qttypes.py, stdtypes.py...).
  • I made a copy of the file personaltypes.py and told Qt Creator about its existence at
    Tools / Options / Debugger / Locals & Expressions / Extra Debugging Helpers

The following code works and displays a "Hello World" in the debugger window for nlohmann::basic_json objects.

import dumper

def qdump__nlohmann__basic_json(d, value): 
    d.putNumChild(0)
    d.putValue("Hello World")      

Unfortunately, despite the documentation, I have no idea how to proceed from here on. I still have absolutely no clue how to correctly call basic_json's dump() function with the dumper from Python (e.g. with d.putCallItem ?).

I also have no starting point how to format the returned std::string so that it is finally displayed in the debugger window.

I imagined something like this, but it doesn't work.

d.putValue("data")
d.putNumChild(1)
d.putCallItem('dump', '@std::string', value, 'dump')

I hope someone can give me a little clue so that I can continue thinking in the right direction. For example, can I call qdump__std__string from stdtypes.py myself to interpret the std::string?

0 Answers
Related