forked from sdarwin/json
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue_stack.cpp
More file actions
125 lines (91 loc) · 3 KB
/
Copy pathvalue_stack.cpp
File metadata and controls
125 lines (91 loc) · 3 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// Copyright (c) 2019 Vinnie Falco ([email protected])
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/json
//
// Test that header file is self-contained.
#include <boost/json/value_stack.hpp>
#include <boost/json/static_resource.hpp>
#include <boost/json/to_string.hpp>
#include "test_suite.hpp"
BOOST_JSON_NS_BEGIN
class value_stack_test
{
public:
// This is from the javadoc
void
testValueStack()
{
//----------------------------------
// value_stack
{
// This example builds a json::value without any dynamic memory allocations:
// Construct the value stack using a local buffer
char temp[4096];
value_stack st( storage_ptr(), temp, sizeof(temp) );
// Create a static resource with a local initial buffer
char buf[4096];
static_resource mr( buf, sizeof(buf) );
// All values on the stack will use `mr`
st.reset(&mr);
// Push the key/value pair "a":1.
st.push_key("a");
st.push_int64(1);
// Push "b":null
st.push_key("b");
st.push_null();
// Push "c":"hello"
st.push_key("c");
st.push_string("hello");
// Pop the three key/value pairs and push an object with those three values.
st.push_object(3);
// Pop the object from the stack and take ownership.
value jv = st.release();
assert( to_string(jv) == "{\"a\":1,\"b\":null,\"c\":\"hello\"}" );
// At this point we could re-use the stack by calling reset
}
//----------------------------------
// value_stack::push_array
{
value_stack st;
// reset must be called first or else the behavior is undefined
st.reset();
// Place three values on the stack
st.push_int64( 1 );
st.push_int64( 2 );
st.push_int64( 3 );
// Remove the 3 values, and push an array with those 3 elements on the stack
st.push_array( 3 );
// Pop the object from the stack and take ownership.
value jv = st.release();
assert( to_string(jv) == "[1,2,3]" );
// At this point, reset must be called again to use the stack
}
//----------------------------------
// value_stack::push_object
{
value_stack st;
// reset must be called first or else the behavior is undefined
st.reset();
// Place a key/value pair onto the stack
st.push_key( "x" );
st.push_bool( true );
// Replace the key/value pair with an object containing a single element
st.push_object( 1 );
// Pop the object from the stack and take ownership.
value jv = st.release();
assert( to_string(jv) == "{\"x\":true}" );
// At this point, reset must be called again to use the stack
}
}
void
run()
{
testValueStack();
}
};
TEST_SUITE(value_stack_test, "boost.json.value_stack");
BOOST_JSON_NS_END