-
-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Add a type in the natvis template for detail::json_default_base #4973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
Signed-off-by: Mihnea Magheru <[email protected]>
| </Type> | ||
|
|
||
| <Type Name="{{ ns }}::detail::json_default_base"> | ||
| <DisplayString Condition="m_data.m_type == {{ ns }}::detail::value_t::null">null</DisplayString> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m_data is not a member of json_default_base. Why is this useful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Greg!
I understand your concern and I'll have to admit I'm not versed into the library internal data structures.
But what I did notice with the VS version added in the linked issue can be seen in this screenshot

So VS sees the nlohmann:json object obj type as a json default_base object which most likely is just a polymorphic type. That's probably the reason why m_data can be found in the object.
And after I've added the natvis type I was suggesting in this PR VS did manage to show properly the content of it as in the screenshot below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's interesting. What is json_t?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh yeah, sorry, that's just a typedef to nlohmann::json we use in our codebase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey guys,
I've dig deeper into this situation and I've learned the followings:
In Visual Studio 2022, when inspecting nlohmann::json variables, the debugger often shows them as the empty base type nlohmann::detail::json_default_base. This happens because VS displays objects according to their static type, not their dynamic type — even though the runtime object is a full nlohmann::json.
Without a visualizer for json_default_base, the debugger cannot interpret the derived JSON contents and only shows raw memory or {...}.
Adding a Natvis for json_default_base ensures that the debugger displays JSON values based on their actual type (object, array, string, etc.) and allows convenient expansion. Although json_default_base is empty in C++, this visualizer is required to bridge the gap between VS’s static-type view and the actual runtime data.
I've tried a few workarounds to make it take the dynamic type instead but I failed to produce a working natvis file.
Maybe somebody with more Natvis experience would be able to crack this, I dunno :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's definitely not their static type, because if that were their static type, you wouldn't be able to do anything with them. The static type of obj is nlohmann::json. I wonder if it's being confused by the inline namespace or the alias, and that's what it's looking up in natvis. It is finding the base type, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might help track down the issue: https://stackoverflow.com/questions/74713426/debugging-visual-studio-code-natvis-based-custom-view-objects
With version 3.12.0 the underlying type for a
nlohmann::jsonobject has becomenlohmann::json_abi_diag_v3_12_0::detail::json_default_baseand this type does not have a Type definition in the corresponding natvis template and generated natvis file.This PR updates the Natvis jinja template to add all the types derived from json_default_base. Also the template is rendered to produce the nlohmann_json.natvis
fixes #4972
make amalgamate.Read the Contribution Guidelines for detailed information.