-
-
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
Derived classes from json class #4316
Comments
Hi can I work on this problem? |
Sure. |
The difference is because you derive from |
I looked into it but I have a hard time figuring it out. I think its because of some typechecking during runtime. |
How did you define the constructor of Class Cookie? |
I was able to write the following test that passes. I used constructor delegation from the //
// Created by arthi on 20-3-2024.
//
#include "doctest_compatibility.h"
#include <nlohmann/json.hpp>
#include <string>
using nlohmann::json;
class Cookie : public json
{
public:
Cookie(const std::string& cookieName, const std::string cookieValue ,const std::string jsonValue ) :json(jsonValue)
{
// this->operator[]("name") = cookieName;
// this->operator[]("value") = cookieValue;
}
};
class JsonFile : public json
{
};
TEST_CASE("Derived classes handling") {
SECTION("Assign derived object to json") {
Cookie A1("testCookie", "A1=d=AQABBN3h6GUCECMYqmLClG1pK2uiuaCh0xoFEgABCAEq6mUTZutMb2UBAiAAAAcI3OHoZXSoivI&S=AQAAAqvfHaGFJkLfQ07HNDr2tH0;Expires=Thu, 6 Mar 2025 213629 GMT;",R"({ {"one", 1}, {"two", 2} })");
JsonFile cJsonFile;
cJsonFile["Cookies"]["A1"] = A1;
CHECK(cJsonFile["Cookies"]["A1"].dump() == A1.dump());
}
}
Still, I have problems with the following lines: // (*this)["name"] = cookieName;
// (*this)["value"] = cookieValue;
|
Well, maybe the problem stems from my misunderstanding of object inheritance... If I want to create a json object, I could create it like
This means that I can create key-value pairs in the inherited class as well, right? This way it will be like for example:
This will prints out:
|
I think you are right I have no clue why that is not working |
I was thinking that when json takes over the parameters of the derived class, it filters based on some object id, so that either std::string or a json_base class can be passed. I still don't fully understand everything about C++, but it seems like the compiler doesn't like something around std::forward. I also tried passing it as |
I think we need some help from someone that has more experience with this codebase. |
Can you post a full code example that shows the problem? You can use this as a starting point. https://www.godbolt.org/z/93osKEKEs |
Here it goes, Sir: I didn't post the full JsonFile class, because it has a few dependencies of my own classes (in the original you can pass trough a filepath and it tries to open it that will be the load/save file for the json object, but basically that's the only difference), but the error is the same here. |
There's something weird with the conversion from This reproduces the error:
This change removes the error:
|
Minimal reproduction scenario:
So there's apparently something wrong with the constructor selection logic. |
Thanks! |
If you don't want to change like that, then you can also do this to clean up the selection.
|
Oh, thanks a lot! This is what I'm looking for! So basically it's not a simple nlohmann::json cast, but a const reference... that's why it didn't work for me before. |
Description
Equal operator won't work with derived classes.
Reproduction steps
class Cookie : public nlohmann::json
<- this can handle cookies, chops up cookie strings from headers, etc...class JsonFile : public nlohmann::json
<- this is nothing else but the json class joined up with a fstream to make easier to load and save json from file.Cookie A1("A1=d=AQABBN3h6GUCECMYqmLClG1pK2uiuaCh0xoFEgABCAEq6mUTZutMb2UBAiAAAAcI3OHoZXSoivI&S=AQAAAqvfHaGFJkLfQ07HNDr2tH0;Expires=Thu, 6 Mar 2025 213629 GMT;");
<- this will create a json objectPass it to
JsonFile
FileHandlers::JsonFile cJsonFile(cookieJson);
cJsonFile["Cookies"]["A1"] = A1;
<- this drops error: "terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_2::detail::type_error' what(): [json.exception.type_error.302] type must be string, but is object"nlohmann::json::parse(A1.dump())
<- this works, but it seems like a not ideal workaroudExpected vs. actual results
I expect that as an nlohmann::json derived object it should be passed as
nlohmann::json x = { {"one", 1}, {"two", 2} };
nlohmann::json y;
y["thingy"] = x;
Minimal code example
In reproduction steps.
Error messages
"terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_2::detail::type_error' what(): [json.exception.type_error.302] type must be string, but is object"
Compiler and operating system
GCC, Ubuntu
Library version
3.11.2
Validation
develop
branch is used.The text was updated successfully, but these errors were encountered: