Electroneum
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
50template <class Stream, bool IsSaving>
52{
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_ << ", ";
66 stream_ << '"' << tag << "\": ";
67 object_begin = false;
68 }
69
71 {
72 stream_ << "{";
73 ++depth_;
74 object_begin = true;
75 }
76
78 {
79 --depth_;
81 stream_ << "}";
82 }
83
85 void end_variant() { end_object(); }
86 Stream &stream() { return stream_; }
87
88protected:
90 {
91 if (indent_)
92 {
93 stream_ << '\n' << std::string(2 * depth_, ' ');
94 }
95 }
96
97protected:
99 bool indent_;
101 size_t depth_;
102};
103
104
111template <bool W>
113
114template <>
115struct 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>
129 {
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>
150 {
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
172 {
173 stream_ << ", ";
174 }
175
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
191private:
192 size_t inner_array_size_;
193};
Concept for reading and writing characters.
Simple DSL AAPI based on.
const char * buf
#define true
#define false
void serialize_string(std::string str, const char *delimiter="\"")
void write_variant_tag(const char *t)
json_archive(stream_type &s, bool indent=false)
void serialize_blob(void *buf, size_t len, const char *delimiter="\"")
static auto promote_to_printable_integer_type(T v) -> decltype(+v)
void begin_array(size_t s=0)
void serialize_varint(T &v)
void begin_string(const char *delimiter="\"")
void end_string(const char *delimiter="\"")
Stream & stream()
stream_type & stream_
json_archive_base< Stream, IsSaving > base_type
json_archive_base(stream_type &s, bool indent=false)
boost::mpl::bool_< IsSaving > is_saving
void tag(const char *tag)
const char * variant_tag_type
a archive using the JSON standard
#define T(x)