From 26de05437de785f1d516cff1ef42ebea6cb00869 Mon Sep 17 00:00:00 2001 From: zhou_xq Date: Mon, 13 Jan 2025 21:55:16 +0800 Subject: [PATCH] jsoncpp_to_cjson Signed-off-by: zhou_xq --- bundle.json | 3 +- example/BUILD.gn | 1 - interfaces/innerkits/formatter/BUILD.gn | 2 +- .../formatter/dfx_json_formatter.cpp | 117 ++++++++++------ .../formatter/include/dfx_json_formatter.h | 2 +- .../faultloggerdserver_fuzzer/BUILD.gn | 1 - test/unittest/dump_catcher/BUILD.gn | 2 +- test/unittest/faultloggerd/BUILD.gn | 1 - test/unittest/process_dump/BUILD.gn | 2 - tools/process_dump/BUILD.gn | 4 +- .../dfx_stack_info_json_formatter.cpp | 129 +++++++++--------- .../dfx_stack_info_json_formatter.h | 12 +- 12 files changed, 154 insertions(+), 122 deletions(-) diff --git a/bundle.json b/bundle.json index d9f62999c..3be345658 100644 --- a/bundle.json +++ b/bundle.json @@ -33,7 +33,6 @@ "hitrace", "init", "ipc", - "jsoncpp", "libunwind", "libuv", "lzma", @@ -176,4 +175,4 @@ ] } } -} \ No newline at end of file +} diff --git a/example/BUILD.gn b/example/BUILD.gn index 46fc0c879..e759c02b5 100644 --- a/example/BUILD.gn +++ b/example/BUILD.gn @@ -67,7 +67,6 @@ if (defined(ohos_lite)) { external_deps = [ "c_utils:utils", "hilog:libhilog", - "jsoncpp:jsoncpp", ] part_name = "faultloggerd" diff --git a/interfaces/innerkits/formatter/BUILD.gn b/interfaces/innerkits/formatter/BUILD.gn index c7a8b71fd..710e0230e 100644 --- a/interfaces/innerkits/formatter/BUILD.gn +++ b/interfaces/innerkits/formatter/BUILD.gn @@ -36,7 +36,7 @@ if (defined(ohos_lite)) { version_script = "libjson_stack_formatter.map" external_deps = [ "bounds_checking_function:libsec_shared", - "jsoncpp:jsoncpp", + "cJSON:cjson", ] install_images = [ diff --git a/interfaces/innerkits/formatter/dfx_json_formatter.cpp b/interfaces/innerkits/formatter/dfx_json_formatter.cpp index f54b0ec73..f8db6cbbf 100644 --- a/interfaces/innerkits/formatter/dfx_json_formatter.cpp +++ b/interfaces/innerkits/formatter/dfx_json_formatter.cpp @@ -19,7 +19,7 @@ #include #include "dfx_kernel_stack.h" #ifndef is_ohos_lite -#include "json/json.h" +#include "cJSON.h" #endif namespace OHOS { @@ -27,7 +27,27 @@ namespace HiviewDFX { #ifndef is_ohos_lite namespace { const int FRAME_BUF_LEN = 1024; -static bool FormatJsFrame(const Json::Value& frames, const uint32_t& frameIdx, std::string& outStr) +std::string ParseJsonValueToString(const cJSON* jsonObject, const char* key) +{ + if (jsonObject == nullptr || key == nullptr) { + return ""; + } + + cJSON* jsonItem = cJSON_GetObjectItem(jsonObject, key); + if (jsonItem == nullptr) { + return ""; + } + switch ((jsonItem->type) & 0xFF) { + case cJSON_String: + return std::string(jsonItem->valuestring); + case cJSON_Number: + return std::to_string(static_cast(jsonItem->valuedouble)); + default: + return ""; + } +} + +static bool FormatJsFrame(const cJSON* frame, const uint32_t frameIdx, std::string& outStr) { const int jsIdxLen = 10; char buf[jsIdxLen] = { 0 }; @@ -36,32 +56,32 @@ static bool FormatJsFrame(const Json::Value& frames, const uint32_t& frameIdx, s return false; } outStr = std::string(buf, strlen(buf)); - std::string symbol = frames[frameIdx]["symbol"].asString(); + std::string symbol = ParseJsonValueToString(frame, "symbol"); if (!symbol.empty()) { outStr.append(" " + symbol); } - std::string packageName = frames[frameIdx]["packageName"].asString(); + std::string packageName = ParseJsonValueToString(frame, "packageName"); if (!packageName.empty()) { outStr.append(" " + packageName); } - std::string file = frames[frameIdx]["file"].asString(); + std::string file = ParseJsonValueToString(frame, "file"); if (!file.empty()) { - std::string line = frames[frameIdx]["line"].asString(); - std::string column = frames[frameIdx]["column"].asString(); + std::string line = ParseJsonValueToString(frame, "line"); + std::string column = ParseJsonValueToString(frame, "column"); outStr.append(" (" + file + ":" + line + ":" + column + ")"); } return true; } -static bool FormatNativeFrame(const Json::Value& frames, const uint32_t& frameIdx, std::string& outStr) +static bool FormatNativeFrame(const cJSON* frame, const uint32_t& frameIdx, std::string& outStr) { char buf[FRAME_BUF_LEN] = {0}; char format[] = "#%02u pc %s %s"; - std::string buildId = frames[frameIdx]["buildId"].asString(); - std::string file = frames[frameIdx]["file"].asString(); - std::string offset = frames[frameIdx]["offset"].asString(); - std::string pc = frames[frameIdx]["pc"].asString(); - std::string symbol = frames[frameIdx]["symbol"].asString(); + std::string buildId = ParseJsonValueToString(frame, "buildId"); + std::string file = ParseJsonValueToString(frame, "file"); + std::string offset = ParseJsonValueToString(frame, "offset"); + std::string pc = ParseJsonValueToString(frame, "pc"); + std::string symbol = ParseJsonValueToString(frame, "symbol"); if (snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, frameIdx, pc.c_str(), file.empty() ? "Unknown" : file.c_str()) <= 0) { return false; @@ -77,41 +97,51 @@ static bool FormatNativeFrame(const Json::Value& frames, const uint32_t& frameId } } -bool DfxJsonFormatter::FormatJsonStack(std::string jsonStack, std::string& outStackStr) +bool DfxJsonFormatter::FormatJsonStack(const std::string& jsonStack, std::string& outStackStr) { - Json::Reader reader; - Json::Value threads; - if (!(reader.parse(jsonStack, threads))) { + cJSON* threads = cJSON_Parse(jsonStack.c_str()); + if (!threads) { outStackStr.append("Failed to parse json stack info."); return false; } - for (uint32_t i = 0; i < threads.size(); ++i) { + cJSON* thread = nullptr; + for (int i = 0; i < cJSON_GetArraySize(threads); ++i) { std::string ss; - Json::Value thread = threads[i]; - if (thread["tid"].isConvertibleTo(Json::stringValue) && - thread["thread_name"].isConvertibleTo(Json::stringValue)) { - ss += "Tid:" + thread["tid"].asString() + ", Name:" + thread["thread_name"].asString() + "\n"; + thread = cJSON_GetArrayItem(threads, i); + std::string tid = ParseJsonValueToString(thread, "tid"); + std::string threadName = ParseJsonValueToString(thread, "thread_name"); + if (!tid.empty() && !threadName.empty()) { + ss += "Tid:" + tid + ", Name:" + threadName + "\n"; } - const Json::Value frames = thread["frames"]; - for (uint32_t j = 0; j < frames.size(); ++j) { + cJSON* frames = cJSON_GetObjectItem(thread, "frames"); + for (int j = 0; j < cJSON_GetArraySize(frames); ++j) { std::string frameStr = ""; bool formatStatus = false; - if (frames[j]["line"].asString().empty()) { - formatStatus = FormatNativeFrame(frames, j, frameStr); + cJSON* frame = cJSON_GetArrayItem(frames, j); + if (frame == nullptr) { + outStackStr.append("Frame info is illegal."); + cJSON_Delete(threads); + return false; + } + std::string line = ParseJsonValueToString(frame, "line"); + if (line.empty()) { + formatStatus = FormatNativeFrame(frame, j, frameStr); } else { - formatStatus = FormatJsFrame(frames, j, frameStr); + formatStatus = FormatJsFrame(frame, j, frameStr); } if (formatStatus) { ss += frameStr + "\n"; } else { // Shall we try to print more information? outStackStr.append("Frame info is illegal."); + cJSON_Delete(threads); return false; } } outStackStr.append(ss); } + cJSON_Delete(threads); return true; } @@ -144,30 +174,33 @@ static bool FormatKernelStackJson(std::vector processStack, std: if (processStack.empty()) { return false; } - Json::Value jsonInfo; + cJSON* jsonInfo = cJSON_CreateArray(); for (const auto &threadStack : processStack) { - Json::Value threadInfo; - threadInfo["thread_name"] = threadStack.threadName; - threadInfo["tid"] = threadStack.tid; - Json::Value frames(Json::arrayValue); + cJSON* threadInfo = cJSON_CreateObject(); + cJSON_AddStringToObject(threadInfo, "thread_name", threadStack.threadName.c_str()); + cJSON_AddNumberToObject(threadInfo, "tid", threadStack.tid); + cJSON* frames = cJSON_CreateArray(); for (const auto& frame : threadStack.frames) { - Json::Value frameJson; + cJSON* frameJson = cJSON_CreateObject(); char buf[FRAME_BUF_LEN] = {0}; char format[] = "%016" PRIx64; if (snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, frame.relPc) <= 0) { continue; } - frameJson["pc"] = std::string(buf, strlen(buf)); - frameJson["symbol"] = ""; - frameJson["offset"] = 0; - frameJson["file"] = frame.mapName.empty() ? "Unknown" : frame.mapName; - frameJson["buildId"] = ""; - frames.append(frameJson); + cJSON_AddStringToObject(frameJson, "pc", std::string(buf, strlen(buf)).c_str()); + cJSON_AddStringToObject(frameJson, "symbol", ""); + cJSON_AddNumberToObject(frameJson, "offset", 0); + cJSON_AddStringToObject(frameJson, "file", (frame.mapName.empty() ? "Unknown" : frame.mapName).c_str()); + cJSON_AddStringToObject(frameJson, "buildId", ""); + cJSON_AddItemToArray(frames, frameJson); } - threadInfo["frames"] = frames; - jsonInfo.append(threadInfo); + cJSON_AddItemToObject(threadInfo, "frames", frames); + cJSON_AddItemToArray(jsonInfo, threadInfo); } - formattedStack = Json::FastWriter().write(jsonInfo); + char* jsonInfoChars = cJSON_Print(jsonInfo); + formattedStack = jsonInfoChars; + free(jsonInfoChars); + cJSON_Delete(jsonInfo); return true; } #endif diff --git a/interfaces/innerkits/formatter/include/dfx_json_formatter.h b/interfaces/innerkits/formatter/include/dfx_json_formatter.h index 965ecb646..0c5b5f8ce 100644 --- a/interfaces/innerkits/formatter/include/dfx_json_formatter.h +++ b/interfaces/innerkits/formatter/include/dfx_json_formatter.h @@ -33,7 +33,7 @@ public: * @param outStackStr output the stack string * @return bool */ - static bool FormatJsonStack(std::string jsonStack, std::string& outStackStr); + static bool FormatJsonStack(const std::string& jsonStack, std::string& outStackStr); /** * @brief Format kernel stack diff --git a/test/fuzztest/faultloggerdserver_fuzzer/BUILD.gn b/test/fuzztest/faultloggerdserver_fuzzer/BUILD.gn index 54f9526c8..eec3307df 100644 --- a/test/fuzztest/faultloggerdserver_fuzzer/BUILD.gn +++ b/test/fuzztest/faultloggerdserver_fuzzer/BUILD.gn @@ -62,7 +62,6 @@ if (defined(ohos_lite)) { "hisysevent:libhisysevent", "hitrace:hitrace_meter", "init:libbegetutil", - "jsoncpp:jsoncpp", ] } diff --git a/test/unittest/dump_catcher/BUILD.gn b/test/unittest/dump_catcher/BUILD.gn index 5c64ca12e..dfb3507d5 100644 --- a/test/unittest/dump_catcher/BUILD.gn +++ b/test/unittest/dump_catcher/BUILD.gn @@ -90,10 +90,10 @@ if (defined(ohos_lite)) { } resource_config_file = "$faultloggerd_path/test/resource/ohos_test.xml" external_deps = [ + "cJSON:cjson", "c_utils:utils", "googletest:gtest_main", "hilog:libhilog", - "jsoncpp:jsoncpp", ] } diff --git a/test/unittest/faultloggerd/BUILD.gn b/test/unittest/faultloggerd/BUILD.gn index 82ab2e57e..4f9767039 100644 --- a/test/unittest/faultloggerd/BUILD.gn +++ b/test/unittest/faultloggerd/BUILD.gn @@ -134,7 +134,6 @@ if (defined(ohos_lite)) { "hisysevent:libhisysevent", "hitrace:hitrace_meter", "init:libbegetutil", - "jsoncpp:jsoncpp", ] if (build_selinux) { diff --git a/test/unittest/process_dump/BUILD.gn b/test/unittest/process_dump/BUILD.gn index b61b37dbf..005fb8489 100644 --- a/test/unittest/process_dump/BUILD.gn +++ b/test/unittest/process_dump/BUILD.gn @@ -99,7 +99,6 @@ if (defined(ohos_lite)) { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", - "jsoncpp:jsoncpp", ] if (support_jsapi) { deps += [ "$faultloggerd_path/test/resource:FaultloggerdJsTest" ] @@ -128,7 +127,6 @@ if (defined(ohos_lite)) { "googletest:gmock_main", "googletest:gtest_main", "hilog:libhilog", - "jsoncpp:jsoncpp", ] } diff --git a/tools/process_dump/BUILD.gn b/tools/process_dump/BUILD.gn index 3f58a1c3b..b004b0fe5 100644 --- a/tools/process_dump/BUILD.gn +++ b/tools/process_dump/BUILD.gn @@ -147,11 +147,11 @@ if (defined(ohos_lite)) { ] external_deps = [ + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "hisysevent:libhisysevent", "init:libbegetutil", - "jsoncpp:jsoncpp", ] part_name = "faultloggerd" @@ -195,12 +195,12 @@ if (defined(ohos_lite)) { ] external_deps = [ + "cJSON:cjson", "c_utils:utils", "hilog:libhilog_base", "hisysevent:libhisysevent", "hitrace:hitrace_meter", "init:libbegetutil", - "jsoncpp:jsoncpp", ] version_script = "processdump.map" install_images = [ diff --git a/tools/process_dump/dfx_stack_info_json_formatter.cpp b/tools/process_dump/dfx_stack_info_json_formatter.cpp index 2c6c5c8bf..527781f78 100644 --- a/tools/process_dump/dfx_stack_info_json_formatter.cpp +++ b/tools/process_dump/dfx_stack_info_json_formatter.cpp @@ -35,15 +35,15 @@ namespace { static const char NATIVE_CRASH_TYPE[] = "NativeCrash"; #ifndef is_ohos_lite -void FillJsFrame(const DfxFrame& frame, Json::Value& jsonInfo) +void FillJsFrame(const DfxFrame& frame, cJSON* jsonInfo) { - Json::Value frameJson; - frameJson["file"] = frame.mapName; - frameJson["packageName"] = frame.packageName; - frameJson["symbol"] = frame.funcName; - frameJson["line"] = frame.line; - frameJson["column"] = frame.column; - jsonInfo.append(frameJson); + cJSON* frameJson = cJSON_CreateObject(); + cJSON_AddStringToObject(frameJson, "file", frame.mapName.c_str()); + cJSON_AddStringToObject(frameJson, "packageName", frame.packageName.c_str()); + cJSON_AddStringToObject(frameJson, "symbol", frame.funcName.c_str()); + cJSON_AddNumberToObject(frameJson, "line", frame.line); + cJSON_AddNumberToObject(frameJson, "column", frame.column); + cJSON_AddItemToArray(jsonInfo, frameJson); } #endif } @@ -51,7 +51,7 @@ void FillJsFrame(const DfxFrame& frame, Json::Value& jsonInfo) bool DfxStackInfoJsonFormatter::GetJsonFormatInfo(bool isDump, std::string& jsonStringInfo) const { #ifndef is_ohos_lite - Json::Value jsonInfo; + cJSON* jsonInfo = cJSON_CreateArray(); if ((process_ == nullptr) || (request_ == nullptr)) { DFXLOGE("GetStackInfo var is null"); return false; @@ -61,72 +61,77 @@ bool DfxStackInfoJsonFormatter::GetJsonFormatInfo(bool isDump, std::string& json } else { GetCrashJsonFormatInfo(jsonInfo); } - jsonStringInfo.append(Json::FastWriter().write(jsonInfo)); + char* jsonInfoChars = cJSON_Print(jsonInfo); + jsonStringInfo.append(jsonInfoChars); + free(jsonInfoChars); + cJSON_Delete(jsonInfo); return true; #endif return false; } #ifndef is_ohos_lite -void DfxStackInfoJsonFormatter::GetCrashJsonFormatInfo(Json::Value& jsonInfo) const +void DfxStackInfoJsonFormatter::GetCrashJsonFormatInfo(cJSON* jsonInfo) const { - jsonInfo["time"] = request_->timeStamp; - jsonInfo["uuid"] = ""; - jsonInfo["crash_type"] = NATIVE_CRASH_TYPE; - jsonInfo["pid"] = process_->processInfo_.pid; - jsonInfo["uid"] = process_->processInfo_.uid; - jsonInfo["app_running_unique_id"] = request_->appRunningId; + cJSON_AddNumberToObject(jsonInfo, "time", request_->timeStamp); + cJSON_AddStringToObject(jsonInfo, "uuid", ""); + cJSON_AddStringToObject(jsonInfo, "crash_type", NATIVE_CRASH_TYPE); + cJSON_AddNumberToObject(jsonInfo, "pid", process_->processInfo_.pid); + cJSON_AddNumberToObject(jsonInfo, "uid", process_->processInfo_.uid); + cJSON_AddStringToObject(jsonInfo, "app_running_unique_id", request_->appRunningId); DfxSignal dfxSignal(request_->siginfo.si_signo); - Json::Value signal; - signal["signo"] = request_->siginfo.si_signo; - signal["code"] = request_->siginfo.si_code; + cJSON* signal = cJSON_CreateObject(); + cJSON_AddNumberToObject(signal, "signo", request_->siginfo.si_signo); + cJSON_AddNumberToObject(signal, "code", request_->siginfo.si_code); + if (dfxSignal.IsAddrAvailable()) { - signal["address"] = StringPrintf("%" PRIX64_ADDR, reinterpret_cast(request_->siginfo.si_addr)); + cJSON_AddStringToObject(signal, "address", + StringPrintf("%" PRIX64_ADDR, reinterpret_cast(request_->siginfo.si_addr)).c_str()); } else { - signal["address"] = ""; + cJSON_AddStringToObject(signal, "address", ""); } - Json::Value exception; - exception["signal"] = signal; - exception["message"] = process_->GetFatalMessage(); + cJSON* exception = cJSON_CreateObject(); + cJSON_AddItemToObject(exception, "signal", signal); + cJSON_AddStringToObject(jsonInfo, "message", process_->GetFatalMessage().c_str()); if (process_->keyThread_ == nullptr) { - exception["thread_name"] = ""; - exception["tid"] = 0; + cJSON_AddStringToObject(exception, "thread_name", ""); + cJSON_AddNumberToObject(exception, "tid", 0); } else { - exception["thread_name"] = process_->keyThread_->threadInfo_.threadName; - exception["tid"] = process_->keyThread_->threadInfo_.tid; + cJSON_AddStringToObject(exception, "thread_name", process_->keyThread_->threadInfo_.threadName.c_str()); + cJSON_AddNumberToObject(exception, "tid", process_->keyThread_->threadInfo_.tid); } - Json::Value frames(Json::arrayValue); + cJSON* frames = cJSON_CreateArray(); if (process_->keyThread_ != nullptr) { FillFrames(process_->keyThread_, frames); } - exception["frames"] = frames; - jsonInfo["exception"] = exception; + cJSON_AddItemToObject(exception, "frames", frames); + cJSON_AddItemToObject(jsonInfo, "exception", exception); // fill other thread info auto otherThreads = process_->GetOtherThreads(); if (otherThreads.size() > 0) { - Json::Value threadsJsonArray(Json::arrayValue); - AppendThreads(otherThreads, threadsJsonArray); - jsonInfo["threads"] = threadsJsonArray; + cJSON* threadsJson = cJSON_CreateArray(); + AppendThreads(otherThreads, threadsJson); + cJSON_AddItemToObject(jsonInfo, "threads", threadsJson); } } -void DfxStackInfoJsonFormatter::GetDumpJsonFormatInfo(Json::Value& jsonInfo) const +void DfxStackInfoJsonFormatter::GetDumpJsonFormatInfo(cJSON* jsonInfo) const { - Json::Value thread; - Json::Value frames(Json::arrayValue); + cJSON* thread = cJSON_CreateObject(); + cJSON* frames = cJSON_CreateArray(); if (process_->keyThread_ == nullptr) { - thread["thread_name"] = ""; - thread["tid"] = 0; + cJSON_AddStringToObject(thread, "thread_name", ""); + cJSON_AddNumberToObject(thread, "tid", 0); } else { - thread["thread_name"] = process_->keyThread_->threadInfo_.threadName; - thread["tid"] = process_->keyThread_->threadInfo_.tid; + cJSON_AddStringToObject(thread, "thread_name", process_->keyThread_->threadInfo_.threadName.c_str()); + cJSON_AddNumberToObject(thread, "tid", process_->keyThread_->threadInfo_.tid); FillFrames(process_->keyThread_, frames); } - thread["frames"] = frames; - jsonInfo.append(thread); + cJSON_AddItemToObject(thread, "frames", frames); + cJSON_AddItemToArray(jsonInfo, thread); // fill other thread info auto otherThreads = process_->GetOtherThreads(); @@ -136,7 +141,7 @@ void DfxStackInfoJsonFormatter::GetDumpJsonFormatInfo(Json::Value& jsonInfo) con } bool DfxStackInfoJsonFormatter::FillFrames(const std::shared_ptr& thread, - Json::Value& jsonInfo) const + cJSON* jsonInfo) const { if (thread == nullptr) { DFXLOGE("FillFrames thread is null"); @@ -158,39 +163,39 @@ bool DfxStackInfoJsonFormatter::FillFrames(const std::shared_ptr& thr return true; } -void DfxStackInfoJsonFormatter::FillNativeFrame(const DfxFrame& frame, Json::Value& jsonInfo) const +void DfxStackInfoJsonFormatter::FillNativeFrame(const DfxFrame& frame, cJSON* jsonInfo) const { - Json::Value frameJson; + cJSON* frameJson = cJSON_CreateObject(); #ifdef __LP64__ - frameJson["pc"] = StringPrintf("%016lx", frame.relPc); + cJSON_AddStringToObject(frameJson, "pc", StringPrintf("%016lx", frame.relPc).c_str()); #else - frameJson["pc"] = StringPrintf("%08llx", frame.relPc); + cJSON_AddStringToObject(frameJson, "pc", StringPrintf("%08llx", frame.relPc).c_str()); #endif if (frame.funcName.length() > MAX_FUNC_NAME_LEN) { DFXLOGD("length of funcName greater than 256 byte, do not report it"); - frameJson["symbol"] = ""; + cJSON_AddStringToObject(frameJson, "symbol", ""); } else { - frameJson["symbol"] = frame.funcName; + cJSON_AddStringToObject(frameJson, "symbol", frame.funcName.c_str()); } - frameJson["offset"] = frame.funcOffset; + cJSON_AddNumberToObject(frameJson, "offset", frame.funcOffset); std::string strippedMapName = frame.mapName; DfxMap::UnFormatMapName(strippedMapName); - frameJson["file"] = strippedMapName; - frameJson["buildId"] = frame.buildId; - jsonInfo.append(frameJson); + cJSON_AddStringToObject(frameJson, "file", strippedMapName.c_str()); + cJSON_AddStringToObject(frameJson, "buildId", frame.buildId.c_str()); + cJSON_AddItemToArray(jsonInfo, frameJson); } void DfxStackInfoJsonFormatter::AppendThreads(const std::vector>& threads, - Json::Value& jsonInfo) const + cJSON* jsonInfo) const { for (auto const& oneThread : threads) { - Json::Value threadJson; - threadJson["thread_name"] = oneThread->threadInfo_.threadName; - threadJson["tid"] = oneThread->threadInfo_.tid; - Json::Value frames(Json::arrayValue); + cJSON* threadJson = cJSON_CreateObject(); + cJSON_AddStringToObject(threadJson, "thread_name", oneThread->threadInfo_.threadName.c_str()); + cJSON_AddNumberToObject(threadJson, "tid", oneThread->threadInfo_.tid); + cJSON* frames = cJSON_CreateArray(); FillFrames(oneThread, frames); - threadJson["frames"] = frames; - jsonInfo.append(threadJson); + cJSON_AddItemToObject(threadJson, "frames", frames); + cJSON_AddItemToArray(jsonInfo, threadJson); } } #endif diff --git a/tools/process_dump/dfx_stack_info_json_formatter.h b/tools/process_dump/dfx_stack_info_json_formatter.h index a95524384..5b5bdf033 100644 --- a/tools/process_dump/dfx_stack_info_json_formatter.h +++ b/tools/process_dump/dfx_stack_info_json_formatter.h @@ -25,7 +25,7 @@ #include "dfx_dump_request.h" #include "dfx_process.h" #ifndef is_ohos_lite -#include "json/json.h" +#include "cJSON.h" #endif namespace OHOS { @@ -41,11 +41,11 @@ public: private: DfxStackInfoJsonFormatter() = delete; #ifndef is_ohos_lite - void GetCrashJsonFormatInfo(Json::Value& jsonInfo) const; - void GetDumpJsonFormatInfo(Json::Value& jsonInfo) const; - bool FillFrames(const std::shared_ptr& thread, Json::Value& jsonInfo) const; - void FillNativeFrame(const DfxFrame& frame, Json::Value& jsonInfo) const; - void AppendThreads(const std::vector>& threads, Json::Value& jsonInfo) const; + void GetCrashJsonFormatInfo(cJSON* jsonInfo) const; + void GetDumpJsonFormatInfo(cJSON* jsonInfo) const; + bool FillFrames(const std::shared_ptr& thread, cJSON* jsonInfo) const; + void FillNativeFrame(const DfxFrame& frame, cJSON* jsonInfo) const; + void AppendThreads(const std::vector>& threads, cJSON* jsonInfo) const; #endif private: -- Gitee