| 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 |
#pragma once |
| 28 |
|
| 29 |
#include <string> |
| 30 |
|
| 31 |
#include "byte_slice.h" |
| 32 |
#include "parserse_base_utils.h" |
| 33 |
#include "portable_storage.h" |
| 34 |
#include "file_io_utils.h" |
| 35 |
#include "span.h" |
| 36 |
|
| 37 |
namespace epee |
| 38 |
{ |
| 39 |
class byte_stream; |
| 40 |
|
| 41 |
namespace serialization |
| 42 |
{ |
| 43 |
|
| 44 |
template<class t_struct> |
| 45 |
bool load_t_from_json(t_struct& out, const std::string& json_buff) |
| 46 |
{ |
| 47 |
portable_storage ps; |
| 48 |
bool rs = ps.load_from_json(json_buff); |
| 49 |
if(!rs) |
| 50 |
return false; |
| 51 |
|
| 52 |
return out.load(ps); |
| 53 |
} |
| 54 |
|
| 55 |
template<class t_struct> |
| 56 |
bool load_t_from_json_file(t_struct& out, const std::string& json_file) |
| 57 |
{ |
| 58 |
std::string f_buff; |
| 59 |
if(!file_io_utils::load_file_to_string(json_file, f_buff)) |
| 60 |
return false; |
| 61 |
|
| 62 |
return load_t_from_json(out, f_buff); |
| 63 |
} |
| 64 |
|
| 65 |
template<class t_struct> |
| 66 |
bool store_t_to_json(t_struct& str_in, std::string& json_buff, size_t indent = 0, bool insert_newlines = true) |
| 67 |
{ |
| 68 |
portable_storage ps; |
| 69 |
str_in.store(ps); |
| 70 |
ps.dump_as_json(json_buff, indent, insert_newlines); |
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
template<class t_struct> |
| 75 |
std::string store_t_to_json(t_struct& str_in, size_t indent = 0, bool insert_newlines = true) |
| 76 |
{ |
| 77 |
std::string json_buff; |
| 78 |
store_t_to_json(str_in, json_buff, indent, insert_newlines); |
| 79 |
return json_buff; |
| 80 |
} |
| 81 |
|
| 82 |
template<class t_struct> |
| 83 |
bool store_t_to_json_file(t_struct& str_in, const std::string& fpath) |
| 84 |
{ |
| 85 |
std::string json_buff; |
| 86 |
store_t_to_json(str_in, json_buff); |
| 87 |
return file_io_utils::save_string_to_file(fpath, json_buff); |
| 88 |
} |
| 89 |
|
| 90 |
template<class t_struct> |
| 91 |
bool load_t_from_binary(t_struct& out, const epee::span<const uint8_t> binary_buff, const epee::serialization::portable_storage::limits_t *limits = NULL) |
| 92 |
{ |
| 93 |
portable_storage ps; |
| 94 |
bool rs = ps.load_from_binary(binary_buff, limits); |
| 95 |
if(!rs) |
| 96 |
return false; |
| 97 |
|
| 98 |
return out.load(ps); |
| 99 |
} |
| 100 |
|
| 101 |
template<class t_struct> |
| 102 |
bool load_t_from_binary(t_struct& out, const std::string& binary_buff) |
| 103 |
{ |
| 104 |
return load_t_from_binary(out, epee::strspan<uint8_t>(binary_buff)); |
| 105 |
} |
| 106 |
|
| 107 |
template<class t_struct> |
| 108 |
bool load_t_from_binary_file(t_struct& out, const std::string& binary_file) |
| 109 |
{ |
| 110 |
std::string f_buff; |
| 111 |
if(!file_io_utils::load_file_to_string(binary_file, f_buff)) |
| 112 |
return false; |
| 113 |
|
| 114 |
return load_t_from_binary(out, f_buff); |
| 115 |
} |
| 116 |
|
| 117 |
template<class t_struct> |
| 118 |
bool store_t_to_binary(t_struct& str_in, byte_slice& binary_buff, size_t initial_buffer_size = 8192) |
| 119 |
{ |
| 120 |
portable_storage ps; |
| 121 |
str_in.store(ps); |
| 122 |
return ps.store_to_binary(binary_buff, initial_buffer_size); |
| 123 |
} |
| 124 |
|
| 125 |
template<class t_struct> |
| 126 |
byte_slice store_t_to_binary(t_struct& str_in, size_t initial_buffer_size = 8192) |
| 127 |
{ |
| 128 |
byte_slice binary_buff; |
| 129 |
store_t_to_binary(str_in, binary_buff, initial_buffer_size); |
| 130 |
return binary_buff; |
| 131 |
} |
| 132 |
|
| 133 |
template<class t_struct> |
| 134 |
bool store_t_to_binary(t_struct& str_in, byte_stream& binary_buff) |
| 135 |
{ |
| 136 |
portable_storage ps; |
| 137 |
str_in.store(ps); |
| 138 |
return ps.store_to_binary(binary_buff); |
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
} |