8 #include <boost/archive/iterators/base64_from_binary.hpp>
9 #include <boost/archive/iterators/binary_from_base64.hpp>
10 #include <boost/archive/iterators/transform_width.hpp>
11 #include <boost/algorithm/string.hpp>
25 return m_isVectorPayload;
29 m_isVectorPayload = isVectorPayload;
34 for(
auto&
t: s_typeToString){
35 if(
t.second.compare(
type)==0)
45 throw std::out_of_range(
"Invalid integer value for TypeId");
52 using namespace boost::archive::iterators;
53 typedef base64_from_binary<transform_width<const char *, 6, 8>> base64_encoder;
55 std::string encodedData;
56 std::copy(base64_encoder(
data), base64_encoder(
data + len), std::back_inserter(encodedData));
58 size_t numPaddingChars = (3 - len % 3) % 3;
59 encodedData.append(numPaddingChars,
'=');
67 using namespace boost::archive::iterators;
68 typedef transform_width<binary_from_base64<std::string::const_iterator>, 8, 6> base64_decoder;
70 std::string encodedDataNoPadding = encodedData;
71 boost::algorithm::erase_all(encodedDataNoPadding,
"=");
73 std::vector<unsigned char> decodedData;
74 std::copy(base64_decoder(encodedDataNoPadding.begin()), base64_decoder(encodedDataNoPadding.end()), std::back_inserter(decodedData));
81 m_payload_spec.emplace_back(
name,
type);
86 addColumn(
name, stringToTypeId(
type));
96 auto it = std::find_if(m_payload_spec.begin(), m_payload_spec.end(),
98 { return column.first == name; });
100 if (
it != m_payload_spec.end())
102 m_row[
name] = nlohmann::json::value_t::null;
112 for (
auto &
column : m_payload_spec)
122 m_row[
name] = va_arg(
ap,
int);
125 m_row[
name] =
static_cast<unsigned char>(va_arg(
ap,
int));
129 m_row[
name] = va_arg(
ap,
int);
133 m_row[
name] = va_arg(
ap,
unsigned int);
139 m_row[
name] = va_arg(
ap, int64_t);
143 m_row[
name] = va_arg(
ap,
double);
151 m_row[
name] = std::string(va_arg(
ap,
const char *));
168 validatePayloadSize(
data);
171 for (
const auto &data_row :
data)
173 auto row_arr_data = createRowArray(data_row);
175 if (m_isVectorPayload)
177 arr_data.push_back(row_arr_data);
181 arr_data = row_arr_data;
185 m_vector_data.clear();
191 if (!
data.is_array())
192 throw CommonCrestException(
"The format of data is wrong. It should be the vector (size 1 for non vector payload)");
193 if (!m_isVectorPayload && m_vector_data.size() > 1)
195 throw CommonCrestException(
"The payload is not a vector, but the size seems to be larger than 1....");
202 for (
const auto &
column : m_payload_spec)
204 if (data_row.find(
column.first) == data_row.end())
206 std::string
msg =
"The data does not contain the column: " +
column.first;
209 row_arr_data.push_back(data_row[
column.first]);
216 m_iov_data[
"since"] =
since;
217 m_iov_data[
"data"] = m_payload;
223 m_iov_data[
"since"]=
since;
228 std::vector<std::string> chs;
229 for (
auto&
x : m_iov_data[
"data"].
items()){
230 chs.push_back(
x.key());
236 return m_payload_spec;
241 if (m_payload.empty())
243 m_payload = m_iov_data[
"data"];
246 if (
it == m_payload.end())
248 std::string
msg =
"Channel id " + std::string(
channel_id) +
" is not found.";
253 for (
const auto &
column : m_payload_spec)
258 throw std::runtime_error(
"Column name not found.");
268 auto it = std::find_if(m_payload_spec.begin(), m_payload_spec.end(),
270 { return column.first == name; });
272 if (
it != m_payload_spec.end())
284 if (m_isVectorPayload)
286 m_vector_data.push_back(m_row);
291 std::string
msg =
"The payload is not a vector.";
298 if (m_isVectorPayload)
299 return m_vector_data;
300 m_vector_data.push_back(m_row);
301 return m_vector_data;
306 if (m_payload.empty() && m_iov_data.empty())
308 std::string
msg =
"The payload is empty.";
311 if (m_payload.empty())
313 m_payload = m_iov_data[
"data"];
320 if (m_iov_data.empty())
322 std::string
msg =
"The iov data is empty.";
340 return getPayload().dump();
345 return m_iov_data.dump();
351 for (
auto &
column : m_payload_spec)
354 std::map<TypeId, std::string>::const_iterator
pos = Crest::s_typeToString.find(
column.second);
355 if (
pos == Crest::s_typeToString.
end()) {
360 pspec_data.push_back(j);
369 for (
const auto &
column : j)
373 addColumn(
name,
static_cast<TypeId>(std::stoi(
type.get<std::string>())));
379 for (
const auto &[
name,
type] : j.items())
390 m_vector_data.clear();
398 m_vector_data.clear();
408 file << std::setprecision(6) << j.dump(4);
413 std::cerr <<
"CondContainer::dump_json_to_file: Error opening file: " <<
filename << std::endl;
421 std::ifstream specfile(spec_filename);
423 if (specfile.is_open())
430 std::cerr <<
"CondContainer::read_json_from_file: Error opening file: " << spec_filename << std::endl;
433 setPayloadSpec(jspec);
444 std::cerr <<
"CondContainer::read_json_from_file: Error opening file: " <<
filename << std::endl;
451 if (thisVal.is_null()){
452 m_row[colName] =
"NULL";
456 if(strVal.size()>2&& strVal[0]==
'"'&& strVal[strVal.size()-1]==
'"')
457 strVal=strVal.substr(1,strVal.size()-2);
458 if((strVal.compare(
"NULL")==0||strVal.compare(
"null")==0)&&
463 m_row[colName] =
"NULL";
469 const bool newVal=(strVal ==
"true");
470 m_row[colName] = newVal;
475 m_row[colName]=std::stoul(strVal);
480 m_row[colName]=std::stol(strVal);
485 m_row[colName]=std::stoul(strVal);
490 m_row[colName]=std::stoi(strVal);
495 m_row[colName]=std::stoull(strVal);
500 m_row[colName]=std::stoull(strVal);
505 m_row[colName]=std::stoll(strVal);
510 m_row[colName]=std::stof(strVal);
515 m_row[colName]=std::stod(strVal);
524 m_row[colName]=thisVal.get<std::string>();
531 m_row[colName]=thisVal.get<std::string>();
536 throw std::runtime_error(
"UNTREATED TYPE!");
543 std::cerr <<
e.what() << std::endl;
544 throw std::runtime_error(
e.what());
553 ss >> std::quoted(st);
559 if (j.contains(
"data") && j[
"data"].is_object())
561 const auto &
data = j[
"data"];
566 std::string channelKey =
channel.key();
567 const auto &data_ch =
channel.value();
569 if(m_isVectorPayload)
572 vecJson.push_back(data_ch);
574 for (
const auto &
values : vecJson)
578 if (
values.is_array() &&
values.size() == m_payload_spec.size())
580 for (
size_t i = 0;
i <
values.size(); ++
i)
582 const auto &
spec = m_payload_spec[
i];
583 std::string colName =
spec.first;
588 parseOldFormat(colName,colType,
values[
i]);
594 m_row[colName] =
values[
i].get<
bool>();
597 m_row[colName] =
values[
i].get<
unsigned char>();
601 m_row[colName] =
values[
i].get<
int>();
605 m_row[colName] =
values[
i].get<
unsigned int>();
611 m_row[colName] =
values[
i].get<int64_t>();
615 m_row[colName] =
values[
i].get<
double>();
622 m_row[colName] =
values[
i].get<std::string>();
627 m_row[colName] =
values[
i].get<std::string>();
636 std::cerr <<
"CrestContainer::from_json: Mismatch in number of values for channel " << channelKey << std::endl;
638 if(m_isVectorPayload)
641 addData(channelKey.c_str());
646 std::cerr <<
"CrestContainer::from_json: JSON does not contain a 'data' object or it is not a JSON object." << std::endl;
647 std::cerr <<
"CrestContainer::from_json json:"<<j<<std::endl;