Skip to content

Incorrect behaviour of basic_json::count method #78

Closed

Description

@drewpts

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".

Activity

  1. nlohmann commented on May 30, 2015

    @nlohmann
    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).

  2. added
    solution: invalid
    on May 30, 2015
  3. drewpts commented on May 30, 2015

    @drewpts
    Author

    I understood. Sorry.

  4. nlohmann commented on May 30, 2015

    @nlohmann
    Owner

    No worries!

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

    solution: invalid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions