Computer Assisted Medical Intervention Tool Kit version 6.0
 
Loading...
Searching...
No Matches
JsonUtils.h
Go to the documentation of this file.
1/*****************************************************************************
2 * $CAMITK_LICENCE_BEGIN$
3 *
4 * CamiTK - Computer Assisted Medical Intervention ToolKit
5 * (c) 2001-2025 Univ. Grenoble Alpes, CNRS, Grenoble INP - UGA, TIMC, 38000 Grenoble, France
6 *
7 * Visit http://camitk.imag.fr for more information
8 *
9 * This file is part of CamiTK.
10 *
11 * CamiTK is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * CamiTK is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
22 *
23 * $CAMITK_LICENCE_END$
24 ****************************************************************************/
25
26#include <QJsonDocument>
27
32class JsonUtils {
33public:
34 static void printJSON(QJsonObject data, bool indented = true) {
35 qDebug().noquote().nospace() << QJsonDocument(data).toJson((indented) ? QJsonDocument::Indented : QJsonDocument::Compact);
36 }
37
45 static QJsonObject stringToJSON(QString dataString, bool print = false) {
46 QJsonParseError jsonParseError;
47 QJsonDocument jsonDoc = QJsonDocument::fromJson(dataString.toUtf8(), &jsonParseError);
48 if (jsonDoc.isNull()) {
49 // extract the string from the error offset to the next return carriage
50 QString jsonErrorData(dataString.mid(jsonParseError.offset, dataString.indexOf("\n", jsonParseError.offset) - jsonParseError.offset));
51 int lineNr = dataString.left(jsonParseError.offset).count('\n');
52 if (lineNr == 0) {
53 qWarning().noquote().nospace() << "Error creating JSON from string \"" << dataString << "\":\n" << jsonParseError.errorString() << ":\n" << jsonErrorData << "\n" << "Please check that the input string is a valid JSON string";
54 }
55 else {
56 QString errorLine(dataString.split('\n')[lineNr - 1]);
57 qWarning().noquote().nospace() << "Error creating JSON from string \"" << dataString << "\":\n" << jsonParseError.errorString() << ":\n" << jsonErrorData << "\nOn line " << lineNr << ":\n" << errorLine;
58 }
59 return QJsonObject();
60 }
61 else if (!jsonDoc.isObject()) {
62 qWarning().noquote().nospace() << "Error creating JSON from string \"" << dataString << "\":\n: JSON document is not an object";
63 return QJsonObject();
64 }
65 else {
66 QJsonObject data = jsonDoc.object();
67 if (print) {
68 printJSON(data);
69 }
70 return data;
71 }
72 }
73};
Utility class.
Definition JsonUtils.h:32
static void printJSON(QJsonObject data, bool indented=true)
Definition JsonUtils.h:34
static QJsonObject stringToJSON(QString dataString, bool print=false)
Creates a QJsonObject from a string.
Definition JsonUtils.h:45