-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Need help with to_json for derived class, keep getting "cannot use operator" #1931
Comments
I think I figured out how to write the json file as I wanted it, but I'm still having trouble reading it back in This is how I'm writing it out:
and it produces this file:
However when I try to read it back in as I get the same error about operator[] What would be the correct way to read this back in and fill out the LaserWeapon (and base Weapon) classes? |
The code is creating the JSON as arrays instead of nested objects. For instance, the You can generate nested objects as demonstrated in README: https://github.com/nlohmann/json#json-as-first-class-data-type Here is a boiled-down example: https://wandbox.org/permlink/LABEaFLHBIUZMjtQ |
thanks for the tips! This code:
produces this:
however I'm really struggling to figure out how to read it back into a LaserWeapon, filling out the base class and the derived class's extra variables... I've tried several methods and most give me an exception about using at() or operator[] |
Again, your code is creating nested arrays which would not be easy to handle. For instance, you have to access j[0][1][1][1]["x"] Instead, you should fix the code to construct nested JSON objects. Here is a demonstration: https://wandbox.org/permlink/mtQMInu3E2FkmocE |
your example on wandbox is perfect, and I think it should be added to the readme/examples to help others thanks so much for your time! |
Hello,
I have a two part question:
I'm having trouble accessing the base class from inside the to_json for the derived class, even though I have it set to "friend" everywhere. I got it somewhat working by doing
j = (static_cast<Weapon>(weapon));
, is that the expected way to do it?I'm having trouble with to_json from the derived class, every attempt I make throws the error "cannot use operator[]", or "cannot use at()", even though the exact same code works fine outside of the to_json() function!
I have a base class:
and a derived class:
I want to write a to_json function that will produce something similar to this output:
but I keep getting errors when trying j[] , j.at(), j.emplace etc
when inside the to_json function I can't even use
j["test"] = 4;
orj.at("test") = 4;
without it throwing an error (JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
), but that same code works fine inside of main() - what am I doing wrong?just to show you what I'm trying, I would expect this to work based on documentation, but this is where I keep getting runtime errors:
however I get
"cannot use operator[] with a string argument with " + std::string(type_name())
Thanks for any help you can provide!
Edit:
also just to save some time, I should have specified that I've already implemented the glm::vec3 part and confirmed that works:
The text was updated successfully, but these errors were encountered: