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

Build Option/Separate Function to Allow to Throw on Duplicate Keys #1560

Closed
jayeshbadwaik opened this issue Apr 5, 2019 · 8 comments
Closed

Comments

@jayeshbadwaik
Copy link

In reference to the #375 , I would like to request a compile time option to allow the library to throw on encountering duplicate keys. Or at least a completely separate parse function to do the same?

@jayeshbadwaik jayeshbadwaik changed the title Build Option to Allow to Throw on Duplicate Keys Build Option/Separate Function to Allow to Throw on Duplicate Keys Apr 5, 2019
@nlohmann

This comment has been minimized.

@jayeshbadwaik
Copy link
Author

Apologies for not being clear in my request. I meant encountering duplicate keys on parsing of JSON file. I know the standard does not specify any behaviour. But, it would be good to have a non-default option to parse a JSON and ensure a unique key.

@nlohmann
Copy link
Owner

nlohmann commented Apr 6, 2019

This could be done by implementing the key event of the SAX interface (see https://github.com/nlohmann/json/blob/develop/include/nlohmann/detail/input/json_sax.hpp#L145) differently. It is currently:

bool key(string_t& val)
{
    // add null at given key and store the reference for later
    object_element = &(ref_stack.back()->m_value.object->operator[](val));
    return true;
}

To throw, one would need to check whether val (the key) is already stored in the object we are parsing to (ref_stack.back()).

Here is a complete example:

#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

class throwing_sax : public nlohmann::detail::json_sax_dom_parser<json>
{
  public:
    throwing_sax(json& j) : nlohmann::detail::json_sax_dom_parser<json>(j) {};
    
    bool key(json::string_t& val)
    {
        if (ref_stack.back()->contains(val))
        {
            throw std::invalid_argument("key " + val + " was already stored");
        }
        return nlohmann::detail::json_sax_dom_parser<json>::key(val);
    }
};

int main(int argc, char *argv[])
{
    json j;
    throwing_sax sax_consumer(j);
    
    auto input = "{ \"key\": 1, \"key\": 2 }";
    json::sax_parse(input, &sax_consumer);
    std::cout << j << std::endl;
}

The only problem left is that ref_stack is a private variable of json_sax_dom_parser. If you set it to protected, the code compiles. Would this be an approach that works for you?

@jayeshbadwaik
Copy link
Author

Yes, this works for me. Thank you.

@tete17
Copy link
Contributor

tete17 commented Apr 9, 2019

The solution @nlohmann provided works but we could also implement this into the library itself under a feature macro. I know is not the way of the true c++ but it could work. I leave the proposed solution, let me know if you guys want me to submit a PR.

bool key(string_t& val)
{
#ifdef JSON_THROW_REPEATED_KEY
    if (ref_stack.back()->contains(val))
    {
        JSON_THROW(parse_error::create(115, "found duplicated key: " + val));
    }
#endif
    // add null at given key and store the reference for later 
    object_element = &(ref_stack.back()->m_value.object->operator[](val));
    return true;
}

I would also need to add the 115 to the json parser error docu 😉

@jayeshbadwaik
Copy link
Author

jayeshbadwaik commented Apr 10, 2019

I feel the advantage of the first method (nholmann's method) is that you can use both behaviors in the same build as necessary.

@stale
Copy link

stale bot commented May 10, 2019

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 May 10, 2019
@stale stale bot closed this as completed May 17, 2019
@jayeshbadwaik
Copy link
Author

Hi! Sorry to raise this issue again, but is there some plan to provide this functionality in the future? Thanks.

@nlohmann nlohmann removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Sep 8, 2019
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