17 #ifndef AVROBYTEARRAYCONVERTER_HPP_
18 #define AVROBYTEARRAYCONVERTER_HPP_
25 #include <avro/Compiler.hh>
26 #include <avro/Specific.hh>
27 #include <avro/Stream.hh>
28 #include <avro/Encoder.hh>
29 #include <avro/Decoder.hh>
63 T
fromByteArray(
const std::uint8_t* data,
const std::uint32_t& dataSize);
72 void fromByteArray(
const std::uint8_t* data,
const std::uint32_t& dataSize, T& datum);
87 void toByteArray(
const T& datum, std::vector<std::uint8_t>& dest);
94 void toByteArray(
const T& datum, std::ostream& stream);
100 encoder_ = avro::jsonEncoder(schema);
101 decoder_ = avro::jsonDecoder(schema);
105 encoder_ = avro::binaryEncoder();
106 decoder_ = avro::binaryDecoder();
110 avro::EncoderPtr encoder_;
111 avro::DecoderPtr decoder_;
123 if ((data && !dataSize) || (dataSize && !data)) {
128 std::unique_ptr<avro::InputStream> in = avro::memoryInputStream(data, dataSize);
131 avro::decode(*decoder_, datum);
139 if ((data && !dataSize) || (dataSize && !data)) {
143 std::unique_ptr<avro::InputStream> in = avro::memoryInputStream(data, dataSize);
146 avro::decode(*decoder_, datum);
152 std::stringstream ostream;
153 std::unique_ptr<avro::OutputStream> out = avro::ostreamOutputStream(ostream);
155 encoder_->init(*out);
156 avro::encode(*encoder_, datum);
161 std::streampos beg = ostream.tellg();
162 ostream.seekg(0, std::ios_base::end);
164 std::streampos end = ostream.tellg();
165 ostream.seekg(0, std::ios_base::beg);
167 buffer.second = end - beg;
168 buffer.first.reset(
new uint8_t[buffer.second]);
169 std::copy(std::istreambuf_iterator<char>(ostream), std::istreambuf_iterator<char>(), buffer.first.get());
177 std::stringstream ostream;
178 std::unique_ptr<avro::OutputStream> out = avro::ostreamOutputStream(ostream);
180 encoder_->init(*out);
181 avro::encode(*encoder_, datum);
184 std::streampos beg = ostream.tellg();
185 ostream.seekg(0, std::ios_base::end);
187 std::streampos end = ostream.tellg();
188 ostream.seekg(0, std::ios_base::beg);
190 dest.reserve(end - beg);
191 dest.assign(std::istreambuf_iterator<char>(ostream), std::istreambuf_iterator<char>());
197 std::unique_ptr<avro::OutputStream> out = avro::ostreamOutputStream(stream);
199 encoder_->init(*out);
200 avro::encode(*encoder_, datum);
void switchToJson(const avro::ValidSchema &schema)
SharedDataBuffer toByteArray(const T &datum)
T fromByteArray(const std::uint8_t *data, const std::uint32_t &dataSize)
std::pair< boost::shared_array< std::uint8_t >, std::uint32_t > SharedDataBuffer