Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to create a QJsonValue from custom type?
QtWS25 Last Chance

How to create a QJsonValue from custom type?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MozzieM Offline
    MozzieM Offline
    Mozzie
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      what shoul I do?

      Use QUrl::toString().

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      MozzieM 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

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

        what shoul I do?

        Use QUrl::toString().

        MozzieM Offline
        MozzieM Offline
        Mozzie
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          MozzieM 1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            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.

            MozzieM Offline
            MozzieM Offline
            Mozzie
            wrote on last edited by
            #5

            @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.

            kshegunovK 1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • MozzieM Mozzie

                @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.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by kshegunov
                #7

                @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.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                4

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved