Monero
Loading...
Searching...
No Matches
portable_storage_to_json.h
Go to the documentation of this file.
1// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above copyright
9// notice, this list of conditions and the following disclaimer in the
10// documentation and/or other materials provided with the distribution.
11// * Neither the name of the Andrey N. Sabelnikov nor the
12// names of its contributors may be used to endorse or promote products
13// derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25//
26
27
28
29#pragma once
30
31#include "misc_language.h"
33#include "parserse_base_utils.h"
34
35namespace epee
36{
37 namespace serialization
38 {
39
40 template<class t_stream>
41 void dump_as_json(t_stream& strm, const array_entry& ae, size_t indent, bool insert_newlines);
42 template<class t_stream>
43 void dump_as_json(t_stream& strm, const storage_entry& se, size_t indent, bool insert_newlines);
44 template<class t_stream>
45 void dump_as_json(t_stream& strm, const std::string& v, size_t indent, bool insert_newlines);
46 template<class t_stream>
47 void dump_as_json(t_stream& strm, const int8_t& v, size_t indent, bool insert_newlines);
48 template<class t_stream>
49 void dump_as_json(t_stream& strm, const uint8_t& v, size_t indent, bool insert_newlines);
50 template<class t_stream>
51 void dump_as_json(t_stream& strm, const bool& v, size_t indent, bool insert_newlines);
52 template<class t_stream, class t_type>
53 void dump_as_json(t_stream& strm, const t_type& v, size_t indent, bool insert_newlines);
54 template<class t_stream>
55 void dump_as_json(t_stream& strm, const section& sec, size_t indent, bool insert_newlines);
56
57
58 inline std::string make_indent(size_t indent)
59 {
60 return std::string(indent*2, ' ');
61 }
62
63 template<class t_stream>
64 struct array_entry_store_to_json_visitor: public boost::static_visitor<void>
65 {
66 t_stream& m_strm;
67 size_t m_indent;
69 array_entry_store_to_json_visitor(t_stream& strm, size_t indent,
70 bool insert_newlines = true)
71 : m_strm(strm), m_indent(indent), m_insert_newlines(insert_newlines)
72 {}
73
74 template<class t_type>
76 {
77 m_strm << "[";
78 if(a.m_array.size())
79 {
80 auto last_it = --a.m_array.end();
81 for(auto it = a.m_array.begin(); it != a.m_array.end(); it++)
82 {
84 if(it != last_it)
85 m_strm << ",";
86 }
87 }
88 m_strm << "]";
89 }
90 };
91
92 template<class t_stream>
93 struct storage_entry_store_to_json_visitor: public boost::static_visitor<void>
94 {
95 t_stream& m_strm;
96 size_t m_indent;
98 storage_entry_store_to_json_visitor(t_stream& strm, size_t indent,
99 bool insert_newlines = true)
100 : m_strm(strm), m_indent(indent), m_insert_newlines(insert_newlines)
101 {}
102 //section, array_entry
103 template<class visited_type>
104 void operator()(const visited_type& v)
105 {
107 }
108 };
109
110 template<class t_stream>
111 void dump_as_json(t_stream& strm, const array_entry& ae, size_t indent, bool insert_newlines)
112 {
113 array_entry_store_to_json_visitor<t_stream> aesv(strm, indent, insert_newlines);
114 boost::apply_visitor(aesv, ae);
115 }
116
117 template<class t_stream>
118 void dump_as_json(t_stream& strm, const storage_entry& se, size_t indent, bool insert_newlines)
119 {
120 storage_entry_store_to_json_visitor<t_stream> sv(strm, indent, insert_newlines);
121 boost::apply_visitor(sv, se);
122 }
123
124 template<class t_stream>
125 void dump_as_json(t_stream& strm, const std::string& v, size_t indent, bool insert_newlines)
126 {
127 strm << "\"" << misc_utils::parse::transform_to_escape_sequence(v) << "\"";
128 }
129
130 template<class t_stream>
131 void dump_as_json(t_stream& strm, const int8_t& v, size_t indent, bool insert_newlines)
132 {
133 strm << static_cast<int32_t>(v);
134 }
135
136 template<class t_stream>
137 void dump_as_json(t_stream& strm, const uint8_t& v, size_t indent, bool insert_newlines)
138 {
139 strm << static_cast<int32_t>(v);
140 }
141
142 template<class t_stream>
143 void dump_as_json(t_stream& strm, const bool& v, size_t indent, bool insert_newlines)
144 {
145 if(v)
146 strm << "true";
147 else
148 strm << "false";
149 }
150
151
152
153 template<class t_stream, class t_type>
154 void dump_as_json(t_stream& strm, const t_type& v, size_t indent, bool insert_newlines)
155 {
156 strm << v;
157 }
158
159 template<class t_stream>
160 void dump_as_json(t_stream& strm, const section& sec, size_t indent, bool insert_newlines)
161 {
162 size_t local_indent = indent + 1;
163 std::string newline = insert_newlines ? "\r\n" : "";
164 strm << "{" << newline;
165 std::string indent_str = make_indent(local_indent);
166 if(sec.m_entries.size())
167 {
168 auto it_last = --sec.m_entries.end();
169 for(auto it = sec.m_entries.begin(); it!= sec.m_entries.end();it++)
170 {
171 strm << indent_str << "\"" << misc_utils::parse::transform_to_escape_sequence(it->first) << "\"" << ": ";
172 dump_as_json(strm, it->second, local_indent, insert_newlines);
173 if(it_last != it)
174 strm << ",";
175 strm << newline;
176 }
177 }
178 strm << make_indent(indent) << "}";
179 }
180 }
181}
std::string transform_to_escape_sequence(const std::string &src)
Definition parserse_base_utils.cpp:42
Definition keyvalue_serialization_overloads.h:49
std::string make_indent(size_t indent)
Definition portable_storage_to_json.h:58
void dump_as_json(t_stream &strm, const array_entry &ae, size_t indent, bool insert_newlines)
Definition portable_storage_to_json.h:111
boost::variant< uint64_t, uint32_t, uint16_t, uint8_t, int64_t, int32_t, int16_t, int8_t, double, bool, std::string, section, array_entry > storage_entry
Definition portable_storage_base.h:161
boost::make_recursive_variant< array_entry_t< section >, array_entry_t< uint64_t >, array_entry_t< uint32_t >, array_entry_t< uint16_t >, array_entry_t< uint8_t >, array_entry_t< int64_t >, array_entry_t< int32_t >, array_entry_t< int16_t >, array_entry_t< int8_t >, array_entry_t< double >, array_entry_t< bool >, array_entry_t< std::string >, array_entry_t< section >, array_entry_t< boost::recursive_variant_ > >::type array_entry
Definition portable_storage_base.h:159
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1124
unsigned char uint8_t
Definition stdint.h:124
signed char int8_t
Definition stdint.h:121
Definition portable_storage_to_json.h:65
array_entry_store_to_json_visitor(t_stream &strm, size_t indent, bool insert_newlines=true)
Definition portable_storage_to_json.h:69
bool m_insert_newlines
Definition portable_storage_to_json.h:68
void operator()(const array_entry_t< t_type > &a)
Definition portable_storage_to_json.h:75
t_stream & m_strm
Definition portable_storage_to_json.h:66
size_t m_indent
Definition portable_storage_to_json.h:67
Definition portable_storage_base.h:83
Definition portable_storage_base.h:169
std::map< std::string, storage_entry > m_entries
Definition portable_storage_base.h:170
Definition portable_storage_to_json.h:94
size_t m_indent
Definition portable_storage_to_json.h:96
storage_entry_store_to_json_visitor(t_stream &strm, size_t indent, bool insert_newlines=true)
Definition portable_storage_to_json.h:98
t_stream & m_strm
Definition portable_storage_to_json.h:95
bool m_insert_newlines
Definition portable_storage_to_json.h:97
void operator()(const visited_type &v)
Definition portable_storage_to_json.h:104