Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 257a2bf

Browse files
author
jezal
committed
New JSON serialization
1 parent 640efb3 commit 257a2bf

17 files changed

+2248
-2
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ else()
3131
else()
3232
set(ARCH_FLAG "-march=${ARCH}")
3333
endif()
34-
set(WARNINGS "-Wall -Wextra -Wpointer-arith -Wundef -Wvla -Wwrite-strings -Werror -Wno-error=extra -Wno-error=deprecated-declarations -Wno-error=sign-compare -Wno-error=strict-aliasing -Wno-error=type-limits -Wno-unused-parameter -Wno-error=unused-variable -Wno-error=undef -Wno-error=uninitialized")
34+
set(WARNINGS "-Wall -Wextra -Wpointer-arith -Wundef -Wvla -Wwrite-strings -Werror -Wno-error=extra -Wno-error=deprecated-declarations -Wno-error=sign-compare -Wno-error=strict-aliasing -Wno-error=type-limits -Wno-unused-parameter -Wno-error=unused-variable -Wno-error=undef -Wno-error=uninitialized -Wno-error=unused-result")
3535
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
36-
set(WARNINGS "${WARNINGS} -Wno-error=mismatched-tags -Wno-error=null-conversion -Wno-overloaded-shift-op-parentheses -Wno-error=shift-count-overflow -Wno-error=tautological-constant-out-of-range-compare -Wno-error=unused-private-field -Wno-error=unneeded-internal-declaration")
36+
set(WARNINGS "${WARNINGS} -Wno-error=mismatched-tags -Wno-error=null-conversion -Wno-overloaded-shift-op-parentheses -Wno-error=shift-count-overflow -Wno-error=tautological-constant-out-of-range-compare -Wno-error=unused-private-field -Wno-error=unneeded-internal-declaration -Wno-error=unused-function")
3737
else()
3838
set(WARNINGS "${WARNINGS} -Wlogical-op -Wno-error=maybe-uninitialized")
3939
endif()
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
// Copyright (c) 2012-2014, The CryptoNote developers, The Bytecoin developers
2+
//
3+
// This file is part of Bytecoin.
4+
//
5+
// Bytecoin is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Lesser General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Bytecoin is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Lesser General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Lesser General Public License
16+
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
17+
18+
#include "BinaryInputStreamSerializer.h"
19+
#include "SerializationOverloads.h"
20+
21+
#include <algorithm>
22+
#include <cassert>
23+
#include <stdexcept>
24+
25+
namespace {
26+
27+
void deserialize(std::istream& stream, uint8_t& v) {
28+
char c;
29+
stream.get(c);
30+
v = static_cast<uint8_t>(c);
31+
}
32+
33+
void deserialize(std::istream& stream, int8_t& v) {
34+
uint8_t val;
35+
deserialize(stream, val);
36+
v = val;
37+
}
38+
39+
void deserialize(std::istream& stream, bool& v) {
40+
uint8_t val;
41+
deserialize(stream, val);
42+
43+
v = val;
44+
}
45+
46+
void deserialize(std::istream& stream, uint32_t& v) {
47+
char c;
48+
49+
stream.get(c);
50+
v = static_cast<uint8_t>(c);
51+
52+
stream.get(c);
53+
v += static_cast<uint8_t>(c) << 8;
54+
55+
stream.get(c);
56+
v += static_cast<uint8_t>(c) << 16;
57+
58+
stream.get(c);
59+
v += static_cast<uint8_t>(c) << 24;
60+
}
61+
62+
void deserialize(std::istream& stream, int32_t& v) {
63+
uint32_t val;
64+
deserialize(stream, val);
65+
v = val;
66+
}
67+
68+
void deserialize(std::istream& stream, uint64_t& v) {
69+
char c;
70+
uint64_t uc;
71+
72+
stream.get(c);
73+
uc = static_cast<unsigned char>(c);
74+
v = uc;
75+
76+
stream.get(c);
77+
uc = static_cast<unsigned char>(c);
78+
v += (uc << 8);
79+
80+
stream.get(c);
81+
uc = static_cast<unsigned char>(c);
82+
v += (uc << 16);
83+
84+
stream.get(c);
85+
uc = static_cast<unsigned char>(c);
86+
v += (uc << 24);
87+
88+
stream.get(c);
89+
uc = static_cast<unsigned char>(c);
90+
v += (uc << 32);
91+
92+
stream.get(c);
93+
uc = static_cast<unsigned char>(c);
94+
v += (uc << 40);
95+
96+
stream.get(c);
97+
uc = static_cast<unsigned char>(c);
98+
v += (uc << 48);
99+
100+
stream.get(c);
101+
uc = static_cast<unsigned char>(c);
102+
v += (uc << 56);
103+
}
104+
105+
void deserialize(std::istream& stream, int64_t& v) {
106+
uint64_t val;
107+
deserialize(stream, val);
108+
v = val;
109+
}
110+
111+
void deserialize(std::istream& stream, char* buf, size_t len) {
112+
const size_t chunk = 1000;
113+
114+
// stream.read(buf, len);
115+
116+
// looks redundant, but i had a bug with it
117+
while (len && stream) {
118+
size_t toRead = std::min(len, chunk);
119+
stream.read(buf, toRead);
120+
len -= toRead;
121+
buf += toRead;
122+
}
123+
}
124+
125+
}
126+
127+
namespace cryptonote {
128+
129+
ISerializer::SerializerType BinaryInputStreamSerializer::type() const {
130+
return ISerializer::INPUT;
131+
}
132+
133+
ISerializer& BinaryInputStreamSerializer::beginObject(const std::string& name) {
134+
return *this;
135+
}
136+
137+
ISerializer& BinaryInputStreamSerializer::endObject() {
138+
return *this;
139+
}
140+
141+
ISerializer& BinaryInputStreamSerializer::beginArray(std::size_t& size, const std::string& name) {
142+
uint64_t val;
143+
serializeVarint(val, name, *this);
144+
size = val;
145+
146+
return *this;
147+
}
148+
149+
ISerializer& BinaryInputStreamSerializer::endArray() {
150+
return *this;
151+
}
152+
153+
ISerializer& BinaryInputStreamSerializer::operator()(uint8_t& value, const std::string& name) {
154+
deserialize(stream, value);
155+
156+
return *this;
157+
}
158+
159+
ISerializer& BinaryInputStreamSerializer::operator()(uint32_t& value, const std::string& name) {
160+
deserialize(stream, value);
161+
162+
return *this;
163+
}
164+
165+
ISerializer& BinaryInputStreamSerializer::operator()(int32_t& value, const std::string& name) {
166+
uint32_t v;
167+
operator()(v, name);
168+
value = v;
169+
170+
return *this;
171+
}
172+
173+
ISerializer& BinaryInputStreamSerializer::operator()(int64_t& value, const std::string& name) {
174+
deserialize(stream, value);
175+
176+
return *this;
177+
}
178+
179+
ISerializer& BinaryInputStreamSerializer::operator()(uint64_t& value, const std::string& name) {
180+
deserialize(stream, value);
181+
182+
return *this;
183+
}
184+
185+
ISerializer& BinaryInputStreamSerializer::operator()(bool& value, const std::string& name) {
186+
deserialize(stream, value);
187+
188+
return *this;
189+
}
190+
191+
ISerializer& BinaryInputStreamSerializer::operator()(std::string& value, const std::string& name) {
192+
uint64_t size;
193+
serializeVarint(size, name, *this);
194+
195+
std::vector<char> temp;
196+
temp.resize(size);
197+
198+
deserialize(stream, &temp[0], size);
199+
200+
value.reserve(size);
201+
value.assign(&temp[0], size);
202+
203+
return *this;
204+
}
205+
206+
ISerializer& BinaryInputStreamSerializer::operator()(char* value, std::size_t size, const std::string& name) {
207+
stream.read(value, size);
208+
209+
return *this;
210+
}
211+
212+
ISerializer& BinaryInputStreamSerializer::tag(const std::string& name) {
213+
return *this;
214+
}
215+
216+
ISerializer& BinaryInputStreamSerializer::untagged(uint8_t& value) {
217+
char v;
218+
stream.get(v);
219+
value = v;
220+
221+
return *this;
222+
}
223+
224+
ISerializer& BinaryInputStreamSerializer::endTag() {
225+
return *this;
226+
}
227+
228+
bool BinaryInputStreamSerializer::hasObject(const std::string& name) {
229+
assert(false); //the method is not supported for this type of serialization
230+
throw std::runtime_error("hasObject method is not supported in BinaryInputStreamSerializer");
231+
232+
return false;
233+
}
234+
235+
ISerializer& BinaryInputStreamSerializer::operator()(double& value, const std::string& name) {
236+
assert(false); //the method is not supported for this type of serialization
237+
throw std::runtime_error("double serialization is not supported in BinaryInputStreamSerializer");
238+
239+
return *this;
240+
}
241+
242+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) 2012-2014, The CryptoNote developers, The Bytecoin developers
2+
//
3+
// This file is part of Bytecoin.
4+
//
5+
// Bytecoin is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Lesser General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Bytecoin is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Lesser General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Lesser General Public License
16+
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
17+
18+
#pragma once
19+
20+
#include "ISerializer.h"
21+
#include "SerializationOverloads.h"
22+
23+
#include <istream>
24+
25+
namespace cryptonote {
26+
27+
class BinaryInputStreamSerializer : public ISerializer {
28+
public:
29+
BinaryInputStreamSerializer(std::istream& strm) : stream(strm) {}
30+
virtual ~BinaryInputStreamSerializer() {}
31+
32+
virtual ISerializer::SerializerType type() const;
33+
34+
virtual ISerializer& beginObject(const std::string& name) override;
35+
virtual ISerializer& endObject() override;
36+
37+
virtual ISerializer& beginArray(std::size_t& size, const std::string& name) override;
38+
virtual ISerializer& endArray() override;
39+
40+
virtual ISerializer& operator()(uint8_t& value, const std::string& name) override;
41+
virtual ISerializer& operator()(int32_t& value, const std::string& name) override;
42+
43+
virtual ISerializer& operator()(uint32_t& value, const std::string& name) override;
44+
virtual ISerializer& operator()(int64_t& value, const std::string& name) override;
45+
virtual ISerializer& operator()(uint64_t& value, const std::string& name) override;
46+
virtual ISerializer& operator()(double& value, const std::string& name) override;
47+
virtual ISerializer& operator()(bool& value, const std::string& name) override;
48+
virtual ISerializer& operator()(std::string& value, const std::string& name) override;
49+
virtual ISerializer& operator()(char* value, std::size_t size, const std::string& name);
50+
51+
virtual ISerializer& tag(const std::string& name) override;
52+
virtual ISerializer& untagged(uint8_t& value) override;
53+
virtual ISerializer& endTag() override;
54+
55+
virtual bool hasObject(const std::string& name) override;
56+
57+
template<typename T>
58+
ISerializer& operator()(T& value, const std::string& name) {
59+
return ISerializer::operator()(value, name);
60+
}
61+
62+
private:
63+
std::istream& stream;
64+
};
65+
66+
}

0 commit comments

Comments
 (0)