json icon indicating copy to clipboard operation
json copied to clipboard

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

Open iMiamas opened this issue 9 years ago • 18 comments

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

iMiamas avatar Mar 13 '17 14:03 iMiamas

Could you provide a code example that triggers this error?

nlohmann avatar Mar 13 '17 14:03 nlohmann

(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.)

nlohmann avatar Mar 13 '17 16:03 nlohmann

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 avatar Mar 14 '17 01:03 iMiamas

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

iMiamas avatar Mar 14 '17 02:03 iMiamas

No worries!

nlohmann avatar Mar 14 '17 05:03 nlohmann

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 avatar Mar 14 '17 06:03 iMiamas

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.

iMiamas avatar Mar 14 '17 06:03 iMiamas

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

nlohmann avatar Mar 14 '17 07:03 nlohmann

got it, thank you!

iMiamas avatar Mar 14 '17 08:03 iMiamas

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)
	{
		// ...
	}
}

Chrjstoph avatar Mar 18 '17 11:03 Chrjstoph

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

nlohmann avatar Mar 18 '17 11:03 nlohmann

Oh, how promt, nice.

glm::vec3 is a vector, wrapper for a three element float array.

https://glm.g-truc.net/0.9.2/api/a00001.html

Error is same as above: json.hpp(1038): error C2338: could not find to_json() method in T's namespace

From: Niels Lohmann Sent: Saturday, March 18, 2017 12:06 PM To: nlohmann/json Cc: Christoph ; Comment Subject: Re: [nlohmann/json] error C2338: could not find to_json() method in T's namespace (#501)

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

Chrjstoph avatar Mar 18 '17 11:03 Chrjstoph

@theodelrieu Any ideas on this?

nlohmann avatar Mar 18 '17 11:03 nlohmann

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.

fehmud avatar May 16 '17 18:05 fehmud

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.

theodelrieu avatar May 17 '17 09:05 theodelrieu

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

fehmud avatar May 17 '17 19:05 fehmud

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.

tigerpup avatar Nov 24 '17 13:11 tigerpup

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

gregmarr avatar Nov 24 '17 17:11 gregmarr