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;
86 const std::string& ks=
k.key();
87 const long long key=std::stoll(ks);
88 std::vector<coral::AttributeList> tempVector;
89 for (json::const_iterator
i=
f.begin();
i!=
f.end();++
i){
90 const json& arrayElem=
i.value();
92 const auto & attList=
r.attributeList();
93 tempVector.push_back(attList);
101 const std::string& ks=
i.key();
102 const long long key=std::stoll(ks);
104 const auto & attList=
r.attributeList();
113 cool::RecordSpecification *
115 if (stringSpecification.empty())
return nullptr;
116 std::string
input(stringSpecification);
117 auto *
spec =
new cool::RecordSpecification();
119 std::string
regex=R
"delim(([^\s,:]*):\s?([^\s,]*),?)delim";
125 std::string
n(
what[1]);
126 std::string
t(
what[2]);
128 spec->extend(
n, typeCorrespondance.find(
t)->second);
138 cool::Record
a(*pSpec);
139 unsigned int s=
a.size();
141 json::const_iterator
it = j.begin();
142 for (
unsigned int i(0);
i!=
s;++
i){
147 const auto thisVal =
it.value();
154 if (thisVal.is_null()){
158 cool::StorageType::TypeId typespec =
f.storageType().id();
160 if(strVal.size()>2&& strVal[0]==
'"'&& strVal[strVal.size()-1]==
'"')
161 strVal=strVal.substr(1,strVal.size()-2);
163 if((strVal.compare(
"NULL")==0||strVal.compare(
"null")==0)&&
164 (typespec==StorageType::Bool || typespec==StorageType::Int16 || typespec==StorageType::UInt16
165 || typespec==StorageType::Int32 || typespec==StorageType::UInt32
166 || typespec==StorageType::Int64 || typespec==StorageType::UInt63
167 || typespec==StorageType::Float || typespec==StorageType::Double)){
172 case StorageType::Bool:
174 const bool newVal=(strVal ==
"true");
175 att.setValue<
bool>(newVal);
178 case StorageType::UChar:
180 const unsigned char newVal=std::stoul(strVal);
181 att.setValue<
unsigned char>(newVal);
184 case StorageType::Int16:
186 const short newVal=std::stol(strVal);
187 att.setValue<
short>(newVal);
190 case StorageType::UInt16:
192 const unsigned short newVal=std::stoul(strVal);
193 att.setValue<
unsigned short>(newVal);
196 case StorageType::Int32:
198 const int newVal=std::stoi(strVal);
199 att.setValue<
int>(newVal);
202 case StorageType::UInt32:
204 const unsigned int newVal=std::stoull(strVal);
205 att.setValue<
unsigned int>(newVal);
208 case StorageType::UInt63:
210 const unsigned long long newVal=std::stoull(strVal);
211 att.setValue<
unsigned long long>(newVal);
214 case StorageType::Int64:
216 const long long newVal=std::stoll(strVal);
217 att.setValue<
long long>(newVal);
220 case StorageType::Float:
222 const float newVal=std::stof(strVal);
223 att.setValue<
float>(newVal);
226 case StorageType::Double:
228 const double newVal=std::stod(strVal);
229 att.setValue<
double>(newVal);
232 case StorageType::String255:
233 case StorageType::String4k:
234 case StorageType::String64k:
235 case StorageType::String16M:
236 case StorageType::String128M:
238 att.setValue<std::string>(thisVal.get<std::string>());
241 case StorageType::Blob128M:
242 case StorageType::Blob16M:
243 case StorageType::Blob64k:
252 for (
auto& [
key,
val] : typeCorrespondance) {
261 std::string errorMessage(
"UNTREATED TYPE! " +
typeName);
262 std::cerr << errorMessage << std::endl;
263 throw std::runtime_error(errorMessage);
269 std::cerr <<
e.what() << std::endl;
270 throw std::runtime_error(
e.what());