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

Explicitly getting string without .dump() #401

Closed
Noitidart opened this issue Dec 21, 2016 · 3 comments
Closed

Explicitly getting string without .dump() #401

Noitidart opened this issue Dec 21, 2016 · 3 comments

Comments

@Noitidart
Copy link

Noitidart commented Dec 21, 2016

Hi there all!
I had a need to get a string explicitly from a json object. I saw this in the readme:

// explicit conversion to string
std::string s = j.dump();    // {\"happy\":true,\"pi\":3.141}

However this caused me a hidden issue a couple times without realizing. It would surround it with double quotes. This is expected and great behavior, but is there a way to get explicit string without .dump()?

These are my uses cases:

  1. First use:
void launchSystemFile(json aArg) {
   aArg["path"].find("blah")
}

This fails compiling and underlines the != in visual studio like this:

  1. Another use is this:
    std::string path = "";
    if (aArg.count("path")) {
        path = aArg["path"]
    }

This compiles, but it makes visual studio get a red underline (it just looks scary haha):

@nlohmann
Copy link
Owner

You can get a string like this:

std::string my_string = aArg["path"];
if (my_string.find("firefox.exe") != std::string::npos)
    ...

Note that the JSON value aArg["path"] must be a JSON string. You can check this with aArg["path"].is_string() if you're not sure.

@Noitidart
Copy link
Author

Noitidart commented Dec 21, 2016

Ah thank you! That is so easy! I was trying all kinds of weird stuff haha! I was trying string_cast<std::string>(aArg["path"[) with the below I found on stackoverflow. Goodness gracious haha! I need to keep it simple haha.

// start - strcast
#include <vector>
#include <string>
#include <cstring>
#include <cwchar>
#include <cassert>

#include <windows.h>
#include <atlstr.h> // for CString

template<typename Td>
Td string_cast(const wchar_t* pSource, unsigned int codePage = CP_ACP);

template<typename Td>
Td cstring_cast(CString* pSource);

template<>
std::string string_cast(const wchar_t* pSource, unsigned int codePage)
{
	assert(pSource != 0);
	size_t sourceLength = std::wcslen(pSource);
	if (sourceLength > 0)
	{
		int length = ::WideCharToMultiByte(codePage, 0, pSource, sourceLength, NULL, 0, NULL, NULL);
		if (length == 0)
			return std::string();

		std::vector<char> buffer(length);
		::WideCharToMultiByte(codePage, 0, pSource, sourceLength, &buffer[0], length, NULL, NULL);

		return std::string(buffer.begin(), buffer.end());
	}
	else
		return std::string();

}

template<>
std::string cstring_cast(CString* pSource)
{
	// http://stackoverflow.com/a/258052/1828637
	CT2CA pszConvertedAnsiString(*pSource);
	// construct a std::string using the LPCSTR input
	std::string str(pszConvertedAnsiString);
	return str;

}

@nlohmann
Copy link
Owner

Good that this helped.

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