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
16
17#ifndef JSON_JSONQuery_INCLUDED
18#define JSON_JSONQuery_INCLUDED
19
20
21#include "Poco/JSON/JSON.h"
22#include "Poco/JSON/Object.h"
23#include "Poco/JSON/Array.h"
24
25
26namespace Poco {
27namespace JSON {
28
29
30class JSON_API Query
31 /// Class that can be used to search for a value in a JSON object or array.
32{
33public:
34 Query(const Dynamic::Var& source);
35 /// Creates a Query/
36 ///
37 /// Source must be JSON Object, Array, Object::Ptr,
38 /// Array::Ptr or empty Var. Any other type will trigger throwing of
39 /// InvalidArgumentException.
40 ///
41 /// Creating Query holding Ptr will typically result in faster
42 /// performance.
43
44 virtual ~Query();
45 /// Destroys the Query.
46
47 Object::Ptr findObject(const std::string& path) const;
48 /// Search for an object.
49 ///
50 /// When the object can't be found, a zero Ptr is returned;
51 /// otherwise, a shared pointer to internally held object
52 /// is returned.
53 /// If object (as opposed to a pointer to object) is held
54 /// internally, a shared pointer to new (heap-allocated) Object is
55 /// returned; this may be expensive operation.
56
57 Object& findObject(const std::string& path, Object& obj) const;
58 /// Search for an object.
59 ///
60 /// If object is found, it is assigned to the
61 /// Object through the reference passed in. When the object can't be
62 /// found, the provided Object is emptied and returned.
63
64 Array::Ptr findArray(const std::string& path) const;
65 /// Search for an array.
66 ///
67 /// When the array can't be found, a zero Ptr is returned;
68 /// otherwise, a shared pointer to internally held array
69 /// is returned.
70 /// If array (as opposed to a pointer to array) is held
71 /// internally, a shared pointer to new (heap-allocated) Object is
72 /// returned; this may be expensive operation.
73
74 Array& findArray(const std::string& path, Array& obj) const;
75 /// Search for an array.
76 ///
77 /// If array is found, it is assigned to the
78 /// Object through the reference passed in. When the array can't be
79 /// found, the provided Object is emptied and returned.
80
81 Dynamic::Var find(const std::string& path) const;
82 /// Searches a value.
83 ///
84 /// Example: "person.children[0].name" will return the
85 /// the name of the first child. When the value can't be found
86 /// an empty value is returned.
87
88 template<typename T>
89 T findValue(const std::string& path, const T& def) const
90 /// Searches for a value will convert it to the given type.
91 /// When the value can't be found or has an invalid type
92 /// the default value will be returned.
93 {
94 T result = def;
95 Dynamic::Var value = find(path);
96 if (!value.isEmpty())
97 {
98 try
99 {
100 result = value.convert<T>();
101 }
102 catch (...)
103 {
104 }
105 }
106 return result;
107 }
108
109 std::string findValue(const char* path, const char* def) const
110 /// Searches for a value will convert it to the given type.
111 /// When the value can't be found or has an invalid type
112 /// the default value will be returned.
113 {
114 return findValue<std::string>(path, def);
115 }
116
117private:
118 Dynamic::Var _source;
119};
120
121
122} } // namespace Poco::JSON
123
124
125#endif // JSON_JSONQuery_INCLUDED
126