Skip to content

when enum is used as key in a map the json format is not an expected format #4378

Description

@ordit1985

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind: bugsolution: wontfixthe issue will not be fixed (either it is impossible or deemed out of scope)

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions