@@ -93,7 +93,9 @@ PURPOSE. See the above copyright notices for more information.
9393#include < QFileInfo>
9494#include < QDir>
9595#include < QTextStream>
96-
96+ #include < QJsonParseError>
97+ #include < QJsonDocument>
98+ #include < QJsonArray>
9799
98100#include " CemrgCommonUtils.h"
99101
@@ -1021,6 +1023,138 @@ mitk::DataNode::Pointer CemrgCommonUtils::AddToStorage(
10211023 return node;
10221024}
10231025
1026+ QJsonObject CemrgCommonUtils::ReadJSONFile (QString dir, QString fname) {
1027+ fname += (!fname.endsWith (" .json" )) ? " .json" : " " ;
1028+ QFile file (dir + " /" + fname);
1029+
1030+ if (!file.open (QIODevice::ReadOnly)) {
1031+ qWarning (" Failed to open file" );
1032+ return QJsonObject ();
1033+ }
1034+
1035+ QByteArray jsonData = file.readAll ();
1036+
1037+ QJsonParseError error;
1038+ QJsonDocument jsonDoc = QJsonDocument::fromJson (jsonData, &error);
1039+
1040+ MITK_WARN ((jsonDoc.isNull ())) << (" Failed to parse JSON: " + error.errorString ()).toStdString ();
1041+
1042+ QJsonObject jsonObj = jsonDoc.object ();
1043+
1044+ file.close ();
1045+
1046+ return jsonObj;
1047+ }
1048+
1049+ bool CemrgCommonUtils::WriteJSONFile (QJsonObject json, QString dir, QString fname) {
1050+ // Create the JSON document
1051+ QJsonDocument jsonDoc (json);
1052+ bool success = true ;
1053+
1054+ // Open the file for writing
1055+ fname += (!fname.endsWith (" .json" )) ? " .json" : " " ;
1056+ QFile file (dir + " /" + fname);
1057+ if (!file.open (QIODevice::WriteOnly | QIODevice::Truncate)) {
1058+ qWarning (" Failed to open file" );
1059+ success = false ;
1060+ }
1061+
1062+ file.write (jsonDoc.toJson ());
1063+ file.close ();
1064+
1065+ return success;
1066+ }
1067+
1068+ bool CemrgCommonUtils::ModifyJSONFile (QString dir, QString fname, QString key, QString value, QString type) {
1069+ bool success = true ;
1070+ QJsonObject json = ReadJSONFile (dir, fname);
1071+
1072+ if (json.empty ()) {
1073+ return false ;
1074+ }
1075+
1076+ if (value.isEmpty ()) {
1077+ json.remove (key);
1078+ } else {
1079+ // Determine the data type of the value
1080+ QJsonValue jsonValue;
1081+ QJsonArray json_array;
1082+ if (type == " string" ) {
1083+ jsonValue = QJsonValue::fromVariant (value);
1084+ }
1085+ else if (type == " int" ) {
1086+ jsonValue = QJsonValue::fromVariant (value.toInt ());
1087+ }
1088+ else if (type == " float" || type == " double" ) {
1089+ jsonValue = QJsonValue::fromVariant (value.toDouble ());
1090+ }
1091+ else if (type == " bool" ) {
1092+ jsonValue = QJsonValue::fromVariant (value.toLower () == " true" );
1093+ }
1094+ else if (type == " array" ) {
1095+ // split value
1096+ QStringList array_values = value.split (" ," );
1097+ for (int ix = 0 ; ix < array_values.size (); ix++) {
1098+ // QJsonValue json_value = array_values.at(ix).toDouble();
1099+ json_array.push_back (array_values.at (ix).toDouble ());
1100+ }
1101+ }
1102+ else {
1103+ // If the data type is not recognized, use a null value
1104+ jsonValue = QJsonValue ();
1105+ }
1106+
1107+ // Add the key-value pair to the object
1108+ json[key] = (type == " array" ) ? json_array : jsonValue;
1109+ }
1110+
1111+ success = WriteJSONFile (json, dir, fname);
1112+ return success;
1113+ }
1114+
1115+ QJsonObject CemrgCommonUtils::CreateJSONObject (QStringList keys_list, QStringList values_list, QStringList types_list) {
1116+ QJsonObject jsonObj;
1117+
1118+ for (int i = 0 ; i < keys_list.size (); i++) {
1119+ QString key = keys_list.at (i);
1120+ QString value = values_list.at (i);
1121+ QString type = types_list.at (i);
1122+
1123+ // Determine the data type of the value
1124+ QJsonValue jsonValue;
1125+ QJsonArray json_array;
1126+ if (type == " string" ) {
1127+ jsonValue = QJsonValue::fromVariant (value);
1128+ }
1129+ else if (type == " int" ) {
1130+ jsonValue = QJsonValue::fromVariant (value.toInt ());
1131+ }
1132+ else if (type == " float" || type == " double" ) {
1133+ jsonValue = QJsonValue::fromVariant (value.toDouble ());
1134+ }
1135+ else if (type == " bool" ) {
1136+ jsonValue = QJsonValue::fromVariant (value.toLower () == " true" );
1137+ }
1138+ else if (type == " array" ) {
1139+ // split value
1140+ QStringList array_values = value.split (" ," );
1141+ for (int ix = 0 ; ix < array_values.size (); ix++) {
1142+ // QJsonValue json_value = array_values.at(ix).toDouble();
1143+ json_array.push_back (array_values.at (ix).toDouble ());
1144+ }
1145+ }
1146+ else {
1147+ // If the data type is not recognized, use a null value
1148+ jsonValue = QJsonValue ();
1149+ }
1150+
1151+ // Add the key-value pair to the object
1152+ jsonObj[key] = (type == " array" ) ? json_array : jsonValue;
1153+ }
1154+
1155+ return jsonObj;
1156+ }
1157+
10241158mitk::Image::Pointer CemrgCommonUtils::ImageFromSurfaceMesh (mitk::Surface::Pointer surf, double origin[3 ], double spacing[3 ]){
10251159 vtkSmartPointer<vtkPolyData> pd = surf->GetVtkPolyData ();
10261160 double bounds[6 ];
0 commit comments