Parse json array to QMatrix4x4












0















I have such an array as json-object:



{
"id":"1",
"Matrix":
[
[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]
]
}


With QJsonDocument, QJsonObject I can already parse the array, but how can I convert it to the QMatrix4x4?



The content of array looks like:



QJsonArray jsonArray = jsonObject["Matrix"].toArray();

Matrix: QJsonArray([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])









share|improve this question



























    0















    I have such an array as json-object:



    {
    "id":"1",
    "Matrix":
    [
    [1,2,3,4],
    [5,6,7,8],
    [9,10,11,12],
    [13,14,15,16]
    ]
    }


    With QJsonDocument, QJsonObject I can already parse the array, but how can I convert it to the QMatrix4x4?



    The content of array looks like:



    QJsonArray jsonArray = jsonObject["Matrix"].toArray();

    Matrix: QJsonArray([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])









    share|improve this question

























      0












      0








      0








      I have such an array as json-object:



      {
      "id":"1",
      "Matrix":
      [
      [1,2,3,4],
      [5,6,7,8],
      [9,10,11,12],
      [13,14,15,16]
      ]
      }


      With QJsonDocument, QJsonObject I can already parse the array, but how can I convert it to the QMatrix4x4?



      The content of array looks like:



      QJsonArray jsonArray = jsonObject["Matrix"].toArray();

      Matrix: QJsonArray([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])









      share|improve this question














      I have such an array as json-object:



      {
      "id":"1",
      "Matrix":
      [
      [1,2,3,4],
      [5,6,7,8],
      [9,10,11,12],
      [13,14,15,16]
      ]
      }


      With QJsonDocument, QJsonObject I can already parse the array, but how can I convert it to the QMatrix4x4?



      The content of array looks like:



      QJsonArray jsonArray = jsonObject["Matrix"].toArray();

      Matrix: QJsonArray([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])






      json qt






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 16 '18 at 10:49









      mystic.06mystic.06

      295




      295
























          1 Answer
          1






          active

          oldest

          votes


















          1














          QMatrix4x4 has two suitable constructors:





          • QMatrix4x4(const float *values):



            To use this constructor, you need to convert your QJsonArray to a data structure that provides a C-compatible floats array:



            auto jsonDoc = QJsonDocument::fromJson(jsonData);
            QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
            std::array<float, 16> myArr;
            for (std::size_t i = 0; i < 16; i++)
            myArr[i] =
            static_cast<float>(jsonArr.at(i / 4).toArray().at(i % 4).toDouble());

            QMatrix4x4 mat(myArr.data());



          • QMatrix4x4(float m11, float m12, ...):



            To use this constructor, you need to pass elements from your QJsonArray as separate arguments to the constructor. You can do that manually, or you can leverage some template meta-programming tricks to make the compiler generate that for you:



            template <std::size_t... indexes>
            QMatrix4x4 jsonArrayToQMatrix4x4Impl(const QJsonArray &jsonArray,
            std::index_sequence<indexes...>) {
            return QMatrix4x4(static_cast<float>(
            jsonArray.at(indexes / 4).toArray().at(indexes % 4).toDouble())...);
            }

            QMatrix4x4 jsonArrayToQMatrix4x4(const QJsonArray &jsonArray) {
            return jsonArrayToQMatrix4x4Impl(jsonArray, std::make_index_sequence<16>{});
            }

            //usage:
            QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
            QMatrix4x4 mat = jsonArrayToQMatrix4x4(jsonArr);





          If you are dealing with different QMatrix sizes, you might want to use this generic adapter:



          template <typename QMatrixType, std::size_t cols, std::size_t... indexes>
          QMatrixType jsonArrayToQMatrixImpl(const QJsonArray &jsonArray,
          std::index_sequence<indexes...> seq) {
          const std::array<float, seq.size()> array{static_cast<float>(
          jsonArray.at(indexes / cols).toArray().at(indexes % cols).toDouble())...};
          return QMatrixType(array.data());
          }

          template <std::size_t cols, std::size_t rows,
          typename QMatrixType = QGenericMatrix<cols, rows, float>>
          QMatrixType jsonArrayToQMatrix(const QJsonArray &jsonArray) {
          return jsonArrayToQMatrixImpl<QMatrixType, cols>(
          jsonArray, std::make_index_sequence<rows * cols>{});
          }

          //usage:
          QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
          QMatrix4x4 mat = jsonArrayToQMatrix<4, 4, QMatrix4x4>(jsonArr);
          QMatrix4x3 mat2 = jsonArrayToQMatrix<4, 3>(jsonArr);




          Please note that the above examples do not check that the input QJsonArray is of a valid size before the conversion. When the QJsonArray has less than the required number of elements, the result QMatrix will have 0s where the missing elements should be.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51875248%2fparse-json-array-to-qmatrix4x4%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            QMatrix4x4 has two suitable constructors:





            • QMatrix4x4(const float *values):



              To use this constructor, you need to convert your QJsonArray to a data structure that provides a C-compatible floats array:



              auto jsonDoc = QJsonDocument::fromJson(jsonData);
              QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
              std::array<float, 16> myArr;
              for (std::size_t i = 0; i < 16; i++)
              myArr[i] =
              static_cast<float>(jsonArr.at(i / 4).toArray().at(i % 4).toDouble());

              QMatrix4x4 mat(myArr.data());



            • QMatrix4x4(float m11, float m12, ...):



              To use this constructor, you need to pass elements from your QJsonArray as separate arguments to the constructor. You can do that manually, or you can leverage some template meta-programming tricks to make the compiler generate that for you:



              template <std::size_t... indexes>
              QMatrix4x4 jsonArrayToQMatrix4x4Impl(const QJsonArray &jsonArray,
              std::index_sequence<indexes...>) {
              return QMatrix4x4(static_cast<float>(
              jsonArray.at(indexes / 4).toArray().at(indexes % 4).toDouble())...);
              }

              QMatrix4x4 jsonArrayToQMatrix4x4(const QJsonArray &jsonArray) {
              return jsonArrayToQMatrix4x4Impl(jsonArray, std::make_index_sequence<16>{});
              }

              //usage:
              QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
              QMatrix4x4 mat = jsonArrayToQMatrix4x4(jsonArr);





            If you are dealing with different QMatrix sizes, you might want to use this generic adapter:



            template <typename QMatrixType, std::size_t cols, std::size_t... indexes>
            QMatrixType jsonArrayToQMatrixImpl(const QJsonArray &jsonArray,
            std::index_sequence<indexes...> seq) {
            const std::array<float, seq.size()> array{static_cast<float>(
            jsonArray.at(indexes / cols).toArray().at(indexes % cols).toDouble())...};
            return QMatrixType(array.data());
            }

            template <std::size_t cols, std::size_t rows,
            typename QMatrixType = QGenericMatrix<cols, rows, float>>
            QMatrixType jsonArrayToQMatrix(const QJsonArray &jsonArray) {
            return jsonArrayToQMatrixImpl<QMatrixType, cols>(
            jsonArray, std::make_index_sequence<rows * cols>{});
            }

            //usage:
            QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
            QMatrix4x4 mat = jsonArrayToQMatrix<4, 4, QMatrix4x4>(jsonArr);
            QMatrix4x3 mat2 = jsonArrayToQMatrix<4, 3>(jsonArr);




            Please note that the above examples do not check that the input QJsonArray is of a valid size before the conversion. When the QJsonArray has less than the required number of elements, the result QMatrix will have 0s where the missing elements should be.






            share|improve this answer






























              1














              QMatrix4x4 has two suitable constructors:





              • QMatrix4x4(const float *values):



                To use this constructor, you need to convert your QJsonArray to a data structure that provides a C-compatible floats array:



                auto jsonDoc = QJsonDocument::fromJson(jsonData);
                QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
                std::array<float, 16> myArr;
                for (std::size_t i = 0; i < 16; i++)
                myArr[i] =
                static_cast<float>(jsonArr.at(i / 4).toArray().at(i % 4).toDouble());

                QMatrix4x4 mat(myArr.data());



              • QMatrix4x4(float m11, float m12, ...):



                To use this constructor, you need to pass elements from your QJsonArray as separate arguments to the constructor. You can do that manually, or you can leverage some template meta-programming tricks to make the compiler generate that for you:



                template <std::size_t... indexes>
                QMatrix4x4 jsonArrayToQMatrix4x4Impl(const QJsonArray &jsonArray,
                std::index_sequence<indexes...>) {
                return QMatrix4x4(static_cast<float>(
                jsonArray.at(indexes / 4).toArray().at(indexes % 4).toDouble())...);
                }

                QMatrix4x4 jsonArrayToQMatrix4x4(const QJsonArray &jsonArray) {
                return jsonArrayToQMatrix4x4Impl(jsonArray, std::make_index_sequence<16>{});
                }

                //usage:
                QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
                QMatrix4x4 mat = jsonArrayToQMatrix4x4(jsonArr);





              If you are dealing with different QMatrix sizes, you might want to use this generic adapter:



              template <typename QMatrixType, std::size_t cols, std::size_t... indexes>
              QMatrixType jsonArrayToQMatrixImpl(const QJsonArray &jsonArray,
              std::index_sequence<indexes...> seq) {
              const std::array<float, seq.size()> array{static_cast<float>(
              jsonArray.at(indexes / cols).toArray().at(indexes % cols).toDouble())...};
              return QMatrixType(array.data());
              }

              template <std::size_t cols, std::size_t rows,
              typename QMatrixType = QGenericMatrix<cols, rows, float>>
              QMatrixType jsonArrayToQMatrix(const QJsonArray &jsonArray) {
              return jsonArrayToQMatrixImpl<QMatrixType, cols>(
              jsonArray, std::make_index_sequence<rows * cols>{});
              }

              //usage:
              QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
              QMatrix4x4 mat = jsonArrayToQMatrix<4, 4, QMatrix4x4>(jsonArr);
              QMatrix4x3 mat2 = jsonArrayToQMatrix<4, 3>(jsonArr);




              Please note that the above examples do not check that the input QJsonArray is of a valid size before the conversion. When the QJsonArray has less than the required number of elements, the result QMatrix will have 0s where the missing elements should be.






              share|improve this answer




























                1












                1








                1







                QMatrix4x4 has two suitable constructors:





                • QMatrix4x4(const float *values):



                  To use this constructor, you need to convert your QJsonArray to a data structure that provides a C-compatible floats array:



                  auto jsonDoc = QJsonDocument::fromJson(jsonData);
                  QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
                  std::array<float, 16> myArr;
                  for (std::size_t i = 0; i < 16; i++)
                  myArr[i] =
                  static_cast<float>(jsonArr.at(i / 4).toArray().at(i % 4).toDouble());

                  QMatrix4x4 mat(myArr.data());



                • QMatrix4x4(float m11, float m12, ...):



                  To use this constructor, you need to pass elements from your QJsonArray as separate arguments to the constructor. You can do that manually, or you can leverage some template meta-programming tricks to make the compiler generate that for you:



                  template <std::size_t... indexes>
                  QMatrix4x4 jsonArrayToQMatrix4x4Impl(const QJsonArray &jsonArray,
                  std::index_sequence<indexes...>) {
                  return QMatrix4x4(static_cast<float>(
                  jsonArray.at(indexes / 4).toArray().at(indexes % 4).toDouble())...);
                  }

                  QMatrix4x4 jsonArrayToQMatrix4x4(const QJsonArray &jsonArray) {
                  return jsonArrayToQMatrix4x4Impl(jsonArray, std::make_index_sequence<16>{});
                  }

                  //usage:
                  QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
                  QMatrix4x4 mat = jsonArrayToQMatrix4x4(jsonArr);





                If you are dealing with different QMatrix sizes, you might want to use this generic adapter:



                template <typename QMatrixType, std::size_t cols, std::size_t... indexes>
                QMatrixType jsonArrayToQMatrixImpl(const QJsonArray &jsonArray,
                std::index_sequence<indexes...> seq) {
                const std::array<float, seq.size()> array{static_cast<float>(
                jsonArray.at(indexes / cols).toArray().at(indexes % cols).toDouble())...};
                return QMatrixType(array.data());
                }

                template <std::size_t cols, std::size_t rows,
                typename QMatrixType = QGenericMatrix<cols, rows, float>>
                QMatrixType jsonArrayToQMatrix(const QJsonArray &jsonArray) {
                return jsonArrayToQMatrixImpl<QMatrixType, cols>(
                jsonArray, std::make_index_sequence<rows * cols>{});
                }

                //usage:
                QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
                QMatrix4x4 mat = jsonArrayToQMatrix<4, 4, QMatrix4x4>(jsonArr);
                QMatrix4x3 mat2 = jsonArrayToQMatrix<4, 3>(jsonArr);




                Please note that the above examples do not check that the input QJsonArray is of a valid size before the conversion. When the QJsonArray has less than the required number of elements, the result QMatrix will have 0s where the missing elements should be.






                share|improve this answer















                QMatrix4x4 has two suitable constructors:





                • QMatrix4x4(const float *values):



                  To use this constructor, you need to convert your QJsonArray to a data structure that provides a C-compatible floats array:



                  auto jsonDoc = QJsonDocument::fromJson(jsonData);
                  QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
                  std::array<float, 16> myArr;
                  for (std::size_t i = 0; i < 16; i++)
                  myArr[i] =
                  static_cast<float>(jsonArr.at(i / 4).toArray().at(i % 4).toDouble());

                  QMatrix4x4 mat(myArr.data());



                • QMatrix4x4(float m11, float m12, ...):



                  To use this constructor, you need to pass elements from your QJsonArray as separate arguments to the constructor. You can do that manually, or you can leverage some template meta-programming tricks to make the compiler generate that for you:



                  template <std::size_t... indexes>
                  QMatrix4x4 jsonArrayToQMatrix4x4Impl(const QJsonArray &jsonArray,
                  std::index_sequence<indexes...>) {
                  return QMatrix4x4(static_cast<float>(
                  jsonArray.at(indexes / 4).toArray().at(indexes % 4).toDouble())...);
                  }

                  QMatrix4x4 jsonArrayToQMatrix4x4(const QJsonArray &jsonArray) {
                  return jsonArrayToQMatrix4x4Impl(jsonArray, std::make_index_sequence<16>{});
                  }

                  //usage:
                  QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
                  QMatrix4x4 mat = jsonArrayToQMatrix4x4(jsonArr);





                If you are dealing with different QMatrix sizes, you might want to use this generic adapter:



                template <typename QMatrixType, std::size_t cols, std::size_t... indexes>
                QMatrixType jsonArrayToQMatrixImpl(const QJsonArray &jsonArray,
                std::index_sequence<indexes...> seq) {
                const std::array<float, seq.size()> array{static_cast<float>(
                jsonArray.at(indexes / cols).toArray().at(indexes % cols).toDouble())...};
                return QMatrixType(array.data());
                }

                template <std::size_t cols, std::size_t rows,
                typename QMatrixType = QGenericMatrix<cols, rows, float>>
                QMatrixType jsonArrayToQMatrix(const QJsonArray &jsonArray) {
                return jsonArrayToQMatrixImpl<QMatrixType, cols>(
                jsonArray, std::make_index_sequence<rows * cols>{});
                }

                //usage:
                QJsonArray jsonArr = jsonDoc.object()["Matrix"].toArray();
                QMatrix4x4 mat = jsonArrayToQMatrix<4, 4, QMatrix4x4>(jsonArr);
                QMatrix4x3 mat2 = jsonArrayToQMatrix<4, 3>(jsonArr);




                Please note that the above examples do not check that the input QJsonArray is of a valid size before the conversion. When the QJsonArray has less than the required number of elements, the result QMatrix will have 0s where the missing elements should be.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 20 '18 at 12:30

























                answered Aug 17 '18 at 9:51









                MikeMike

                5,92511431




                5,92511431






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51875248%2fparse-json-array-to-qmatrix4x4%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    'app-layout' is not a known element: how to share Component with different Modules

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    WPF add header to Image with URL pettitions [duplicate]