json_pointer.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <iostream>
  2. #include <nlohmann/json.hpp>
  3. using json = nlohmann::json;
  4. int main()
  5. {
  6. // correct JSON pointers
  7. json::json_pointer p1;
  8. json::json_pointer p2("");
  9. json::json_pointer p3("/");
  10. json::json_pointer p4("//");
  11. json::json_pointer p5("/foo/bar");
  12. json::json_pointer p6("/foo/bar/-");
  13. json::json_pointer p7("/foo/~0");
  14. json::json_pointer p8("/foo/~1");
  15. // error: JSON pointer does not begin with a slash
  16. try
  17. {
  18. json::json_pointer p9("foo");
  19. }
  20. catch (json::parse_error& e)
  21. {
  22. std::cout << e.what() << '\n';
  23. }
  24. // error: JSON pointer uses escape symbol ~ not followed by 0 or 1
  25. try
  26. {
  27. json::json_pointer p10("/foo/~");
  28. }
  29. catch (json::parse_error& e)
  30. {
  31. std::cout << e.what() << '\n';
  32. }
  33. // error: JSON pointer uses escape symbol ~ not followed by 0 or 1
  34. try
  35. {
  36. json::json_pointer p11("/foo/~3");
  37. }
  38. catch (json::parse_error& e)
  39. {
  40. std::cout << e.what() << '\n';
  41. }
  42. }