增加训练记录json解析错误预防 1017/2
authorCaisking <3449750381@qq.com>
Wed, 25 Sep 2024 12:47:40 +0000 (20:47 +0800)
committerCaisking <3449750381@qq.com>
Wed, 25 Sep 2024 12:47:40 +0000 (20:47 +0800)
code/sdk/application/wearable/fd230app/nativeui/traing/include/TraingModel.h
code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp
code/sdk/application/wearable/fd230app/nativeui/traingrecord/src/TraingRecordModel.cpp
code/sdk/application/wearable/fd230app/nativeui/traingrecord/src/TraingRecordPresenter.cpp

index 2e88ece..2231fc5 100644 (file)
@@ -234,7 +234,7 @@ namespace OHOS
         // 训练存储相关配置
         bool CheckDataJson();                      /*判断json文件是否存在*/
         bool SaveDataToJson(SportData *data);      /*保存训练记录到json文件*/
-        std::vector<SportData> LoadDataFormJson(); /*加载json文件*/
+        bool LoadDataFormJson(std::vector<SportData>& sportDataVector); /*加载json文件*/
         void RecordSportData();                    /*保存数据*/
 
         // 训练数据相关配置
index dc2b49d..3998ebb 100644 (file)
@@ -1627,10 +1627,18 @@ namespace OHOS
             return false;
         }
 
+        // 解析json
+        std::vector<SportData> sportDataVector;
+        if(!LoadDataFormJson(sportDataVector))
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "LoadDataFormJson is error");
+            file.close();
+            return false;
+        }
+
         // 关闭文件
         file.close();
-
-        // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CheckDataJson is ok");
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CheckDataJson is ok");
         return true;
     }
 #if 1
@@ -1780,51 +1788,141 @@ namespace OHOS
         return true;
     }
 #endif
-    /* 加载训练数据函数 */
-    std::vector<SportData> TraingModel::LoadDataFormJson()
+    /* 加载训练数据函数 */  
+    bool TraingModel::LoadDataFormJson(std::vector<SportData>& sportDataVector)
     {
         std::ifstream file(TRAINING_RECORD_FILE);
         if (!file.is_open())
         {
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file");
-            return std::vector<SportData>();
+            return false;
         }
 
         std::string jsonData((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
         file.close();
 
         cJSON *root = cJSON_Parse(jsonData.c_str());
+        if (root == NULL)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to parse JSON: %s", cJSON_GetErrorPtr());
+            return false;
+        }
+
         cJSON *dataArray = cJSON_GetObjectItem(root, "data");
+        if (dataArray == NULL ||!cJSON_IsArray(dataArray))
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid or missing 'data' array in JSON");
+            cJSON_Delete(root);
+            return false;
+        }
 
         int dataCount = cJSON_GetArraySize(dataArray);
+        if (dataCount == 0)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "error dataCount is 0");
+            cJSON_Delete(root);
+            return false;
+        }
+
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "dataCount is %d", dataCount);
 
-        std::vector<SportData> sportDataVector;
         for (int i = 0; i < dataCount; i++)
         {
             cJSON *item = cJSON_GetArrayItem(dataArray, i);
+            if (item == NULL)
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid item at index %d", i);
+                continue; // 跳过无效项目
+            }
+
             SportData sportData;
-            sportData.sportType = (Exercisetype)cJSON_GetObjectItem(item, "sportTypeId")->valueint;
-            sportData.sportId = (UIResourceTextId)cJSON_GetObjectItem(item, "sportId")->valueint;
-            sportData.start_date = cJSON_GetObjectItem(item, "startDate")->valuestring;
-            sportData.start_time = cJSON_GetObjectItem(item, "startTime")->valuestring;
-            sportData.end_time = cJSON_GetObjectItem(item, "endTime")->valuestring;
-            sportData.goaldegree = cJSON_GetObjectItem(item, "goalDegree")->valueint;
-            sportData.duration = cJSON_GetObjectItem(item, "duration")->valueint;
-            sportData.distance = cJSON_GetObjectItem(item, "distance")->valueint;
-            sportData.avgheart = cJSON_GetObjectItem(item, "avgHeartRate")->valueint;
-            sportData.steps = cJSON_GetObjectItem(item, "steps")->valueint;
-            sportData.intensity = cJSON_GetObjectItem(item, "intensity")->valueint;
+            bool isValid = true; // 用于标记 sportData 是否有效
+
+            // 检查并获取每个字段,确保其存在且类型正确
+            if (cJSON *sportTypeIdItem = cJSON_GetObjectItem(item, "sportTypeId"); sportTypeIdItem && cJSON_IsNumber(sportTypeIdItem))
+                sportData.sportType = (Exercisetype)sportTypeIdItem->valueint;
+            else
+                isValid = false;
 
