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

error C2338: could not find to_json() method in T's namespace #501

Closed
iMiamas opened this issue Mar 13, 2017 · 18 comments
Closed

error C2338: could not find to_json() method in T's namespace #501

iMiamas opened this issue Mar 13, 2017 · 18 comments
Labels

Comments

@iMiamas
Copy link

iMiamas commented Mar 13, 2017

in vs2015 compile
json.hpp(1038): error C2338: could not find to_json() method in T's namespace

@nlohmann
Copy link
Owner

Could you provide a code example that triggers this error?

@nlohmann nlohmann added the platform: visual studio related to MSVC label Mar 13, 2017
@nlohmann
Copy link
Owner

(The unit tests run fine with AppVeyor, so the error message should come from code outside the library. It indicates that you tried to convert some user-defined type to json that the library does not know and did not find an overload to_json method for.)

@iMiamas
Copy link
Author

iMiamas commented Mar 14, 2017

I us it like this

 struct person {
            std::string name;
            std::string address;
            int age;
        };

    person test {"Ned Flanders", "744 Evergreen Terrace", 60};
    json j = test;

use your example, but it does not work

@iMiamas
Copy link
Author

iMiamas commented Mar 14, 2017

oh sorry,i see, i does not see the flow explain, thank you!

@nlohmann
Copy link
Owner

No worries!

@nlohmann nlohmann added solution: invalid the issue is not related to the library and removed solution: invalid the issue is not related to the library labels Mar 14, 2017
@iMiamas
Copy link
Author

iMiamas commented Mar 14, 2017

another question,like this

typedef char DateType[9];
typedef char TimeType[9];
typedef char UserIDType[16];

struct UserInfo
{
	DateType	  TradingDay;
	TimeType	  LoginTime;
        UserIDType  UserID;
}

How can i convert struct UserInfo to Json?

@iMiamas
Copy link
Author

iMiamas commented Mar 14, 2017

i don't find a way to convert c array, just convert like this in from_json function

    strcpy_s(obj.TradingDay,j["TradingDay"].get<std::string>().c_str());
    strcpy_s(obj.LoginTime, j["LoginTime"].get<std::string>().c_str());
    strcpy_s(obj.UserID,j["UserID"].get<std::string>().c_str());

but i don‘t kown if it's a reasonable way
do you have any suggest to me? thank you.

@nlohmann
Copy link
Owner

C-arrays are currently not supported, see #502.

@iMiamas
Copy link
Author

iMiamas commented Mar 14, 2017

got it, thank you!

@Chrjstoph
Copy link

Chrjstoph commented Mar 18, 2017

I want to chime in and ask if this has been resolved (vs2015 x64).

The following comlains as well:

namespace glm
{
	void to_json(json& j, const vec3& p) 
	{
		// ...
	}

	void from_json(const json& j, vec3& p)
	{
		// ...
	}
}

@nlohmann
Copy link
Owner

What does the error message look like? What is vec3?

@Chrjstoph
Copy link

Chrjstoph commented Mar 18, 2017 via email

@nlohmann
Copy link
Owner

@theodelrieu Any ideas on this?

@fehmud
Copy link

fehmud commented May 16, 2017

I have stumbled upon the same issue. I found the offending code by replacing the static_assert with a run-time check that resembled this pseudo-code (sorry, but I am not at my workstation now):

std::string offending_type = typeid(T).name();
if (fail) { DebugBreak(); } // Breakpoint.

I was on Windows with Visual Studio 2015, which provided a helpful type name (other compilers can be less helpful in this regard).

In other words, if you can't provide a descriptive error message at compile time, then please implement a run-time check with the most descriptive message that you can put together, or invoke the debugger. After all, your code will run under a debugger at least once.

Thanks for providing us with this very useful library.

@theodelrieu
Copy link
Contributor

I understand that the error message is not descriptive enough, I'd really like to put the real type in the message, but AFAIK, it's not possible, static_assert requiring a string literal.

I am strongly opposed to transform a compile-time error to a runtime error, however I'm aware that VS2015 only shows the static_assert line without any additional info, whereas Clang and GCC provide a stack that eventually shows the type name.

We could add some lines after the static_assert, that calls the non-existent to_json method, so that VS2015 is forced to show you the type.

@fehmud
Copy link

fehmud commented May 17, 2017

Yes, a descriptive compilation error would be a good alternative. Thanks for your attention.

@tigerpup
Copy link

Hi I m facing this issue while sending some video data using json from my c++ addon to my node script
Here is the code where I m getting the Error :
`static json getjsonpacket(void data, void data1, int stride, int width , int height,int type,int rotation){
unsigned char* cb = 0;
if(data1)
{
cb = (unsigned char*)data1;
}
else
cb = ((unsigned char*) data+ (stride *height));

    unsigned char* cr = cb+ ((stride * height) >> 2);

      json sample;
      // Here is where the error is triggered from
      sample["loadImageData"] = (unsigned char*)data;
      // sample["chromaBPtr"]=cb;
      sample["chromaBSize"]=stride >> 1;
      // sample["chromaRPtr"]=cr;
      sample["ChromaRSize"]=stride >> 1;
      sample["lumaSize"] = stride;
      sample["format"]=type;
      sample["rotation"]=rotation;
      sample["height"]=height;
      sample["width"]=width;
  return sample;    
}`

The Exact error I get is :

static_assert failed "could not find to_json() method in T's namespace"
static_assert(sizeof(BasicJsonType) == 0,

this is a function to return json for a certain input arguments. The issue is , that i get the above error when I build.
below is the function call of function given above
json sample=VRenderParam::getjsonpacket(packet->Y, packet->U ,packet->width,packet->width,packet->height,packet->type,packet->rotation);

where Y and U are of the type (void *) . I dont know why is it so. They are part YUV video data that I m trying to send via a stream , using streaming-worker with node.
If helpful here is the packet object which packet->Y,packet->U are part of :
int width; int height; int stride; char* data; void* imgBuf; void* Y; void* U; void* V; int dataLen; int type; int rotation;

I think the error is that Template is unable to take the type unsigned char* which I trying to explicitly convert data and data1 in the first function given here. how should I tackle this. because If I comment these fields the program works fine. Please Let me know if I should convert to some other type or some other way to send this data using json.

@gregmarr
Copy link
Contributor

You need to convert them to std::string, assuming they are nul-terminated strings.

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

No branches or pull requests

7 participants