Electroneum
json_archive.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2020, 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  template <class T>
144  {
145  stream_ << std::dec << promote_to_printable_integer_type(v);
146  }
147 
148  void begin_string(const char *delimiter="\"")
149  {
150  stream_ << delimiter;
151  }
152 
153  void end_string(const char *delimiter="\"")
154  {
155  stream_ << delimiter;
156  }
157 
158  void begin_array(size_t s=0)
159  {
160  inner_array_size_ = s;
161  ++depth_;
162  stream_ << "[ ";
163  }
164 
166  {
167  stream_ << ", ";
168  }
169 
170  void end_array()
171  {
172  --depth_;
173  if (0 < inner_array_size_)
174  {
175  make_indent();
176  }
177  stream_ << "]";
178  }
179 
180  void write_variant_tag(const char *t)
181  {
182  tag(t);
183  }
184 
185 private:
187 };
#define s(x, c)
Definition: aesb.c:47
const uint32_t T[512]
Definition: groestl_tables.h:37
std::string hex(difficulty_type v)
Definition: difficulty.cpp:254
Simple DSL AAPI based on.
#define true
Definition: stdbool.h:37
#define false
Definition: stdbool.h:38
void write_variant_tag(const char *t)
Definition: json_archive.h:180
size_t inner_array_size_
Definition: json_archive.h:186
json_archive(stream_type &s, bool indent=false)
Definition: json_archive.h:117
void serialize_blob(void *buf, size_t len, const char *delimiter="\"")
Definition: json_archive.h:133
static auto promote_to_printable_integer_type(T v) -> decltype(+v)
Definition: json_archive.h:120
void serialize_int(T v)
Definition: json_archive.h:128
void begin_array(size_t s=0)
Definition: json_archive.h:158
void serialize_varint(T &v)
Definition: json_archive.h:143
void end_array()
Definition: json_archive.h:170
void delimit_array()
Definition: json_archive.h:165
void begin_string(const char *delimiter="\"")
Definition: json_archive.h:148
void end_string(const char *delimiter="\"")
Definition: json_archive.h:153
the base class of json archive type
Definition: json_archive.h:52
size_t depth_
Definition: json_archive.h:101
void end_variant()
Definition: json_archive.h:85
Stream & stream()
Definition: json_archive.h:86
stream_type & stream_
Definition: json_archive.h:98
Stream stream_type
Definition: json_archive.h:53
bool object_begin
Definition: json_archive.h:100
json_archive_base< Stream, IsSaving > base_type
Definition: json_archive.h:54
void make_indent()
Definition: json_archive.h:89
json_archive_base(stream_type &s, bool indent=false)
Definition: json_archive.h:59
boost::mpl::bool_< IsSaving > is_saving
Definition: json_archive.h:55
void tag(const char *tag)
Definition: json_archive.h:62
void begin_variant()
Definition: json_archive.h:84
void begin_object()
Definition: json_archive.h:70
const char * variant_tag_type
Definition: json_archive.h:57
void end_object()
Definition: json_archive.h:77
bool indent_
Definition: json_archive.h:99
a archive using the JSON standard
Definition: json_archive.h:112