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

Help request #539

Closed
abdessattar opened this issue Mar 30, 2017 · 22 comments
Closed

Help request #539

abdessattar opened this issue Mar 30, 2017 · 22 comments

Comments

@abdessattar
Copy link

Hello,
I want to thank you for the important advice. I get what I want but with simple class.
When I tried your solution with singleton and other specific classes. I got this error "C2338 could not find to _json() method in T's namespace ".
Really, I need your help for advancing as soon as possible.
thank you in advance,
Best regards,

@nlohmann
Copy link
Owner

Can you provide the code you try to compile?

@nlohmann nlohmann added the platform: visual studio related to MSVC label Mar 30, 2017
@abdessattar
Copy link
Author

Ok, but the problem that I work on specifec private project.
You find below the main code.

#include <iostream>
#include <stdio.h>
#include <fstream>
#include "StdAfx.h"
#include <stdlib.h>
#include "C:\Users\abahri\Desktop\visual studio\Project\first part\gui\g4\v5.1.0.0\v5.1.0.0\GUI_Descr.xsd.Output\SourceCodeCPP\GUI_DescrLib.h"
#include "C:\Users\abahri\Desktop\visual studio\Project\first part\gui\g4\v5.1.0.0\v5.1.0.0\GUI_Descr.xsd.Output\SourceCodeCPP\GUI_DescrLib\DataStream.h"
#include "C:\Users\abahri\Desktop\visual studio\Project\first part\gui\g4\v5.1.0.0\v5.1.0.0\GUI_Descr.xsd.Output\SourceCodeCPP\GUI_DescrLib\BaseDir.h"
#include "C:\Users\abahri\Desktop\visual studio\Project\first part\gui\g4\json-develop\json-develop\src\json.hpp"
using namespace LtXmlLib5;
using namespace GUI_DescrLib;
using json = nlohmann::json;

bool SimpleTestGUI_DescrLibCDataStream(LPCTSTR);
int main()
{

	if(SimpleTestGUI_DescrLibCDataStream(_T("<UNCOMMENT & INSERT A SAMPLE XML FILENAME HERE>")));
	std::cout << "hello";
	system("pause");
	return 0;
}


bool SimpleTestGUI_DescrLibCDataStream(LPCTSTR lpctFilename)
{
	GUI_DescrLib_Classes ecls;
	
	LtXmlLib5Data::CParentElementInfo elm = GUI_DescrLib::CClassFactory::getinfoclass(ecls, lpctFilename);
		json j;
		j["ms_pParentElementInfo"] = elm;

			return false;
		
}

@nlohmann
Copy link
Owner

In line j["ms_pParentElementInfo"] = elm;, you try to assign type LtXmlLib5Data::CParentElementInfo to nlohmann::json. Such a conversion is not provided directly by the library (only default types such as strings, numbers, etc. are supported out of the box). Therefore, the library tries to find whether you provided a to_json() function in the namespace of your type (LtXmlLib5Data in your case).

You need to define a method void to_json(json& j, const CParentElementInfo& e) in namespace LtXmlLib5Data where you implement the conversion from a LtXmlLib5Data::CParentElementInfo object e to a nlohmann::json object j. This is documented in https://github.com/nlohmann/json#arbitrary-types-conversions.

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation state: please discuss please discuss the issue or vote for your favorite option labels Mar 30, 2017
@theodelrieu
Copy link
Contributor

We could add support for enum classes too, this is quite trivial, and won't conflict with existing to_json(json, my_enum) methods

@nlohmann
Copy link
Owner

That would be great. Is this related to #513?

@theodelrieu
Copy link
Contributor

Yes it is!

@nlohmann nlohmann removed the state: please discuss please discuss the issue or vote for your favorite option label Mar 31, 2017
@abdessattar
Copy link
Author

abdessattar commented Apr 3, 2017

Is it necessary that the method have two arguments, we cannot define it only with first argument (json)?
Thank you in advance,

@nlohmann
Copy link
Owner

nlohmann commented Apr 3, 2017

No, it has to have two arguments.

@abdessattar
Copy link
Author

abdessattar commented Apr 3, 2017

Because I get the same error, despite I define it with two arguments.
To your mind what is the solution?

@nlohmann
Copy link
Owner

nlohmann commented Apr 3, 2017

Did you try what I proposed in #539 (comment)?

@abdessattar
Copy link
Author

abdessattar commented Apr 3, 2017

yes I tried, you will find my test in this code.
Such as my class is a singleton.

#include "StdAfx.h"
#include <iostream>
#include <stdio.h>
#include "GUI_DescrLib.h"
#include "BaseDir.h"

#include "D:\Sources\serialization_avec_json&GUIdescr2015\json-develop\json-develop\src\json.hpp"
using nlohmann::json;

using namespace LtXmlLib5;
using namespace std;
using namespace GUI_DescrLib;



// forward declarations

void to_json(json& j, const CClassFactory & p, GUI_DescrLib_Classes eCls, LPCTSTR strElementName);

/// <summary>
/// The main entry point for the application.
/// </summary>
int main(int argc, char* argv[])
{

	
	 
	system("pause");
	return 0;
}


void to_json(json& j) {

	GUI_DescrLib_Classes eCls = ClsName_CBaseDir;
	CBaseDirPtr	elm = CBaseDir::CreateInstance();

	j = json{ { "name", CBaseDir::GetparentElement() },{ "address", CBaseDir::Getelementinfo() },{ "age", CBaseDir:: Getattributrinfo() } };
}

@nlohmann
Copy link
Owner

nlohmann commented Apr 3, 2017

You need to define the to_json function in the namespace of the class you want to convert to JSON. It has to have exactly two parameters: json & and a const reference of your class type.

@abdessattar
Copy link
Author

The problem that the class type is a singleton, so Ihave to create one and only one instance of this class.

@nlohmann
Copy link
Owner

nlohmann commented Apr 3, 2017

I don't understand. The to_json function is static.

@abdessattar
Copy link
Author

abdessattar commented Apr 3, 2017

As I said in the previous comment, the classes in my project are singletons and their attributes are static. So can I define thes method in the classes as a static method?
Really I need your help.
Thank you in advance,

@nlohmann
Copy link
Owner

nlohmann commented Apr 3, 2017

I see. Then you cannot use this mechanism. You can define any function that returns a json.

@nlohmann
Copy link
Owner

nlohmann commented Apr 4, 2017

btw: this is a strange implementation of a singleton as you have no instance at all. If you follow approaches like http://stackoverflow.com/questions/1008019/c-singleton-design-pattern you would have exactly one instance and could the approach described in the README.

@abdessattar
Copy link
Author

Good morning,
In which section the approach described?
Thank you in advance,

@nlohmann
Copy link
Owner

nlohmann commented Apr 4, 2017

See #539 (comment)

@abdessattar
Copy link
Author

abdessattar commented Apr 4, 2017

To my mind, Ihave to add my one "adl_serializer", would do you please show an example of it.
Because I copied the example i this link.

template <typename CBaseDir>
struct adl_serializer {
	static void to_json(json& j, const	CBaseDir& value) {
	 tojson(json& j);
	}

};

But I get this error "syntax error;identifier'json'".
Thank you in advance

@nlohmann
Copy link
Owner

nlohmann commented Apr 4, 2017

Did you add using nlohmann::json; to your code.

@theodelrieu
Copy link
Contributor

There is an example with boost::optional in the readme, it seems like your use case

@nlohmann nlohmann removed the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Apr 7, 2017
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

3 participants