FIX: the json val could be long long or string

jira: [none]
Change-Id: Ic06276adf5e7663d57ba1c4b6f9a57e897a9075d
This commit is contained in:
xin.zhang 2025-02-08 19:51:49 +08:00 committed by lane.wei
parent 9a9f634e31
commit edaaa0b705
4 changed files with 47 additions and 15 deletions

View File

@ -3704,7 +3704,7 @@ int MachineObject::parse_json(std::string payload, bool key_field_only)
} }
if (jj.contains("job_id")) { if (jj.contains("job_id")) {
is_support_wait_sending_finish = true; is_support_wait_sending_finish = true;
this->job_id_ = jj["job_id"].get<std::string>(); this->job_id_ = JsonValParser::get_longlong_val(jj["job_id"]);
} }
else { else {
is_support_wait_sending_finish = false; is_support_wait_sending_finish = false;
@ -4710,7 +4710,7 @@ int MachineObject::parse_json(std::string payload, bool key_field_only)
BOOST_LOG_TRIVIAL(info) << "parse_json, ack of project_prepare = " << j.dump(4); BOOST_LOG_TRIVIAL(info) << "parse_json, ack of project_prepare = " << j.dump(4);
if (m_agent) { if (m_agent) {
if (jj.contains("job_id")) { if (jj.contains("job_id")) {
this->job_id_ = jj["job_id"].get<std::string>(); this->job_id_ = JsonValParser::get_longlong_val(jj["job_id"]);
} }
} }
@ -7433,4 +7433,28 @@ void change_the_opacity(wxColour& colour)
colour = wxColour(colour.Red(), colour.Green(), colour.Blue(), 254); colour = wxColour(colour.Red(), colour.Green(), colour.Blue(), 254);
} }
} }
//************************************
// Method: get_longlong_val
// FullName: Slic3r::JsonValParser::get_longlong_val
// Access: public static
// Returns: std::string
// Qualifier:
// Parameter: const json & j
//************************************
std::string JsonValParser::get_longlong_val(const json& j)
{
if (j.is_number())
{
return std::to_string(j.get<long long>());
}
else if (j.is_string())
{
return j.get<std::string>();
}
return string();
}
} // namespace Slic3r } // namespace Slic3r

View File

@ -1367,6 +1367,14 @@ public:
void change_the_opacity(wxColour& colour); void change_the_opacity(wxColour& colour);
wxString generate_nozzle_id(NozzleVolumeType nozzle_type); wxString generate_nozzle_id(NozzleVolumeType nozzle_type);
} // namespace Slic3r
class JsonValParser
{
public:
static std::string get_longlong_val(const json& j);
};
}; // namespace Slic3r
#endif // slic3r_DeviceManager_hpp_ #endif // slic3r_DeviceManager_hpp_

View File

@ -34,7 +34,7 @@ int get_hms_info_version(std::string& version)
try { try {
json j = json::parse(body); json j = json::parse(body);
if (j.contains("ver")) { if (j.contains("ver")) {
version = std::to_string(j["ver"].get<long long>()); version = JsonValParser::get_longlong_val(j["ver"]);
} }
} catch (...) { } catch (...) {
; ;
@ -87,7 +87,7 @@ int HMSQuery::download_hms_related(const std::string& hms_type, const std::strin
return; return;
} }
const std::string& remote_ver = std::to_string(j["ver"].get<long long>()); const std::string& remote_ver = JsonValParser::get_longlong_val(j["ver"]);
if (remote_ver <= local_version) if (remote_ver <= local_version)
{ {
return; return;
@ -135,12 +135,12 @@ _copy_dir(const fs::path& from_dir, const fs::path& to_dir) /* copy and override
{ {
return; return;
} }
if (!fs::exists(to_dir)) if (!fs::exists(to_dir))
{ {
fs::create_directory(to_dir); fs::create_directory(to_dir);
} }
for (const auto &entry : fs::directory_iterator(from_dir)) for (const auto &entry : fs::directory_iterator(from_dir))
{ {
const fs::path &source_path = entry.path(); const fs::path &source_path = entry.path();
@ -148,7 +148,7 @@ _copy_dir(const fs::path& from_dir, const fs::path& to_dir) /* copy and override
const fs::path &dest_path = to_dir / relative_path; const fs::path &dest_path = to_dir / relative_path;
if (fs::is_regular_file(source_path)) if (fs::is_regular_file(source_path))
{ {
if (fs::exists(dest_path)) if (fs::exists(dest_path))
{ {
fs::remove(dest_path); fs::remove(dest_path);
@ -164,7 +164,7 @@ _copy_dir(const fs::path& from_dir, const fs::path& to_dir) /* copy and override
} }
catch (...) catch (...)
{ {
} }
} }
@ -209,8 +209,8 @@ int HMSQuery::load_from_local(const std::string& hms_type, const std::string& de
if ((*load_json).contains("ver")) if ((*load_json).contains("ver"))
{ {
load_version = (*load_json)["ver"].get<std::string>(); load_version = JsonValParser::get_longlong_val((*load_json)["ver"]);
} }
else else
{ {
BOOST_LOG_TRIVIAL(warning) << "HMS: load_from_local, no version info"; BOOST_LOG_TRIVIAL(warning) << "HMS: load_from_local, no version info";
@ -221,7 +221,7 @@ int HMSQuery::load_from_local(const std::string& hms_type, const std::string& de
json_file >> (*load_json); json_file >> (*load_json);
if ((*load_json).contains("version")) if ((*load_json).contains("version"))
{ {
load_version = (*load_json)["version"].get<std::string>(); load_version = JsonValParser::get_longlong_val((*load_json)["version"]);
} }
else else
{ {
@ -534,7 +534,7 @@ wxImage HMSQuery::query_image_from_local(const wxString& image_name)
{ {
const fs::path& local_img_dir = fs::path(Slic3r::data_dir()) / HMS_LOCAL_IMG_PATH; const fs::path& local_img_dir = fs::path(Slic3r::data_dir()) / HMS_LOCAL_IMG_PATH;
if (fs::exists(local_img_dir)) if (fs::exists(local_img_dir))
{ {
for (const auto &entry : fs::directory_iterator(local_img_dir)) for (const auto &entry : fs::directory_iterator(local_img_dir))
{ {
const fs::path& image_path = entry.path(); const fs::path& image_path = entry.path();

View File

@ -484,7 +484,7 @@ void PrintJob::process()
try { try {
job_info_j.parse(job_info); job_info_j.parse(job_info);
if (job_info_j.contains("job_id")) { if (job_info_j.contains("job_id")) {
curr_job_id = job_info_j["job_id"].get<std::string>(); curr_job_id = JsonValParser::get_longlong_val(job_info_j["job_id"]);
} }
BOOST_LOG_TRIVIAL(trace) << "print_job: curr_obj_id=" << curr_job_id; BOOST_LOG_TRIVIAL(trace) << "print_job: curr_obj_id=" << curr_job_id;