Monero
json_archive.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2022, 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 
60 
61  bool good() const { return stream_.good(); }
62  void set_fail() { stream_.setstate(std::ios::failbit); }
63  void clear_fail() { stream_.clear(); }
64 
65  void tag(const char *tag) {
66  if (!object_begin)
67  stream_ << ", ";
68  make_indent();
69  stream_ << '"' << tag << "\": ";
70  object_begin = false;
71  }
72 
73  void begin_object()
74  {
75  stream_ << "{";
76  ++depth_;
77  object_begin = true;
78  }
79 
80  void end_object()
81  {
82  --depth_;
83  make_indent();
84  stream_ << "}";
85  }
86 
88  void end_variant() { end_object(); }
89 
90  bool varint_bug_backward_compatibility_enabled() const { return false; }
91 
92 protected:
93  void make_indent()
94  {
95  if (indent_)
96  {
97  stream_ << '\n' << std::string(2 * depth_, ' ');
98  }
99  }
100 
101 protected:
103  bool indent_;
105  size_t depth_;
106 };
107 
108 
115 template <bool W>
117 
118 template <>
119 struct json_archive<true> : public json_archive_base<std::ostream, true>
120 {
121  json_archive(stream_type &s, bool indent = false) : base_type(s, indent), inner_array_size_(0) { }
122 
123  std::streampos getpos() const { return stream_.tellp(); }
124 
125  template<typename T>
126  static auto promote_to_printable_integer_type(T v) -> decltype(+v)
127  {
128  // Unary operator '+' performs integral promotion on type T [expr.unary.op].
129  // If T is signed or unsigned char, it's promoted to int and printed as number.
130  return +v;
131  }
132 
133  template <class T>
134  void serialize_int(T v)
135  {
136  stream_ << std::dec << promote_to_printable_integer_type(v);
137  }
138 
139  void serialize_blob(void *buf, size_t len, const char *delimiter="\"") {
140  begin_string(delimiter);
141  for (size_t i = 0; i < len; i++) {
142  unsigned char c = ((unsigned char *)buf)[i];
143  stream_ << std::hex << std::setw(2) << std::setfill('0') << (int)c;
144  }
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:
193 };
static auto promote_to_printable_integer_type(T v) -> decltype(+v)
Definition: json_archive.h:126
bool good() const
Definition: json_archive.h:61
the base class of json archive type
Definition: json_archive.h:50
size_t depth_
Definition: json_archive.h:105
const uint32_t T[512]
Definition: groestl_tables.h:36
stream_type & stream_
Definition: json_archive.h:102
bool varint_bug_backward_compatibility_enabled() const
Definition: json_archive.h:90
void begin_variant()
Definition: json_archive.h:87
const char * tag
Definition: testobsdrdr.c:19
int i
Definition: pymoduletest.py:23
void tag(const char *tag)
Definition: json_archive.h:65
::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
t
Definition: console.py:33
void begin_array(size_t s=0)
Definition: json_archive.h:164
const char * s
Definition: minissdp.c:596
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:56
json_archive_base(stream_type &s, bool indent=false)
Definition: json_archive.h:58
bool object_begin
Definition: json_archive.h:104
Simple DSL AAPI based on.
json_archive(stream_type &s, bool indent=false)
Definition: json_archive.h:121
bool indent_
Definition: json_archive.h:103
int
Definition: pymoduletest.py:17
json_archive_base< Stream, IsSaving > base_type
Definition: json_archive.h:53
void delimit_array()
Definition: json_archive.h:171
void serialize_blob(void *buf, size_t len, const char *delimiter="\)
Definition: json_archive.h:139
std::string make_indent(size_t indent)
Definition: portable_storage_to_json.h:58
#define false
Definition: stdbool.h:37
const char * buf
Definition: slow_memmem.cpp:73
void begin_object()
Definition: json_archive.h:73
size_t inner_array_size_
Definition: json_archive.h:192
Stream stream_type
Definition: json_archive.h:52
boost::mpl::bool_< IsSaving > is_saving
Definition: json_archive.h:54
void set_fail()
Definition: json_archive.h:62
void end_array()
Definition: json_archive.h:176
std::streampos getpos() const
Definition: json_archive.h:123
void make_indent()
Definition: json_archive.h:93
void serialize_int(T v)
Definition: json_archive.h:134
void clear_fail()
Definition: json_archive.h:63
void end_variant()
Definition: json_archive.h:88
void end_object()
Definition: json_archive.h:80
static constexpr const char hex[]
Definition: wipeable_string.cpp:36
#define true
Definition: stdbool.h:36
c
Definition: pymoduletest.py:79
a archive using the JSON standard
Definition: json_archive.h:116
indent
Definition: transfer.py:36