Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
json_archive.h
Go to the documentation of this file.
1
// Copyrights(c) 2017-2021, The Electroneum Project
2
// Copyrights(c) 2014-2019, The Monero Project
3
//
4
// All rights reserved.
5
//
6
// Redistribution and use in source and binary forms, with or without modification, are
7
// permitted provided that the following conditions are met:
8
//
9
// 1. Redistributions of source code must retain the above copyright notice, this list of
10
// conditions and the following disclaimer.
11
//
12
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13
// of conditions and the following disclaimer in the documentation and/or other
14
// materials provided with the distribution.
15
//
16
// 3. Neither the name of the copyright holder nor the names of its contributors may be
17
// used to endorse or promote products derived from this software without specific
18
// prior written permission.
19
//
20
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
//
30
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31
36
37
#pragma once
38
39
#include "
serialization.h
"
40
#include <cassert>
41
#include <iostream>
42
#include <iomanip>
43
50
template
<
class
Stream,
bool
IsSaving>
51
struct
json_archive_base
52
{
53
typedef
Stream
stream_type
;
54
typedef
json_archive_base<Stream, IsSaving>
base_type
;
55
typedef
boost::mpl::bool_<IsSaving>
is_saving
;
56
57
typedef
const
char
*
variant_tag_type
;
58
59
json_archive_base
(
stream_type
&s,
bool
indent =
false
)
60
:
stream_
(s),
indent_
(indent),
object_begin
(
false
),
depth_
(0) { }
61
62
void
tag
(
const
char
*
tag
) {
63
if
(!
object_begin
)
64
stream_
<<
", "
;
65
make_indent
();
66
stream_
<<
'"'
<<
tag
<<
"\": "
;
67
object_begin
=
false
;
68
}
69
70
void
begin_object
()
71
{
72
stream_
<<
"{"
;
73
++
depth_
;
74
object_begin
=
true
;
75
}
76
77
void
end_object
()
78
{
79
--
depth_
;
80
make_indent
();
81
stream_
<<
"}"
;
82
}
83
84
void
begin_variant
() {
begin_object
(); }
85
void
end_variant
() {
end_object
(); }
86
Stream
&
stream
() {
return
stream_
; }
87
88
protected
:
89
void
make_indent
()
90
{
91
if
(
indent_
)
92
{
93
stream_
<<
'\n'
<< std::string(2 *
depth_
,
' '
);
94
}
95
}
96
97
protected
:
98
stream_type
&
stream_
;
99
bool
indent_
;
100
bool
object_begin
;
101
size_t
depth_
;
102
};
103
104
111
template
<
bool
W>
112
struct
json_archive
;
113
114
template
<>
115
struct
json_archive
<
true
> :
public
json_archive_base
<std::ostream, true>
116
{
117
json_archive
(
stream_type
&s,
bool
indent =
false
) :
base_type
(s, indent), inner_array_size_(0) { }
118
119
template
<
typename
T>
120
static
auto
promote_to_printable_integer_type
(
T
v) ->
decltype
(+v)
121
{
122
// Unary operator '+' performs integral promotion on type T [expr.unary.op].
123
// If T is signed or unsigned char, it's promoted to int and printed as number.
124
return
+v;
125
}
126
127
template
<
class
T>
128
void
serialize_int
(
T
v)
129
{
130
stream_
<< std::dec <<
promote_to_printable_integer_type
(v);
131
}
132
133
void
serialize_blob
(
void
*
buf
,
size_t
len,
const
char
*delimiter=
"\""
) {
134
begin_string
(delimiter);
135
for
(
size_t
i = 0; i < len; i++) {
136
unsigned
char
c = ((
unsigned
char
*)
buf
)[i];
137
stream_
<< std::hex << std::setw(2) << std::setfill(
'0'
) << (int)c;
138
}
139
end_string
(delimiter);
140
}
141
142
void
serialize_string
(std::string str,
const
char
*delimiter=
"\""
) {
143
begin_string
(delimiter);
144
stream_
<< str;
145
end_string
(delimiter);
146
}
147
148
template
<
class
T>
149
void
serialize_varint
(
T
&v)
150
{
151
stream_
<< std::dec <<
promote_to_printable_integer_type
(v);
152
}
153
154
void
begin_string
(
const
char
*delimiter=
"\""
)
155
{
156
stream_
<< delimiter;
157
}
158
159
void
end_string
(
const
char
*delimiter=
"\""
)
160
{
161
stream_
<< delimiter;
162
}
163
164
void
begin_array
(
size_t
s=0)
165
{
166
inner_array_size_ = s;
167
++
depth_
;
168
stream_
<<
"[ "
;
169
}
170
171
void
delimit_array
()
172
{
173
stream_
<<
", "
;
174
}
175
176
void
end_array
()
177
{
178
--
depth_
;
179
if
(0 < inner_array_size_)
180
{
181
make_indent
();
182
}
183
stream_
<<
"]"
;
184
}
185
186
void
write_variant_tag
(
const
char
*t)
187
{
188
tag
(t);
189
}
190
191
private
:
192
size_t
inner_array_size_;
193
};
Stream
Concept for reading and writing characters.
serialization.h
Simple DSL AAPI based on.
buf
const char * buf
Definition
slow_memmem.cpp:74
true
#define true
false
#define false
json_archive< true >::serialize_string
void serialize_string(std::string str, const char *delimiter="\"")
Definition
json_archive.h:142
json_archive< true >::write_variant_tag
void write_variant_tag(const char *t)
Definition
json_archive.h:186
json_archive< true >::json_archive
json_archive(stream_type &s, bool indent=false)
Definition
json_archive.h:117
json_archive< true >::serialize_blob
void serialize_blob(void *buf, size_t len, const char *delimiter="\"")
Definition
json_archive.h:133
json_archive< true >::promote_to_printable_integer_type
static auto promote_to_printable_integer_type(T v) -> decltype(+v)
Definition
json_archive.h:120
json_archive< true >::serialize_int
void serialize_int(T v)
Definition
json_archive.h:128
json_archive< true >::begin_array
void begin_array(size_t s=0)
Definition
json_archive.h:164
json_archive< true >::serialize_varint
void serialize_varint(T &v)
Definition
json_archive.h:149
json_archive< true >::end_array
void end_array()
Definition
json_archive.h:176
json_archive< true >::delimit_array
void delimit_array()
Definition
json_archive.h:171
json_archive< true >::begin_string
void begin_string(const char *delimiter="\"")
Definition
json_archive.h:154
json_archive< true >::end_string
void end_string(const char *delimiter="\"")
Definition
json_archive.h:159
json_archive_base::depth_
size_t depth_
Definition
json_archive.h:101
json_archive_base::end_variant
void end_variant()
Definition
json_archive.h:85
json_archive_base::stream
Stream & stream()
Definition
json_archive.h:86
json_archive_base::stream_
stream_type & stream_
Definition
json_archive.h:98
json_archive_base::stream_type
Stream stream_type
Definition
json_archive.h:53
json_archive_base::object_begin
bool object_begin
Definition
json_archive.h:100
json_archive_base::base_type
json_archive_base< Stream, IsSaving > base_type
Definition
json_archive.h:54
json_archive_base::make_indent
void make_indent()
Definition
json_archive.h:89
json_archive_base::json_archive_base
json_archive_base(stream_type &s, bool indent=false)
Definition
json_archive.h:59
json_archive_base::is_saving
boost::mpl::bool_< IsSaving > is_saving
Definition
json_archive.h:55
json_archive_base::tag
void tag(const char *tag)
Definition
json_archive.h:62
json_archive_base::begin_variant
void begin_variant()
Definition
json_archive.h:84
json_archive_base::begin_object
void begin_object()
Definition
json_archive.h:70
json_archive_base::variant_tag_type
const char * variant_tag_type
Definition
json_archive.h:57
json_archive_base::end_object
void end_object()
Definition
json_archive.h:77
json_archive_base::indent_
bool indent_
Definition
json_archive.h:99
json_archive
a archive using the JSON standard
Definition
json_archive.h:112
T
#define T(x)
src
serialization
json_archive.h
Generated on
for Electroneum by
1.17.0