forked from arangodb/velocypack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexampleSlice.cpp
42 lines (33 loc) · 1.34 KB
/
exampleSlice.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
#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 attributes "b", "a", "l" and "name"
// note that the attribute names will be sorted in the target VPack object!
Builder b;
b(Value(ValueType::Object))("b", Value(12))("a", Value(true))(
"l", Value(ValueType::Array))(Value(1))(Value(2))(Value(3))()(
"name", Value("Gustav"))();
// a Slice is a lightweight accessor for a VPack value
Slice s(b.start());
// inspect the outermost value (should be an Object...)
std::cout << "Slice: " << s << std::endl;
std::cout << "Type: " << s.type() << std::endl;
std::cout << "Bytesize: " << s.byteSize() << std::endl;
std::cout << "Members: " << s.length() << std::endl;
if (s.isObject()) {
Slice ss = s.get("l"); // Now ss points to the subvalue under "l"
std::cout << "Length of .l: " << ss.length() << std::endl;
std::cout << "Second entry of .l:" << ss.at(1).getInt() << std::endl;
}
Slice sss = s.get("name");
if (sss.isString()) {
ValueLength len;
char const* st = sss.getString(len);
std::cout << "Name in .name: " << std::string(st, checkOverflow(len))
<< std::endl;
}
VELOCYPACK_GLOBAL_EXCEPTION_CATCH
}