Optimize JSON output format and type annotations

Refactored the dumpConfig function in C++ to output valid JSON format by changing brackets and refining the argument-value pair structure. This corrects the previous invalid JSON syntax and ensures compatibility with JSON parsers. In Python, enhanced type safety by explicitly annotating in_arg as a dictionary and streamlined parameter initialization by iterating over items directly. This change improves code readability and reduces potential errors during parameter assignment.

Fixes issue with JSON serialization and parameter handling.
This commit is contained in:
Kumi 2024-02-05 12:57:14 +01:00
parent a4d109dffd
commit cd76cb4300
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 6 additions and 6 deletions

View file

@ -265,7 +265,7 @@ void dumpConfig(Config2HDFattr_t *config, size_t size) {
int ii_oupargs = 0; // TODO: Better name
std::cout << "[" << std::endl;
std::cout << "{" << std::endl;
for (size_t i = 0; i < size; ++i) {
@ -280,7 +280,7 @@ void dumpConfig(Config2HDFattr_t *config, size_t size) {
// Turn argument-value pairs to JSON objects
std::cout << "{\"" << arg << "\": \"";
std::cout << "\"" << arg << "\": \"";
// Need to cast void* data pointer to the correct type
// TODO: Do we lose precision here?
@ -294,7 +294,7 @@ void dumpConfig(Config2HDFattr_t *config, size_t size) {
std::cout << static_cast<char *>(config[i].Value);
}
std::cout << "\"}";
std::cout << "\"";
if (i < size - 1) {
std::cout << ",";
@ -303,7 +303,7 @@ void dumpConfig(Config2HDFattr_t *config, size_t size) {
std::cout << std::endl;
}
std::cout << "]" << std::endl;
std::cout << "}" << std::endl;
}
int main(int argc, char **argv) {

View file

@ -40,8 +40,8 @@ class limr:
in_arg = json.loads(p.stdout.read().decode("utf-8"))
# initialize the parameters
for key in in_arg:
setattr(self, key, in_arg[key][0])
for key, value in in_arg.items():
setattr(self, key, value)
# initialize other variables
self.parvar = {}