Skip to content

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

Closed

Description

@kohsh

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.

Activity

  1. nlohmann commented on Oct 7, 2017

    @nlohmann
    Owner

    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.

  2. kohsh commented on Oct 7, 2017

    @kohsh
    Author

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions