Skip to content
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

Class Composition of json members produces incorrect json when constructing with initialization list #3955

Closed
2 tasks
killerswin2 opened this issue Feb 23, 2023 · 2 comments

Comments

@killerswin2
Copy link

Description

When using Composition of types in a class for json abstraction, constructing the class produces wrong incorrect json when using an initialization list. Passed json objects/docs seem to be pushed back in the json object attribute attached to the class.

Reproduction steps

just call the class constructor of your class and pass in the json object.

Expected vs. actual results

[[10,20,30,40,{"apple":20}]] with initialization list construction.

[10,20,30,40,{"apple":20}] without, manually assign the member attribute to be

Minimal code example

#include <iostream>
#include <string>
#include <nlohmann/json.hpp>


class game_data_json_array
{
public:
	game_data_json_array(nlohmann::json object) : jsonArray{ object } {}		// produces [[10,20,30,40,{"apple":20}]]
	nlohmann::json jsonArray;
};


class game_data_json_array2
{
public:
	game_data_json_array2(nlohmann::json object) {
		jsonArray = object; 				// produces [10,20,30,40,{"apple":20}]
	}
	nlohmann::json jsonArray;
};


// json 
/*
[
  10,
  20,
  30,
  40,
  {"apple": 20}
]

*/


int main(void)
{
	nlohmann::json jsonArray = nlohmann::json::parse(R"([
  10,
  20,
  30,
  40,
  {"apple": 20}
])");
	game_data_json_array test1 = game_data_json_array(jsonArray);
	game_data_json_array2 test2 = game_data_json_array2(jsonArray);

	std::cout << test1.jsonArray;

	std::cout << "\n";

	std::cout << test2.jsonArray;

	return 0;
}

Error messages

N/A

Compiler and operating system

Windows 10, msvc 1935

Library version

3.11.2

Validation

@gregmarr
Copy link
Contributor

See if this first FAQ entry answers your question: https://json.nlohmann.me/home/faq/

@killerswin2
Copy link
Author

oof, that's what I get for not reading the faq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants