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

Using custom string. #2398

Closed
armatti opened this issue Sep 18, 2020 · 6 comments
Closed

Using custom string. #2398

armatti opened this issue Sep 18, 2020 · 6 comments

Comments

@armatti
Copy link

armatti commented Sep 18, 2020

I've tried using custom containers with nlohmann::json. ObjectType and ArrayType can be swapped, but changing StringType breaks creating json from most types.

Here's a minimal, non-functional string implementation, that builds as long as you don't try to create a json from other types than string literals: https://godbolt.org/z/GTvn8T

If I read correctly nlohmann::detail::detector fails to find a matching to_json function, but I don't understand how changing StringType has anything to do with it. Should StringType be replaceable? Does it then need a custom serializer or what's the trick?

@stale
Copy link

stale bot commented Dec 25, 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 Dec 25, 2020
@stale stale bot removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Dec 28, 2020
@nlohmann
Copy link
Owner

I can reproduce your findings, but not yet understood the error.

@nlohmann
Copy link
Owner

I could create a minimal wrapper around std::string with which I could compile your example:

class my_string {
  public:
    using value_type = std::string::value_type;
    using size_type = std::string::size_type;

    my_string() {}
    my_string(const value_type* s) : _s(s) {}
    my_string(std::initializer_list<value_type> ilist) : _s(ilist) {}
    ~my_string() {}

    const value_type* data() const noexcept {
        return _s.data();
    }

    size_type size() const noexcept {
        return _s.size();
    }

    void clear() noexcept {
        _s.clear();
    }

    void push_back(size_type ch) {
        _s.push_back(ch);
    }

    friend bool operator<(const my_string& lhs, const my_string& rhs)
    {
        return lhs._s < rhs._s;
    }

  private:
    std::string _s;
};

I am not sure whether it is complete, but it helped me find a problem in your code. After removing the constructor

my_string(char){}

your code compiles.

@armatti
Copy link
Author

armatti commented May 26, 2021

Sorry for not updating the issue sooner. Yes, thanks, removing that constructor does help. Additional gripe was with nlohmann::detail::int_to_string which was solved by defining an overload of the function with the custom string_type.

Looks like int_to_string was reported also in #2059 and commit aeef507 linked as a fix, but not sure how that is supposed to work: https://godbolt.org/z/aEWbM6joo

@gregmarr
Copy link
Contributor

@armatti you're right that the referenced commit does not do what it was intended to do. ADL is for the "arguments" of the function, not the return type. However, looks like you can use ADL to replace this function instead, which is what it sounds like you did.

void int_to_string(my_string& str, std::size_t value)
{
    str = {};
}

FYI @dota17

@armatti
Copy link
Author

armatti commented May 26, 2021

As problems could be solved without modifying nlohmann::json, I'm happy to close this issue.

@armatti armatti closed this as completed May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants