Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing the default format for unordered_map (or other set) #2132

Closed
1 task done
Enduriel opened this issue May 22, 2020 · 6 comments
Closed
1 task done

Changing the default format for unordered_map (or other set) #2132

Enduriel opened this issue May 22, 2020 · 6 comments
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@Enduriel
Copy link

What do you want to achieve?

I'm looking to use the library to save the state of a game simulation I'm working on. My knowledge of C++ is fairly basic (have only just begun shifting over from C# and Java) and I have been unable to find anyone else looking to achieve the same thing so I'm turning to here.

I'm trying to keep the save files easily and quickly readable with reasonable success. My only issue is with the way maps are converted, and I was wondering if there was a way to change the format without having to manually define a different from and to _json for every type.

When saving the unordered_map instead of having it a map as

[
        [
            "food",
            5.0
        ],
        [
            "amenities",
            10.0
        ]
]

is there an easy way to format this to

        ["food": 5.0],
        ["amenities": 10.0],

for all key value combinations? I have been unable to find anything about this anywhere so finally decided to reach out here as I've seen people be very helpful regarding questions.

  • Compiler: VS 2019 C++17
  • Operating system: Windows 10
  • latest release version 3.7.3
@nlohmann
Copy link
Owner

What is the exact type of which the serialization is

[
        [
            "food",
            5.0
        ],
        [
            "amenities",
            10.0
        ]
]

?

@Enduriel
Copy link
Author

Enduriel commented May 22, 2020

void to_json(json& j, const Resource& resource) {
    j = resource.content;
}
//within Resource
std::unordered_map<ResourceType, double> content;

and ResourceType is an enum where I have set up enum to string (rather than int) serialization, to clarify though, this was still the formatting when the enum was represented as an int.

EDIT: I'm pretty tired and my brain is clearly not functioning properly: To state it simply, that is a serialization of the unordered_map content shown above.

@nlohmann
Copy link
Owner

I see. Since the keys are not convertible to std::string, the map is translated into a list of pairs. You could try two things:

  • Define an implicit conversion of ResourceType to std::string. This could (haven't tested) yield to a conversion to a JSON object.
  • Define a translation for std::unordered_map<ResourceType, double> via an adl_serializer, see https:/nlohmann/json#how-do-i-convert-third-party-types.

@Enduriel
Copy link
Author

Enduriel commented May 24, 2020

The problem with option 2 is that from what I understand I would have to do that for every new way I stored objects together so 1 sounds a lot more viable, but I can't find any way to set up implicit enum to std::string conversion anywhere online.

@nlohmann
Copy link
Owner

Sorry, I forgot that it's not possible to provide a operator std::string() to an enum. So you may be left with option 2.

@stale
Copy link

stale bot commented Jun 26, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jun 26, 2020
@stale stale bot closed this as completed Jul 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

2 participants