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

[Question] Custom StringType template parameter (possibility for a KeyType template parameter) #772

Closed
kohsh opened this issue Oct 7, 2017 · 2 comments

Comments

@kohsh
Copy link

kohsh commented Oct 7, 2017

I've been using this library quite a bit but am encountering one minor issue when using the dump() routine. I understand that the default std::map<StringType, basic_json> ordering used for iteratiors is lexicographical and that this is the ordering used for output in dump(). This is a direct result of std::less being used for the comparison functor as part of the ObjectType template parameter list.

Ideally, it would be nice to have a KeyType template parameter which complies with the default constructor for StringType but otherwise only needs to implement the operator<() as required for std::map.

Ideally i'd like my "KeyType" just to be as follows:
struct OrderedKey
{
std::string keyName;
unsigned int keyOrder;
};

comparison and "toString" operators would be defined for this custom key as well.

My question: Is it possible that this could be a feature in the foreseeable future? Or am I over thinking this? Essentially i just want to be able to define the order in which my keys are printed in dump() without rewriting the dump command.

Thanks.

@nlohmann
Copy link
Owner

nlohmann commented Oct 7, 2017

You may want to check the last bullet point of https://github.com/nlohmann/json#notes: you may pass a different type for the objects to achieve a different ordering in the objects.

@kohsh
Copy link
Author

kohsh commented Oct 7, 2017

Fantastic! I'll give this a go and see if it fits my use case. Thanks for the quick response. I'll mark this issue as closed for now.

@kohsh kohsh closed this as completed Oct 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants