-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Closed
Closed
Copy link
Labels
kind: bugsolution: wontfixthe issue will not be fixed (either it is impossible or deemed out of scope)the issue will not be fixed (either it is impossible or deemed out of scope)
Description
Description
I would expect map to be parsed as json objects eg.
{"aa":"completed","bb":"running"}
In fact this json format is dependent on the key type. When the key type is string, the json is created as expected.
When the key type is an enum, the map is parsed as json array eg.
[["stopped","aa"],["completed","bb"]]
Reproduction steps
#include <iostream>
#include <string>
#include <map>
#include "common/include/json/json.h"
using njson = nlohmann::json;
// example enum type declaration
enum TaskState {
TS_STOPPED,
TS_RUNNING,
TS_COMPLETED,
TS_INVALID=-1,
};
// map TaskState values to JSON as strings
NLOHMANN_JSON_SERIALIZE_ENUM( TaskState, {
{TS_INVALID, nullptr},
{TS_STOPPED, "stopped"},
{TS_RUNNING, "running"},
{TS_COMPLETED, "completed"},
})
int main(int argc, char ** argv) {
std::map<TaskState, std::string> t1 = {{TS_STOPPED, "aa"}, {TS_COMPLETED, "bb"}};
std::map<std::string, TaskState> t2 = {{"aa", TS_COMPLETED}, {"bb", TS_RUNNING}};
printf("enum_in_key_nohlman: %s\n", njson(t1).dump().c_str());
printf("enum_in_val_nohlman: %s\n", njson(t2).dump().c_str());
// when enum is in key the json format changes to unexpected json format.
// enum_in_key_nohlman: [["stopped","aa"],["completed","bb"]]
// enum_in_val_nohlman: {"aa":"completed","bb":"running"}
}
Expected vs. actual results
I would expect both cases to look in same format:
enum_in_key_nohlman: {"stopped","aa","completed","bb"}
enum_in_val_nohlman: {"aa":"completed","bb":"running"}
Minimal code example
#include <iostream>
#include <string>
#include <map>
#include "common/include/json/json.h"
using njson = nlohmann::json;
// example enum type declaration
enum TaskState {
TS_STOPPED,
TS_RUNNING,
TS_COMPLETED,
TS_INVALID=-1,
};
// map TaskState values to JSON as strings
NLOHMANN_JSON_SERIALIZE_ENUM( TaskState, {
{TS_INVALID, nullptr},
{TS_STOPPED, "stopped"},
{TS_RUNNING, "running"},
{TS_COMPLETED, "completed"},
})
int main(int argc, char ** argv) {
std::map<TaskState, std::string> t1 = {{TS_STOPPED, "aa"}, {TS_COMPLETED, "bb"}};
std::map<std::string, TaskState> t2 = {{"aa", TS_COMPLETED}, {"bb", TS_RUNNING}};
printf("enum_in_key_nohlman: %s\n", njson(t1).dump().c_str());
printf("enum_in_val_nohlman: %s\n", njson(t2).dump().c_str());
// when enum is in key the json format changes to unexpected json format.
// enum_in_key_nohlman: [["stopped","aa"],["completed","bb"]]
// enum_in_val_nohlman: {"aa":"completed","bb":"running"}
}
Error messages
unexpected format
enum_in_key_nohlman: [["stopped","aa"],["completed","bb"]]
Compiler and operating system
linux
Library version
3.6.1
Validation
- The bug also occurs if the latest version from the
develop
branch is used. - I can successfully compile and run the unit tests.
Metadata
Metadata
Assignees
Labels
kind: bugsolution: wontfixthe issue will not be fixed (either it is impossible or deemed out of scope)the issue will not be fixed (either it is impossible or deemed out of scope)