forked from arangodb/velocypack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexampleObjectLookup.cpp
63 lines (52 loc) · 2.16 KB
/
exampleObjectLookup.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include "velocypack/vpack.h"
#include "velocypack/velocypack-exception-macros.h"
using namespace arangodb::velocypack;
int main(int, char*[]) {
VELOCYPACK_GLOBAL_EXCEPTION_TRY
// create an object with a few members
Builder b;
b(Value(ValueType::Object));
b.add("foo", Value(42));
b.add("bar", Value("some string value"));
b.add("baz", Value(ValueType::Object));
b.add("qux", Value(true));
b.add("bart", Value("this is a string"));
b.close();
b.add("quux", Value(12345));
b.close();
// a Slice is a lightweight accessor for a VPack value
Slice s(b.start());
// now fetch the string in the object's "bar" attribute
if (s.hasKey("bar")) {
Slice bar(s.get("bar"));
std::cout << "'bar' attribute value has type: " << bar.type() << std::endl;
}
// fetch non-existing attribute "quetzal"
Slice quetzal(s.get("quetzal"));
// note: this returns a slice of type None
std::cout << "'quetzal' attribute value has type: " << quetzal.type()
<< std::endl;
std::cout << "'quetzal' attribute is None: " << std::boolalpha
<< quetzal.isNone() << std::endl;
// fetch subattribute "baz.qux"
Slice qux(s.get(std::vector<std::string>({"baz", "qux"})));
std::cout << "'baz'.'qux' attribute has type: " << qux.type() << std::endl;
std::cout << "'baz'.'qux' attribute has bool value: " << std::boolalpha
<< qux.getBoolean() << std::endl;
std::cout << "Complete value of 'baz' is: " << s.get("baz").toJson()
<< std::endl;
// fetch non-existing subattribute "bark.foobar"
Slice foobar(s.get(std::vector<std::string>({"bark", "foobar"})));
std::cout << "'bark'.'foobar' attribute is None: " << std::boolalpha
<< foobar.isNone() << std::endl;
// check if subattribute "baz"."bart" does exist
if (s.hasKey(std::vector<std::string>({"baz", "bart"}))) {
// access subattribute using operator syntax
std::cout << "'baz'.'bart' attribute has type: " << s["baz"]["bart"].type()
<< std::endl;
std::cout << "'baz'.'bart' attribute has value: '"
<< s["baz"]["bart"].copyString() << "'" << std::endl;
}
VELOCYPACK_GLOBAL_EXCEPTION_CATCH
}