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

find an object that matches the search criteria #1705

Closed
racdhawan opened this issue Aug 12, 2019 · 3 comments
Closed

find an object that matches the search criteria #1705

racdhawan opened this issue Aug 12, 2019 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@racdhawan
Copy link

  • Describe what you want to achieve.
    Trying to find object(s) that matches the search criteria
  • Describe what you tried.
    Do I need to write a search on each key value and loop over all the objects in the vector to find the match? That seems too expensive. Any suggestions? I should be able to search on all the keys and return the matching objects.
class person {
	public:
		std::string name;
		std::string address;
		int age;
	};

	void to_json(json& j, const person& p) {
		j = json{ {"name", p.name}, {"address", p.address}, {"age", p.age} };
	}

	void from_json(const json& j, person& p) {
		if (j.contains("name")) p.name = j.at("name").get<std::string>();
		if (j.contains("address")) p.address = j.at("address").get<std::string>();
		if (j.contains("age")) p.age = j.at("age").get<int>();
	
	}

int main()
{
	       std::vector<ns::person> persons;
		// convert to JSON: copy each value into the JSON object
		ifstream inputFile("input/person.json");
		json j;
		ns::person person;
		try {
			j = json::parse(inputFile, NULL, true);// false to get rid of error
			if (j.is_discarded()) {
				cout << "error";
			}
		}
		catch (std::exception& e)
		{
			std::cerr << e.what() << std::endl;
		}
		for (int i = 0; i < j.size(); i++)
		{
			std::cout << j.at(i)<<"\n";
			person = j.at(i);
			persons.push_back(person);
		}
//ask user for search criteria
// loop over all the objects and return the match?
}
  • Describe which system (OS, compiler) you are using.

  • Describe which version of the library you are using (release version, develop branch).

@nlohmann
Copy link
Owner

The items function may help.

@racdhawan
Copy link
Author

I am not sure how to map the std::vector(thats what i am using) to nlohmann ::json and use the items function on it.

@racdhawan
Copy link
Author

nevermind. I figured looping over each json object in the vector of objects and doing a search on each key using items.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Aug 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants