contains_json_pointer.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <iostream>
  2. #include <nlohmann/json.hpp>
  3. using json = nlohmann::json;
  4. int main()
  5. {
  6. // create a JSON value
  7. json j =
  8. {
  9. {"number", 1}, {"string", "foo"}, {"array", {1, 2}}
  10. };
  11. std::cout << std::boolalpha
  12. << j.contains("/number"_json_pointer) << '\n'
  13. << j.contains("/string"_json_pointer) << '\n'
  14. << j.contains("/array"_json_pointer) << '\n'
  15. << j.contains("/array/1"_json_pointer) << '\n'
  16. << j.contains("/array/-"_json_pointer) << '\n'
  17. << j.contains("/array/4"_json_pointer) << '\n'
  18. << j.contains("/baz"_json_pointer) << std::endl;
  19. // out_of_range.106
  20. try
  21. {
  22. // try to use an array index with leading '0'
  23. j.contains("/array/01"_json_pointer);
  24. }
  25. catch (json::parse_error& e)
  26. {
  27. std::cout << e.what() << '\n';
  28. }
  29. // out_of_range.109
  30. try
  31. {
  32. // try to use an array index that is not a number
  33. j.contains("/array/one"_json_pointer);
  34. }
  35. catch (json::parse_error& e)
  36. {
  37. std::cout << e.what() << '\n';
  38. }
  39. }