-            sportData.apc = cJSON_GetObjectItem(item, "apc")->valueint;
-            sportData.cdc = cJSON_GetObjectItem(item, "cdc")->valueint;
-            sportData.std = cJSON_GetObjectItem(item, "std")->valueint;
+            if (cJSON *sportIdItem = cJSON_GetObjectItem(item, "sportId"); sportIdItem && cJSON_IsNumber(sportIdItem))
+                sportData.sportId = (UIResourceTextId)sportIdItem->valueint;
+            else
+                isValid = false;
+
+            if (cJSON *startDateItem = cJSON_GetObjectItem(item, "startDate"); startDateItem && cJSON_IsString(startDateItem))
+                sportData.start_date = startDateItem->valuestring;
+            else
+                isValid = false;
 
-            sportDataVector.push_back(sportData);
+            if (cJSON *startTimeItem = cJSON_GetObjectItem(item, "startTime"); startTimeItem && cJSON_IsString(startTimeItem))
+                sportData.start_time = startTimeItem->valuestring;
+            else
+                isValid = false;
+
+            if (cJSON *endTimeItem = cJSON_GetObjectItem(item, "endTime"); endTimeItem && cJSON_IsString(endTimeItem))
+                sportData.end_time = endTimeItem->valuestring;
+            else
+                isValid = false;
+
+            if (cJSON *goalDegreeItem = cJSON_GetObjectItem(item, "goalDegree"); goalDegreeItem && cJSON_IsNumber(goalDegreeItem))
+                sportData.goaldegree = goalDegreeItem->valueint;
+            else
+                isValid = false;
+
+            if (cJSON *durationItem = cJSON_GetObjectItem(item, "duration"); durationItem && cJSON_IsNumber(durationItem))
+                sportData.duration = durationItem->valueint;
+            else
+                isValid = false;
+
+            if (cJSON *distanceItem = cJSON_GetObjectItem(item, "distance"); distanceItem && cJSON_IsNumber(distanceItem))
+                sportData.distance = distanceItem->valueint;
+            else
+                isValid = false;
+
+            if (cJSON *avgHeartRateItem = cJSON_GetObjectItem(item, "avgHeartRate"); avgHeartRateItem && cJSON_IsNumber(avgHeartRateItem))
+                sportData.avgheart = avgHeartRateItem->valueint;
+            else
+                isValid = false;
+
+            if (cJSON *stepsItem = cJSON_GetObjectItem(item, "steps"); stepsItem && cJSON_IsNumber(stepsItem))
+                sportData.steps = stepsItem->valueint;
+            else
+                isValid = false;
+
+            if (cJSON *intensityItem = cJSON_GetObjectItem(item, "intensity"); intensityItem && cJSON_IsNumber(intensityItem))
+                sportData.intensity = intensityItem->valueint;
+            else
+                isValid = false;
+
+            if (cJSON *apcItem = cJSON_GetObjectItem(item, "apc"); apcItem && cJSON_IsNumber(apcItem))
+                sportData.apc = apcItem->valueint;
+            else
+                isValid = false;
+
+            if (cJSON *cdcItem = cJSON_GetObjectItem(item, "cdc"); cdcItem && cJSON_IsNumber(cdcItem))
+                sportData.cdc = cdcItem->valueint;
+            else
+                isValid = false;
+
+            if (cJSON *stdItem = cJSON_GetObjectItem(item, "std"); stdItem && cJSON_IsNumber(stdItem))
+                sportData.std = stdItem->valueint;
+            else
+                isValid = false;
+
+            // 只有在所有字段都有效的情况下才添加 sportData 到向量中
+            if (isValid)
+            {
+                sportDataVector.push_back(sportData);
+            }
+            else
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid data in item %d", i);
+            }
         }
 
         cJSON_Delete(root);
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "LoadDataFormJson is ok");
-        return sportDataVector;
+        return true;
     }
+
 }
\ No newline at end of file
index 6c82ded..a43fb90 100644 (file)
@@ -60,7 +60,12 @@ namespace OHOS
     void TraingRecordModel::InitTraingRecordListItem()
     {
         // 1.读取json数据
-        std::vector<SportData> sportDataVector = TraingModel::GetInstance()->LoadDataFormJson();
+        std::vector<SportData> sportDataVector;
+        if (!TraingModel::GetInstance()->LoadDataFormJson(sportDataVector))
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "LoadDataFormJson failed");
+            return;
+        }
         if (!sportDataVector.empty())
         {
             ArrayNum = sportDataVector.size();
index 09f7070..8f96de0 100644 (file)
@@ -133,7 +133,12 @@ namespace OHOS
             int index = p_TraingRecordItemView->GetTraingRecordIndex();
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "the index is %d", index);
             // 读取json数据
-            std::vector<SportData> sportDataVector = TraingModel::GetInstance()->LoadDataFormJson();
+            std::vector<SportData> sportDataVector;
+            if (!TraingModel::GetInstance()->LoadDataFormJson(sportDataVector))
+            {
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "LoadDataFormJson failed");
+                return false;
+            }
             if (!sportDataVector.empty())
             {
                 // 选择点击按键的历史数据