8 #include "CoolKernel/RecordSpecification.h"
9 #include "CoolKernel/Record.h"
10 #include "CoralBase/AttributeList.h"
11 #include "CoralBase/Attribute.h"
13 #include "boost/regex.hpp"
26 const std::map<std::string, cool::StorageType::TypeId> typeCorrespondance={
28 {
"Bool", StorageType::Bool},
29 {
"UChar",StorageType::UChar},
30 {
"Int16", StorageType::Int16},
31 {
"UInt16", StorageType::UInt16},
32 {
"Int32", StorageType::Int32},
33 {
"UInt32", StorageType::UInt32},
34 {
"UInt63",StorageType::UInt63},
35 {
"Int64", StorageType::Int64},
36 {
"Float", StorageType::Float},
37 {
"Double", StorageType::Double},
38 {
"String255", StorageType::String255},
39 {
"String4k", StorageType::String4k},
40 {
"String64k", StorageType::String64k},
41 {
"String16M", StorageType::String16M},
42 {
"String128M", StorageType::String128M},
43 {
"Blob64k", StorageType::Blob64k},
44 {
"Blob16M", StorageType::Blob16M},
45 {
"Blob128M", StorageType::Blob128M}
61 if (not
s.good() or
s.eof()){
62 const std::string
msg(
"Json2Cool constructor; Input is invalid and could not be opened.");
63 throw std::runtime_error(
msg);
69 std::cout<<
"ERROR AT LINE "<<__LINE__<<
" of "<<__FILE__<<std::endl;
70 std::cout<<
e.what()<<std::endl;
73 const auto &
payload=(j.contains(
"data")) ? j[
"data"] : j;
83 const std::string& ks=
k.key();
84 const long long key=std::stoll(ks);
85 std::vector<coral::AttributeList> tempVector;
86 for (json::const_iterator
i=
f.begin();
i!=
f.end();++
i){
87 const json& arrayElem=
i.value();
89 const auto & attList=
r.attributeList();
90 tempVector.push_back(attList);
98 const std::string& ks=
i.key();
99 const long long key=std::stoll(ks);
101 const auto & attList=
r.attributeList();
110 cool::RecordSpecification *
112 if (stringSpecification.empty())
return nullptr;
113 auto *
spec =
new cool::RecordSpecification();
115 std::string inputObj=stringSpecification;
118 for (
unsigned int i = 0;
i < nl.size();
i++)
121 auto it = nl[
i].items().begin();
122 std::string
n((*it).key());
123 std::string
t((*it).value());
124 spec->extend(
n, typeCorrespondance.find(
t)->second);
130 std::string input(stringSpecification);
132 std::string
regex=R
"delim(([^\s,:]*):\s?([^\s,]*),?)delim";
138 std::string
n(
what[1]);
139 std::string
t(
what[2]);
141 spec->extend(
n, typeCorrespondance.find(
t)->second);
142 input =
what.suffix();
151 cool::Record
a(*pSpec);
152 unsigned int s=
a.size();
154 json::const_iterator
it = j.begin();
155 for (
unsigned int i(0);
i!=
s;++
i){
160 const auto thisVal =
it.value();
167 if (thisVal.is_null()){
171 cool::StorageType::TypeId typespec =
f.storageType().id();
173 if(strVal.size()>2&& strVal[0]==
'"'&& strVal[strVal.size()-1]==
'"')
174 strVal=strVal.substr(1,strVal.size()-2);
176 if((strVal.compare(
"NULL")==0||strVal.compare(
"null")==0)&&
177 (typespec==StorageType::Bool || typespec==StorageType::Int16 || typespec==StorageType::UInt16
178 || typespec==StorageType::Int32 || typespec==StorageType::UInt32
179 || typespec==StorageType::Int64 || typespec==StorageType::UInt63
180 || typespec==StorageType::Float || typespec==StorageType::Double)){
185 case StorageType::Bool:
187 const bool newVal=(strVal ==
"true");
188 att.setValue<
bool>(newVal);
191 case StorageType::UChar:
193 const unsigned char newVal=std::stoul(strVal);
194 att.setValue<
unsigned char>(newVal);
197 case StorageType::Int16:
199 const short newVal=std::stol(strVal);
200 att.setValue<
short>(newVal);
203 case StorageType::UInt16:
205 const unsigned short newVal=std::stoul(strVal);
206 att.setValue<
unsigned short>(newVal);
209 case StorageType::Int32:
211 const int newVal=std::stoi(strVal);
212 att.setValue<
int>(newVal);
215 case StorageType::UInt32:
217 const unsigned int newVal=std::stoull(strVal);
218 att.setValue<
unsigned int>(newVal);
221 case StorageType::UInt63:
223 const unsigned long long newVal=std::stoull(strVal);
224 att.setValue<
unsigned long long>(newVal);
227 case StorageType::Int64:
229 const long long newVal=std::stoll(strVal);
230 att.setValue<
long long>(newVal);
233 case StorageType::Float:
235 const float newVal=std::stof(strVal);
236 att.setValue<
float>(newVal);
239 case StorageType::Double:
241 const double newVal=std::stod(strVal);
242 att.setValue<
double>(newVal);
245 case StorageType::String255:
246 case StorageType::String4k:
247 case StorageType::String64k:
248 case StorageType::String16M:
249 case StorageType::String128M:
251 att.setValue<std::string>(thisVal.get<std::string>());
254 case StorageType::Blob128M:
255 case StorageType::Blob16M:
256 case StorageType::Blob64k:
265 for (
auto& [
key,
val] : typeCorrespondance) {
274 std::string errorMessage(
"UNTREATED TYPE! " +
typeName);
275 std::cerr << errorMessage << std::endl;
276 throw std::runtime_error(errorMessage);
282 std::cerr <<
e.what() << std::endl;
283 throw std::runtime_error(
e.what());