cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
json_stream.cpp
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module:
4
5
Author: Peter Schrammel
6
7
\*******************************************************************/
8
9
#include "
json_stream.h
"
10
11
#include <ostream>
12
14
void
json_streamt::output_delimiter
()
15
{
16
if
(!
first
)
17
out
<<
','
;
18
else
19
first
=
false
;
20
out
<<
'\n'
;
21
out
<< std::string((
indent
+ 1) * 2,
' '
);
22
}
23
27
json_stream_arrayt::json_stream_arrayt
(std::ostream &
out
,
unsigned
indent
)
28
:
json_streamt
(
out
,
indent
)
29
{
30
out
<<
'['
;
31
}
32
34
void
json_stream_arrayt::output_child_stream
()
35
{
36
if
(!
object
.empty())
37
{
38
output_delimiter
();
39
object
[
"array_element"
].output_rec(
out
,
indent
+ 1);
40
object
.clear();
41
}
42
if
(
child_stream
)
43
{
44
child_stream
->close();
45
child_stream
=
nullptr
;
46
}
47
}
48
50
void
json_stream_arrayt::output_finalizer
()
51
{
52
out
<<
'\n'
<< std::string(
indent
* 2,
' '
);
53
out
<<
']'
;
54
}
55
59
json_stream_objectt::json_stream_objectt
(std::ostream &
out
,
unsigned
indent
)
60
:
json_streamt
(
out
,
indent
)
61
{
62
out
<<
'{'
;
63
}
64
66
json_stream_arrayt
&
json_streamt::create_child_stream_array
()
67
{
68
child_stream
=
69
std::unique_ptr<json_streamt>(
new
json_stream_arrayt
(
out
,
indent
+ 1));
70
return
static_cast<
json_stream_arrayt
&
>
(*child_stream);
71
}
72
74
json_stream_objectt
&
json_streamt::create_child_stream_object
()
75
{
76
child_stream
=
77
std::unique_ptr<json_streamt>(
new
json_stream_objectt
(
out
,
indent
+ 1));
78
return
static_cast<
json_stream_objectt
&
>
(*child_stream);
79
}
80
82
json_stream_objectt
&
json_stream_arrayt::push_back_stream_object
()
83
{
84
PRECONDITION
(
open
);
85
// To ensure consistency of the output, we flush and
86
// close the current child stream before creating the new one.
87
output_child_stream
();
88
output_delimiter
();
89
return
create_child_stream_object
();
90
}
91
93
json_stream_arrayt
&
json_stream_arrayt::push_back_stream_array
()
94
{
95
PRECONDITION
(
open
);
96
// To ensure consistency of the output, we flush and
97
// close the current child stream before creating the new one.
98
output_child_stream
();
99
output_delimiter
();
100
return
create_child_stream_array
();
101
}
102
105
json_stream_objectt
&
106
json_stream_objectt::push_back_stream_object
(
const
std::string &key)
107
{
108
PRECONDITION
(
open
);
109
// To ensure consistency of the output, we flush and
110
// close the current child stream before creating the new one.
111
output_child_stream
();
112
output_delimiter
();
113
jsont::output_key
(
out
, key);
114
return
create_child_stream_object
();
115
}
116
119
json_stream_arrayt
&
120
json_stream_objectt::push_back_stream_array
(
const
std::string &key)
121
{
122
PRECONDITION
(
open
);
123
// To ensure consistency of the output, we flush and
124
// close the current child stream before creating the new one.
125
output_child_stream
();
126
output_delimiter
();
127
jsont::output_key
(
out
, key);
128
return
create_child_stream_array
();
129
}
130
133
void
json_stream_objectt::output_child_stream
()
134
{
135
for
(
const
auto
&obj :
object
)
136
{
137
output_delimiter
();
138
jsont::output_key
(
out
, obj.first);
139
obj.second.output_rec(
out
,
indent
+ 1);
140
}
141
object
.clear();
142
if
(
child_stream
)
143
{
144
child_stream
->close();
145
child_stream
=
nullptr
;
146
}
147
}
148
150
void
json_stream_objectt::output_finalizer
()
151
{
152
jsont::output_object
(
out
,
object
,
indent
);
153
out
<<
'\n'
<< std::string(
indent
* 2,
' '
);
154
out
<<
'}'
;
155
}
json_stream_arrayt
Provides methods for streaming JSON arrays.
Definition
json_stream.h:93
json_stream_arrayt::push_back_stream_object
json_stream_objectt & push_back_stream_object()
Add a JSON object child stream.
Definition
json_stream.cpp:82
json_stream_arrayt::push_back_stream_array
json_stream_arrayt & push_back_stream_array()
Add a JSON array child stream.
Definition
json_stream.cpp:93
json_stream_arrayt::output_finalizer
void output_finalizer() override
Output the finalizing character for a JSON array.
Definition
json_stream.cpp:50
json_stream_arrayt::output_child_stream
void output_child_stream() override
Output the non-streaming JSON objects and closes the current child stream.
Definition
json_stream.cpp:34
json_stream_arrayt::json_stream_arrayt
json_stream_arrayt(std::ostream &out, unsigned indent=0)
Construct a new JSON array stream.
Definition
json_stream.cpp:27
json_stream_objectt
Provides methods for streaming JSON objects.
Definition
json_stream.h:140
json_stream_objectt::output_finalizer
void output_finalizer() override
Output the finalizing character for a JSON object.
Definition
json_stream.cpp:150
json_stream_objectt::push_back_stream_object
json_stream_objectt & push_back_stream_object(const std::string &key)
Add a JSON object stream for a specific key.
Definition
json_stream.cpp:106
json_stream_objectt::output_child_stream
void output_child_stream() override
Output non-streaming JSON properties and flushes and closes the child stream.
Definition
json_stream.cpp:133
json_stream_objectt::push_back_stream_array
json_stream_arrayt & push_back_stream_array(const std::string &key)
Add a JSON array stream for a specific key.
Definition
json_stream.cpp:120
json_stream_objectt::json_stream_objectt
json_stream_objectt(std::ostream &out, unsigned indent=0)
Constructor for json_stream_objectt.
Definition
json_stream.cpp:59
json_streamt::create_child_stream_array
json_stream_arrayt & create_child_stream_array()
Create a new JSON array child stream.
Definition
json_stream.cpp:66
json_streamt::create_child_stream_object
json_stream_objectt & create_child_stream_object()
Create a new JSON object child stream.
Definition
json_stream.cpp:74
json_streamt::first
bool first
Is the current element the first element in the object or array?
Definition
json_stream.h:68
json_streamt::open
bool open
Denotes whether the current stream is open or has been invalidated.
Definition
json_stream.h:62
json_streamt::output_delimiter
void output_delimiter()
Outputs the delimiter between JSON elements.
Definition
json_stream.cpp:14
json_streamt::indent
unsigned indent
Definition
json_stream.h:64
json_streamt::json_streamt
json_streamt(std::ostream &_out, unsigned _indent)
Constructor to be used by derived classes.
Definition
json_stream.h:56
json_streamt::out
std::ostream & out
Definition
json_stream.h:63
json_streamt::child_stream
std::unique_ptr< json_streamt > child_stream
The current child stream.
Definition
json_stream.h:79
jsont::output_key
static void output_key(std::ostream &out, const std::string &key)
Definition
json.cpp:154
jsont::output_object
static void output_object(std::ostream &out, const objectt &object, unsigned indent)
Basic handling of the printing of a JSON object.
Definition
json.cpp:132
json_stream.h
PRECONDITION
#define PRECONDITION(CONDITION)
Definition
invariant.h:463
util
json_stream.cpp
Generated by
1.17.0