| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#pragma once |
| 30 |
|
| 31 |
#include "misc_language.h" |
| 32 |
#include "portable_storage_base.h" |
| 33 |
#include "parserse_base_utils.h" |
| 34 |
|
| 35 |
namespace 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; |
| 68 |
bool m_insert_newlines; |
| 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> |
| 75 |
void operator()(const array_entry_t<t_type>& a) |
| 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 |
{ |
| 83 |
dump_as_json(m_strm, *it, m_indent, m_insert_newlines); |
| 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; |
| 97 |
bool m_insert_newlines; |
| 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 |
|
| 103 |
template<class visited_type> |
| 104 |
void operator()(const visited_type& v) |
| 105 |
{ |
| 106 |
dump_as_json(m_strm, v, m_indent, m_insert_newlines); |
| 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 |
} |