Skip to content

Commit

Permalink
Try to remove unwanted spaces in JSON output
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Gervais <[email protected]>
  • Loading branch information
g-maxime committed Oct 10, 2023
1 parent 7bed288 commit 23247a2
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions Source/Common/ProcessFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//---------------------------------------------------------------------------
#include "Output.h"
#include "ZenLib/Ztring.h"
#include <functional>
#include <cfloat>
#include <chrono>
#include <cmath>
Expand Down Expand Up @@ -78,6 +79,20 @@ string JSON_Encode (const string& Data)
return Result;
}

//---------------------------------------------------------------------------
string Trim(string Data) {

Data.erase(Data.begin(), std::find_if(Data.begin(), Data.end(), [](unsigned char c) {
return !std::isspace(c);
}));

Data.erase(std::find_if(Data.rbegin(), Data.rend(), [](unsigned char c) {
return !std::isspace(c);
}).base(), Data.end());

return Data;
}

//***************************************************************************
// Callback
//***************************************************************************
Expand Down Expand Up @@ -186,7 +201,14 @@ void file::Parse(const String& FileName)
auto DeviceName = SimulatorWrapper::GetDeviceName(i);
auto DeviceID = i; // Use index as deviceID for simulator
if (Device_Command == 4) //JSON
cout << (Count++ ? "," : "") << "{\"id\":\"" << DeviceID << "\",\"name\":\"" << JSON_Encode(DeviceName) << "\",\"type\":\"" << JSON_Encode(Interface) << "\"}";
cout << (Count++ ? "," : "")
<< "{\"id\":\""
<< DeviceID
<< "\",\"name\":\""
<< JSON_Encode(Trim(DeviceName))
<< "\",\"type\":\""
<< JSON_Encode(Trim(Interface))
<< "\"}";
else
cout << DeviceID << ": " << DeviceName << " [" << Interface << "]" << '\n';
}
Expand All @@ -200,7 +222,14 @@ void file::Parse(const String& FileName)
if (DeviceID.empty())
continue;
if (Device_Command == 4) //JSON
cout << (Count++ ? "," : "") << "{\"id\":\"" << JSON_Encode(DeviceID) << "\",\"name\":\"" << JSON_Encode(DeviceName) << "\",\"type\":\"" << JSON_Encode(Interface) << "\"}";
cout << (Count++ ? "," : "")
<< "{\"id\":\""
<< JSON_Encode(Trim(DeviceID))
<< "\",\"name\":\""
<< JSON_Encode(Trim(DeviceName))
<< "\",\"type\":\""
<< JSON_Encode(Trim(Interface))
<< "\"}";
else
cout << DeviceID << ": " << DeviceName << " [" << Interface << "]" << '\n';
}
Expand All @@ -215,7 +244,11 @@ void file::Parse(const String& FileName)
if (DeviceID.empty())
continue;
if (Device_Command == 4) //JSON
cout << (Count++ ? "," : "") << "{\"id\":\"" << JSON_Encode(DeviceID) << "\",\"name\":\"" << JSON_Encode(DeviceName) << "\",\"type\":\"" << JSON_Encode(Interface) << "\"}";
cout << (Count++ ? "," : "")
<< "{\"id\":\""
<< JSON_Encode(Trim(DeviceID))
<< "\",\"name\":\""
<< JSON_Encode(Trim(DeviceName)) << "\",\"type\":\"" << JSON_Encode(Trim(Interface)) << "\"}";
else
cout << DeviceID << ": " << DeviceName << " [" << Interface << "]" << '\n';
}
Expand All @@ -229,7 +262,14 @@ void file::Parse(const String& FileName)
if (DeviceID.empty())
continue;
if (Device_Command == 4) //JSON
cout << (Count++ ? "," : "") << "{\"id\":\"" << JSON_Encode(DeviceID) << "\",\"name\":\"" << JSON_Encode(DeviceName) << "\",\"type\":\"" << JSON_Encode(Interface) << "\"}";
cout << (Count++ ? "," : "")
<< "{\"id\":\""
<< JSON_Encode(Trim(DeviceID))
<< "\",\"name\":\""
<< JSON_Encode(Trim(DeviceName))
<< "\",\"type\":\""
<< JSON_Encode(Trim(Interface))
<< "\"}";
else
cout << DeviceID << ": " << DeviceName << " [" << Interface << "]" << '\n';
}
Expand All @@ -250,7 +290,7 @@ void file::Parse(const String& FileName)
if (Name.empty())
break;
if (Device_Command == 6) //JSON
cout << (Count++ ? "," : "") << "{\"name\":\"" << JSON_Encode(Name) << "\"}";
cout << (Count++ ? "," : "") << "{\"name\":\"" << JSON_Encode(Trim(Name)) << "\"}";
else
cout << Name << '\n';
}
Expand Down

0 comments on commit 23247a2

Please sign in to comment.