Query.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Query.h
  3. //
  4. // Library: JSON
  5. // Package: JSON
  6. // Module: Query
  7. //
  8. // Definition of the Query class.
  9. //
  10. // Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef JSON_JSONQuery_INCLUDED
  16. #define JSON_JSONQuery_INCLUDED
  17. #include "Poco/JSON/JSON.h"
  18. #include "Poco/JSON/Object.h"
  19. #include "Poco/JSON/Array.h"
  20. namespace Poco {
  21. namespace JSON {
  22. class JSON_API Query
  23. /// Class that can be used to search for a value in a JSON object or array.
  24. {
  25. public:
  26. Query(const Dynamic::Var& source);
  27. /// Creates a Query/
  28. ///
  29. /// Source must be JSON Object, Array, Object::Ptr,
  30. /// Array::Ptr or empty Var. Any other type will trigger throwing of
  31. /// InvalidArgumentException.
  32. ///
  33. /// Creating Query holding Ptr will typically result in faster
  34. /// performance.
  35. virtual ~Query();
  36. /// Destroys the Query.
  37. Object::Ptr findObject(const std::string& path) const;
  38. /// Search for an object.
  39. ///
  40. /// When the object can't be found, a zero Ptr is returned;
  41. /// otherwise, a shared pointer to internally held object
  42. /// is returned.
  43. /// If object (as opposed to a pointer to object) is held
  44. /// internally, a shared pointer to new (heap-allocated) Object is
  45. /// returned; this may be expensive operation.
  46. Object& findObject(const std::string& path, Object& obj) const;
  47. /// Search for an object.
  48. ///
  49. /// If object is found, it is assigned to the
  50. /// Object through the reference passed in. When the object can't be
  51. /// found, the provided Object is emptied and returned.
  52. Array::Ptr findArray(const std::string& path) const;
  53. /// Search for an array.
  54. ///
  55. /// When the array can't be found, a zero Ptr is returned;
  56. /// otherwise, a shared pointer to internally held array
  57. /// is returned.
  58. /// If array (as opposed to a pointer to array) is held
  59. /// internally, a shared pointer to new (heap-allocated) Object is
  60. /// returned; this may be expensive operation.
  61. Array& findArray(const std::string& path, Array& obj) const;
  62. /// Search for an array.
  63. ///
  64. /// If array is found, it is assigned to the
  65. /// Object through the reference passed in. When the array can't be
  66. /// found, the provided Object is emptied and returned.
  67. Dynamic::Var find(const std::string& path) const;
  68. /// Searches a value.
  69. ///
  70. /// Example: "person.children[0].name" will return the
  71. /// the name of the first child. When the value can't be found
  72. /// an empty value is returned.
  73. template<typename T>
  74. T findValue(const std::string& path, const T& def) const
  75. /// Searches for a value will convert it to the given type.
  76. /// When the value can't be found or has an invalid type
  77. /// the default value will be returned.
  78. {
  79. T result = def;
  80. Dynamic::Var value = find(path);
  81. if (!value.isEmpty())
  82. {
  83. try
  84. {
  85. result = value.convert<T>();
  86. }
  87. catch (...)
  88. {
  89. }
  90. }
  91. return result;
  92. }
  93. std::string findValue(const char* path, const char* def) const
  94. /// Searches for a value will convert it to the given type.
  95. /// When the value can't be found or has an invalid type
  96. /// the default value will be returned.
  97. {
  98. return findValue<std::string>(path, def);
  99. }
  100. private:
  101. Dynamic::Var _source;
  102. };
  103. } } // namespace Poco::JSON
  104. #endif // JSON_JSONQuery_INCLUDED