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?
Forum Updated to NodeBB v4.3 + New Features

How to create a QJsonValue from custom type?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.2k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Mozzie
    wrote on 18 Dec 2020, 16:37 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
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 18 Dec 2020, 17:00 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

      M 1 Reply Last reply 19 Dec 2020, 01:49
      3
      • C Christian Ehrlicher
        18 Dec 2020, 17:00

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

        what shoul I do?

        Use QUrl::toString().

        M Offline
        M Offline
        Mozzie
        wrote on 19 Dec 2020, 01:49 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
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 19 Dec 2020, 09:08 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

          M 1 Reply Last reply 20 Dec 2020, 06:04
          2
          • C Christian Ehrlicher
            19 Dec 2020, 09:08

            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.

            M Offline
            M Offline
            Mozzie
            wrote on 20 Dec 2020, 06:04 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.

            K 1 Reply Last reply 20 Dec 2020, 16:19
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 20 Dec 2020, 08:27 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
              • M Mozzie
                20 Dec 2020, 06:04

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

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 20 Dec 2020, 16:19 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

                1/7

                18 Dec 2020, 16:37

                • Login

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