-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Comments
This comment has been minimized.
This comment has been minimized.
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. |
This could be done by implementing the 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 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 |
Yes, this works for me. Thank you. |
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 😉 |
I feel the advantage of the first method (nholmann's method) is that you can use both behaviors in the same build as necessary. |
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. |
Hi! Sorry to raise this issue again, but is there some plan to provide this functionality in the future? Thanks. |
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?
The text was updated successfully, but these errors were encountered: