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

How to use a json::iterator dereferenced value in code? #1120

Closed
014972304505347 opened this issue Jun 5, 2018 · 7 comments
Closed

How to use a json::iterator dereferenced value in code? #1120

014972304505347 opened this issue Jun 5, 2018 · 7 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@014972304505347
Copy link

014972304505347 commented Jun 5, 2018

I am trying to do something like this - it involves taking a json::iterator through some json objects, and getting their keys and values for entries. I want to use the values (i.e. it.value() and mit.value()) as arguments to other functions...

    json result;
    json inboundMessage = ...;//determined at runtime
    json savedMessage = ...; //determined at runtime
    for (json::iterator it = inboundMessage.begin(), mit = savedMessage.begin(); it != inboundMessage.end() && mit != savedMessage.end(); ++it, ++mit) {
	json pair;
	pair.emplace_back("log", it.value());
        pair.emplace_back("data", mit.value());
	result.emplace_back(it.key(), pair);
    }
    return result;

When I build, I get the following error:
'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::basic_json(nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer> &&) noexcept': cannot convert argument 1 from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'std::initializer_list<nlohmann::detail::json_ref<nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>>>'

What is the correct way to access the value referenced by an iterator such that the value can be used by code?

@gregmarr
Copy link
Contributor

gregmarr commented Jun 5, 2018

You want to use json.items() and not the json object itself for iteration.

auto inbound = inboundMessage.items();
auto saved = savedMessage.items();

for (auto it = inbound.begin(), ...

@014972304505347
Copy link
Author

I still get the same error when I do that...

@gregmarr
Copy link
Contributor

gregmarr commented Jun 5, 2018

Which line is giving you the error?

@014972304505347
Copy link
Author

014972304505347 commented Jun 5, 2018

These three lines seem to trigger it individually, triggering 3 errors in total:

     pair.emplace_back("log", it.value());
     pair.emplace_back("data", mit.value());
     result.emplace_back(it.key(), pair);

Actually, the error is technically at line 920 in file "xmemory0". The line is in this block ( I commented on the line with the error) :

template<class _Objty,
	class... _Types>
	static void construct(_Alloc&, _Objty * const _Ptr, _Types&&... _Args)
	{	// construct _Objty(_Types...) at _Ptr
	::new (const_cast<void *>(static_cast<const volatile void *>(_Ptr)))
		_Objty(_STD forward<_Types>(_Args)...); //**THE ERROR IS ON THIS LINE**
	}

File "xmemory0" is located at
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include

for me and is of type "FILE" (no extension...).

@gregmarr
Copy link
Contributor

gregmarr commented Jun 5, 2018

Ah, you're inserting in a map, you want emplace, not emplace_back. The latter is only for arrays.

@014972304505347
Copy link
Author

That solved it. Thank you!

I wonder if its possible to throw better errors in a situation like this. It was not obvious to me that something like this could be the issue, and I thought the error was something else entirely...

@gregmarr
Copy link
Contributor

gregmarr commented Jun 6, 2018

I don't think it's possible, as the error is that by using emplace_back you're essentially passing these parameters to the constructor of a json object through std::vector<json>::emplace_back().

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jun 7, 2018
@nlohmann nlohmann closed this as completed Jun 7, 2018
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