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

[Question] copy assign json-container to vector #635

Closed
MrJakmer opened this issue Jun 23, 2017 · 15 comments
Closed

[Question] copy assign json-container to vector #635

MrJakmer opened this issue Jun 23, 2017 · 15 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@MrJakmer
Copy link

Hi,

upon the recommendation of Niels Lohmann via e-mail, I want to refer to a question I asked on stackoverflow.

Let me share why I am asking that question: I am trying to search for a "lower bound" in a json-object. I assume this can't work since comparing with a double (or integer) isn't possible. That is why I need the conversion (via copy-assign) to a vector. If the question I referred to is not needed because you can solve the error with the lower bound (see code below), it would be great. But I think using vectors would still be relevant since it would be more efficient, if you use them multiple times.

See the following code:

#include "json.hpp"

using json = nlohmann::json;
using namespace std;


int main()
{

	const vector<double> v = { 0 , 10 , 20 ,100 };
	const json j(v);

	json::const_iterator low = lower_bound(j.cbegin(), j.cend(), 11);
	size_t lowerBoundIndex = low - j.begin();
	cout << lowerBoundIndex << endl;

	return 0;
}

I am using JSON for Modern C++ Version 2.1.1 with Microsoft Visual Studio 2015 Version 14.0.25431.01 Update 3 (regarding to your README the compiler should be okay). My operating system is Windows 7 SP1.

Thanks in advance!

@nlohmann
Copy link
Owner

I'm on holiday, but I am confident someone finds an answer.

@theodelrieu
Copy link
Contributor

I see, the implicit conversion (without calling get) fails when types have multiple operator=, like std::vector.

You can solve this easily however:

auto v = j.get<std::vector<double>>();

@MrJakmer
Copy link
Author

Thanks, that solves my problem with the conversion. Is it okay for you, if I post this as an answer at stackoverflow in case someone else might have the same problem?

I assumed searching for a lower bound with the code above isn't possible. Could you also explain whether this is correct?

@theodelrieu
Copy link
Contributor

Of course!

Well, since your array is sorted, this should work as expected.

@MrJakmer
Copy link
Author

Thanks!

It does not work as expected. If I try to compile it, I get the following error:

Error	C2672	'operator __surrogate_func': no matching overloaded function found

Error	C2893	Failed to specialize function template 'unknown-type std::less<void>::operator ()(_Ty1 &&,_Ty2 &&) const'

@theodelrieu
Copy link
Contributor

theodelrieu commented Jun 26, 2017

I see, this is a known bug, that is fixed on develop. Could you try the proposed workaround in #486 ?

EDIT: this might be an unrelated bug, it's weird to have void as a parameter of std::less...

@MrJakmer
Copy link
Author

Just using the workaround

namespace std {
	template <>
	struct less<::nlohmann::detail::value_t>
	{
		bool operator()(::nlohmann::detail::value_t lhs, ::nlohmann::detail::value_t rhs) const noexcept
		{
			return ::nlohmann::detail::operator<(lhs, rhs);
		}
	};
}

before the main-method should do it, right? I also tried the json.hpp from the commit which fixes the bug. Both didn't help. You might be right about this being an unrelated bug.

@theodelrieu
Copy link
Contributor

Hmm, I'll try to find some time to fix it. What is your test file? The code in your first post?

@MrJakmer
Copy link
Author

Yes.

@theodelrieu
Copy link
Contributor

After bissecting for a while, I found the commit that fixes your bug, this is 90273e9.

You could hijack the nlohmann namespace to reproduce the fix, or directly modify the json.hpp file.
Since 3.0 won't be released soon enough, I believe those are the only available solutions right now...

@MrJakmer
Copy link
Author

Converting to a vector is not a problem and should be more performant when using lower bound multiple times. I could just use the json.hpp from the develop-branch, right?

@theodelrieu
Copy link
Contributor

Hmm, I would not recommand using develop frankly... there are some breaking changes.

Converting to std::vector is indeed the best way, I forgot that on my previous comment.

@nlohmann
Copy link
Owner

nlohmann commented Jul 8, 2017

Is there anything left to do here?

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jul 8, 2017
@MrJakmer
Copy link
Author

MrJakmer commented Jul 8, 2017

As far as I'm concerned, no.

@nlohmann
Copy link
Owner

nlohmann commented Jul 8, 2017

OK. Thanks for checking back!

@nlohmann nlohmann closed this as completed Jul 8, 2017
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

3 participants