Electroneum
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 
37 #pragma once
38 
39 #include "serialization.h"
40 #include <cassert>
41 #include <iostream>
42 #include <iomanip>
43 
50 template <class Stream, bool IsSaving>
52 {
53  typedef Stream stream_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 
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:
99  bool indent_;
101  size_t depth_;
102 };
103 
104 
111 template <bool W>
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>
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 
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 };
static auto promote_to_printable_integer_type(T v) -> decltype(+v)
Definition: json_archive.h:120
the base class of json archive type
Definition: json_archive.h:51
const uint32_t T[512]
stream_type & stream_
Definition: json_archive.h:98
void tag(const char *tag)
Definition: json_archive.h:62
::std::string string
Definition: gtest-port.h:1097
void write_variant_tag(const char *t)
Definition: json_archive.h:186
void end_string(const char *delimiter="\)
Definition: json_archive.h:159
void begin_array(size_t s=0)
Definition: json_archive.h:164
void begin_string(const char *delimiter="\)
Definition: json_archive.h:154
void serialize_varint(T &v)
Definition: json_archive.h:149
const char * variant_tag_type
Definition: json_archive.h:57
json_archive_base(stream_type &s, bool indent=false)
Definition: json_archive.h:59
Simple DSL AAPI based on.
void serialize_string(std::string str, const char *delimiter="\)
Definition: json_archive.h:142
return true
json_archive(stream_type &s, bool indent=false)
Definition: json_archive.h:117
json_archive_base< Stream, IsSaving > base_type
Definition: json_archive.h:54
Stream & stream()
Definition: json_archive.h:86
void serialize_blob(void *buf, size_t len, const char *delimiter="\)
Definition: json_archive.h:133
std::string make_indent(size_t indent)
#define false
Definition: stdbool.h:38
const char * buf
Definition: slow_memmem.cpp:74
boost::mpl::bool_< IsSaving > is_saving
Definition: json_archive.h:55
a archive using the JSON standard
Definition: json_archive.h:112