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