Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to create a QJsonValue from custom type?



  • Hi,
    Here is what I want to do:
    first declare a class that can convert to QVariant
    and then use QJsonValue::fromVariant to convert QVariant to a json value

    the first step is declare a class and use the Q_DECLARE_METATYPE macro, right?
    but when after I run a code like:

    qDebug() << QJsonValue::fromVariant(v);
    

    I find it can not show the properties in my class;

    if I want to implement such as :

    		QUrl url("https://www.jd.com/login");
    		QUrlQuery query;
    		query.addQueryItem("username", "admin");
    		query.addQueryItem("password", "123456789");
    		url.setQuery(query);
    
    		QJsonValue jsonVal = QJsonValue::fromVariant(QVariant(url));
    		qDebug() << jsonVal;
    		//QJsonValue(string, "https://www.jd.com/login?username=admin&password=123456789")
    

    for my custom class.

    what shoul I do? Thanks!


  • Lifetime Qt Champion



  • @Christian-Ehrlicher

    Hi,my class is

    #ifndef DATA_H
    #define DATA_H
    
    
    #include <QObject>
    #include <QJsonDocument>
    #include <QJsonObject>
    #include <QVariant>
    
    class Data
    {
    	int id = 0;
    	QString name;
    public:
    	Data();
    	Data(const Data&);
    	~Data();
    
    	Data& operator =(const Data&);
    
    	inline void setId(int id) {this->id = id;}
    	inline int getId() const {return this->id;}
    
    	inline void setName(const QString& name) {this->name = name;}
    	inline const QString& getName() const {return this->name;}
    
    	inline QString toString() const {
    		QJsonObject jsonObj;
    		jsonObj.insert("id", id);
    		jsonObj.insert("name", name);
    		return QString(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact));
    	}
    
    	operator QVariant() const {
    		return QVariant::fromValue(*this);
    	}
    	operator QString() const {
    		return toString();
    	}
    };
    
    Q_DECLARE_METATYPE(Data)
    
    #endif // DATA_H
    
    

    but when i call

    QJsonValue jsonVal = QJsonValue::fromVariant(QVariant(Data()));
    

    It do not use toString()


  • Lifetime Qt Champion

    I'm unsure if this is really supported. Simply provide a toJson() instead a toString() and call this function when you need a json representation is much easier.



  • @Christian-Ehrlicher
    thanks.
    I am trying to find is there any way to serializes a custom type to json by QVariant or Qt meta object system. if you have any suggestions, i would be pretty appreciate.


  • Lifetime Qt Champion

    I don't see a way but I also don't see a benefit.


  • Moderators

    @Mozzie said in How to create a QJsonValue from custom type?:

    I am trying to find is there any way to serializes a custom type to json by QVariant or Qt meta object system. if you have any suggestions, i would be pretty appreciate.

    Not directly, because JSON has a very limited number of types it supports (there isn't even integers). So it doesn't make sense even to ponder about this. If you need to transfer something through JSON, then you need to wrap your own serialize/deserialize code for your types to comply with the standard.


Log in to reply