nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.25k stars 6.57k forks source link

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

Open ordit1985 opened 1 month ago

ordit1985 commented 1 month ago

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

Abastien1734 commented 3 weeks ago

If I wanted to work on this, would I just fork and then submit a PR? Do I need to be assigned to work on this?

t-b commented 3 weeks ago

If I wanted to work on this, would I just fork and then submit a PR?

Yes!

Do I need to be assigned to work on this?

Nope.