pupene
0.2.0
src
json-puppers.cpp
Go to the documentation of this file.
1
#
include
"json-puppers.h"
2
3
namespace
pupene
{
4
std
::
string
JsonWriter
::
quote
(
const
std
::
string
&
s
) {
5
return
'"'
+
s
+
'"'
;
6
}
7
8
void
JsonWriter
::
end
(
const
Meta
&
/*meta*/
) {
9
switch
(
graph
.
back
()) {
10
case
Meta
::
Type
::
Array
:
11
stream
<<
std
::
endl
<<
']'
;
12
break
;
13
case
Meta
::
Type
::
Object
:
14
stream
<<
std
::
endl
<<
'}'
;
15
break
;
16
default
:
17
throw
std
::
runtime_error
(
"expected array or object"
);
18
}
19
20
stack
.
pop_back
();
21
graph
.
pop_back
();
22
}
23
24
const
json
*
JsonReader
::
resolve_child
(
const
Meta
&
meta
) {
25
using
Type
=
Meta
::
Type
;
26
27
if
(
parent_is
(
Type
::
Array
)) {
28
auto
&
parent
=
state
.
back
();
29
return
&
parent
.
dom
->
begin
()[
parent
.
idx
++];
30
}
else
{
// NOLINT
31
const
auto
current
= !
state
.
empty
() ?
state
.
back
().
dom
: &
dom
;
32
const
auto
&
child
= (
strlen
(
meta
.
name
) > 0)
33
?
current
->
at
(
meta
.
name
)
34
: *
current
;
35
return
&
child
;
36
}
37
}
38
39
void
JsonReader
::
end
(
const
Meta
&
/*meta*/
) {
40
state
.
pop_back
();
41
}
42
43
bool
JsonReader
::
parent_is
(
const
Meta
::
Type
type
)
const
{
44
return
!
state
.
empty
() &&
state
.
back
().
meta
.
type
==
type
;
45
}
46
47
std
::
ostream
&
operator
<<(
std
::
ostream
&
out
,
48
const
JsonReader
&
reader
) {
49
50
out <<
"JsonReader<"
<< &reader <<
">"
<< std::endl;
51
std::for_each(reader.state.crbegin(),
52
reader.state.crend(),
53
[&out](
auto
& s) {
54
out <<
" "
<< s.idx <<
": "
<< s.meta << std::endl;
55
});
56
57
return
out;
58
}
59
60
std
::
ostream
&
operator
<<(
std
::
ostream
&
out
,
61
const
Meta
&
meta
) {
62
using
Type = Meta::Type;
63
64
switch
(meta.type) {
65
case
Type::Array:
66
out <<
"(array) "
;
67
break
;
68
case
Type::Object:
69
out <<
"(object) "
;
70
break
;
71
case
Type::Value:
72
out <<
"(value) "
;
73
break
;
74
}
75
76
if
(strlen(meta.name) > 0)
77
out << meta.name;
78
79
return
out;
80
}
81
}
pupene
Definition:
debug.cpp:4
pupene::operator<<
std::ostream & operator<<(std::ostream &out, const Meta &meta)
Definition:
json-puppers.cpp:60
Generated by
1.8.13