nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
42.01k stars 6.63k forks source link

Error with sol for Lua: items() is not a recognized container #4012

Closed UltraEngine closed 1 year ago

UltraEngine commented 1 year ago

Description

I'm trying to integrate nlohmann::json into Lua using the sol library. The only remaining issue is how to iterate through key/value pairs.

Maybe you and the sol developer can work this out? Your libraries work together very well aside from this issue.

Reproduction steps

I declare nlohmann::json in sol like this:

L->new_usertype<Json>("NLOHMANN_JSON",
    "items", [](nlohmann::json& j3) {return j3.items(); },

    // blah blah blah...

);

Then I try to iterate through items in Lua like this:

--Pairs
for k, v in pairs(j3object:items()) do
    Print(tostring(k)..": "..tostring(v))
end

Expected vs. actual results

I get an error saying "Error: It is not recognized as a container".

Minimal code example

No response

Error messages

"Error: It is not recognized as a container"

Compiler and operating system

Visual Studio 2022 / Win 10

Library version

3.4.0

Validation

UltraEngine commented 1 year ago

Here is information on how sol detects containers: https://sol2.readthedocs.io/en/latest/containers.html

UltraEngine commented 1 year ago

I don't think this will ever work. Sol is great but has extremely specific requirements that are easy to upset.

I started developing a table implementation in C++ and I'm getting more traction with that.

UltraEngine commented 1 year ago

I implemented a table class for C++ that can be accessed from Lua and uses nlohmann::json to load JSON files: https://github.com/UltraEngine/tableplusplus