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

Incorrect behaviour of basic_json::count method #78

Closed
drewpts opened this issue May 29, 2015 · 3 comments
Closed

Incorrect behaviour of basic_json::count method #78

drewpts opened this issue May 29, 2015 · 3 comments
Labels
solution: invalid the issue is not related to the library

Comments

@drewpts
Copy link

drewpts commented May 29, 2015

Hello again. :)
If we have code:

#include "json.hpp"
#include <iostream>
using nlohmann::json;
int main()
{
        json k = { {"a", "A"}, {"a", "AA"} };
        std::cout << k.count("a") << '\n';
        return 0;
}

Accordingly to comment:

/// returns the number of occurrences of a key in an object
    inline size_type count(typename object_t::key_type key) const
    {
        // return 0 for all nonobject types
        return (m_type == value_t::object) ? m_value.object->count(key) : 0;
    }

we should got "2" in program output. By I got "1".

@nlohmann
Copy link
Owner

No, 1 is the correct result, because an object is a mapping of keys to values. Hence, a key can only occur once on the mapping.

The standard nlohmann::json uses std::map to implement JSON's object. You could, if you like, use std::multi_map instead. Then, the result would indeed be 2 (to the price that the container is not compliant JSON any more).

@nlohmann nlohmann added the solution: invalid the issue is not related to the library label May 30, 2015
@drewpts
Copy link
Author

drewpts commented May 30, 2015

I understood. Sorry.

@nlohmann
Copy link
Owner

No worries!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

2 participants