HOPS
HOPS class reference
MHO_ContainerJSONConverter.hh
Go to the documentation of this file.
1 #ifndef MHO_ContainerJSONConverter_HH__
2 #define MHO_ContainerJSONConverter_HH__
3 
4 #include "MHO_ClassIdentity.hh"
7 
8 #include "MHO_Axis.hh"
9 #include "MHO_AxisPack.hh"
10 #include "MHO_ObjectTags.hh"
11 #include "MHO_ScalarContainer.hh"
12 #include "MHO_TableContainer.hh"
13 #include "MHO_Taggable.hh"
14 #include "MHO_VectorContainer.hh"
15 
16 #include "MHO_NumpyTypeCode.hh"
17 
18 namespace hops
19 {
20 
37 //verbosity controlling enum
39 {
40  eJSONBasicLevel = 0, //basic quantities (rank, dimensions, etc.)
41  eJSONTagsLevel, //basic quantities plus tags
42  eJSONAxesLevel, //basic quantities plus the axes (if the object is a table)
43  eJSONAxesWithLabelsLevel, //basic quantities plus axes with interval labels
44  eJSONAllLevel //everything including the main data array
45 };
46 
47 //short hand aliases
53 
54 using hops::eJSONAll;
55 using hops::eJSONBasic;
56 using hops::eJSONTags;
57 using hops::eJSONWithAxes;
58 using hops::eJSONWithLabels;
59 
66 inline void FillJSONFromTaggable(const MHO_Taggable* map, mho_json& obj_tags)
67 {
68  bool ok;
69  obj_tags = map->GetMetaDataAsJSON();
70 }
71 
76 {
77  public:
78  MHO_JSONConverter(): fLOD(eJSONBasic), fRank(0), fRawByteSize(0), fRawData(nullptr), fRawDataDescriptor(""){};
79  virtual ~MHO_JSONConverter(){};
80 
86  void SetLevelOfDetail(int level) { fLOD = level; };
87 
93  mho_json* GetJSON() { return &fJSON; }
94 
106  virtual void ConstructJSONRepresentation() = 0;
107 
113  std::size_t GetRank() const { return fRank; }
114 
120  std::size_t GetRawByteSize() const { return fRawByteSize; };
121 
127  const char* GetRawData() const { return fRawData; };
128 
134  std::string GetRawDataDescriptor() const { return fRawDataDescriptor; }
135 
136  protected:
144  template< typename XValueType > void InsertElement(const XValueType& value, mho_json& data) { data.push_back(value); }
145 
156  void InsertElement(const std::complex< long double >& value, mho_json& data)
157  {
158  data.push_back({value.real(), value.imag()});
159  }
160 
167  void InsertElement(const std::complex< double >& value, mho_json& data)
168  {
169  data.push_back({value.real(), value.imag()});
170  }
171 
178  void InsertElement(const std::complex< float >& value, mho_json& data) { data.push_back({value.real(), value.imag()}); }
179 
180  //data
181  int fLOD;
183 
184  //for extracting container raw data (with no meta data structures)
185  std::size_t fRank;
186  std::size_t fRawByteSize;
187  const char* fRawData;
188  std::string fRawDataDescriptor;
189 };
190 
194 template< typename XContainerType > class MHO_ContainerJSONConverter: public MHO_JSONConverter
195 {
196  public:
197  MHO_ContainerJSONConverter(): MHO_JSONConverter() { fContainer = nullptr; }
198 
200  {
201  fContainer = dynamic_cast< XContainerType* >(element);
202  }
203 
205 
212  virtual void SetObjectToConvert(MHO_Serializable* obj) { fContainer = dynamic_cast< XContainerType* >(obj); };
213 
219  {
220  if(fContainer != nullptr)
221  {
222  ConstructJSON(fContainer);
223  }
224  }
225 
226  private:
227  XContainerType* fContainer;
228 
229  protected:
236  template< typename XCheckType > void ConstructJSON(const XCheckType* obj)
237  {
238  fJSON.clear();
239  std::string class_name = MHO_ClassIdentity::ClassName< XCheckType >();
240  std::string class_uuid = MHO_ClassIdentity::GetUUIDFromClass< XCheckType >().as_string();
241  fJSON["class_name"] = class_name;
242  fJSON["class_uuid"] = class_uuid;
243 
244  //for raw data extraction (not possible)
245  fRank = 0;
246  fRawByteSize = 0;
247  fRawData = nullptr;
248  fRawDataDescriptor = "";
249  };
250 
256  template< typename XCheckType = XContainerType >
257  typename std::enable_if< std::is_base_of< MHO_ScalarContainerBase, XCheckType >::value, void >::type
258  ConstructJSON(const XContainerType* obj)
259  {
260  fJSON.clear();
261  std::string class_name = MHO_ClassIdentity::ClassName< XContainerType >();
262  std::string class_uuid = MHO_ClassIdentity::GetUUIDFromClass< XContainerType >().as_string();
263  if(fLOD >= eJSONBasic)
264  {
265  fJSON["class_name"] = class_name;
266  fJSON["class_uuid"] = class_uuid;
267  fJSON["rank"] = fContainer->GetRank();
268  fJSON["total_size"] = fContainer->GetSize();
269  }
270 
271  if(fLOD >= eJSONTags)
272  {
273  mho_json jtags;
274  // FillJSONFromCommonMap(fContainer, jtags);
275  FillJSONFromTaggable(fContainer, jtags);
276  fJSON["tags"] = jtags;
277  }
278 
279  //just one element
280  if(fLOD >= eJSONAll)
281  {
282  mho_json data;
283  InsertElement(fContainer->GetData(), data);
284  fJSON["data"] = data;
285  }
286 
287  //for raw data extraction (not possible)
288  fRank = 0;
289  fRawByteSize = 0;
290  fRawData = nullptr;
291  fRawDataDescriptor = "";
292  };
293 
300  template< typename XCheckType = XContainerType >
301  typename std::enable_if< (std::is_base_of< MHO_VectorContainerBase, XCheckType >::value &&
302  !std::is_base_of< MHO_AxisBase, XCheckType >::value),
303  void >::type
304  ConstructJSON(const XContainerType* obj)
305  {
306  fJSON.clear();
307  std::string class_name = MHO_ClassIdentity::ClassName< XContainerType >();
308  std::string class_uuid = MHO_ClassIdentity::GetUUIDFromClass< XContainerType >().as_string();
309  if(fLOD >= eJSONBasic)
310  {
311  fJSON["class_name"] = class_name;
312  fJSON["class_uuid"] = class_uuid;
313  fJSON["rank"] = fContainer->GetRank();
314  fJSON["total_size"] = fContainer->GetSize();
315  fJSON["dimensions"] = fContainer->GetDimensionArray();
316  }
317 
318  if(fLOD >= eJSONTags)
319  {
320  mho_json jtags;
321  FillJSONFromTaggable(fContainer, jtags);
322  //FillJSONFromCommonMap(fContainer ,jtags);
323  fJSON["tags"] = jtags;
324  }
325 
326  //data goes out flat-packed into 1-d array
327  if(fLOD >= eJSONAll)
328  {
329  mho_json data;
330  for(auto it = fContainer->cbegin(); it != fContainer->cend(); it++)
331  {
332  InsertElement(*it, data);
333  }
334  fJSON["data"] = data;
335  }
336 
337  //for raw data extraction
338  std::size_t elem_size = sizeof(typename XContainerType::value_type);
339  std::size_t n_elem = fContainer->GetSize();
340  fRank = XContainerType::rank::value;
341  fRawByteSize = elem_size * n_elem;
342  fRawData = reinterpret_cast< const char* >(fContainer->GetData());
343  fRawDataDescriptor = MHO_NumpyTypeCode< typename XContainerType::value_type >();
344  };
345 
352  template< typename XCheckType = XContainerType >
353  typename std::enable_if< std::is_base_of< MHO_AxisBase, XCheckType >::value, void >::type
354  ConstructJSON(const XContainerType* obj)
355  {
356  fJSON.clear();
357  std::string class_name = MHO_ClassIdentity::ClassName< XContainerType >();
358  std::string class_uuid = MHO_ClassIdentity::GetUUIDFromClass< XContainerType >().as_string();
359  if(fLOD >= eJSONBasic)
360  {
361  fJSON["class_name"] = class_name;
362  fJSON["class_uuid"] = class_uuid;
363  fJSON["rank"] = fContainer->GetRank();
364  fJSON["total_size"] = fContainer->GetSize();
365  fJSON["dimensions"] = fContainer->GetDimensionArray();
366  }
367 
368  if(fLOD >= eJSONTags)
369  {
370  mho_json jtags;
371  FillJSONFromTaggable(fContainer, jtags);
372  //FillJSONFromCommonMap(fContainer ,jtags);
373  fJSON["tags"] = jtags;
374  }
375 
376  //data goes out flat-packed into 1-d array
377  if(fLOD >= eJSONAll)
378  {
379  mho_json data;
380  for(auto it = fContainer->cbegin(); it != fContainer->cend(); it++)
381  {
382  InsertElement(*it, data);
383  }
384  fJSON["data"] = data;
385  }
386 
387  //for raw data extraction
388  std::size_t elem_size = sizeof(typename XContainerType::value_type);
389  std::size_t n_elem = fContainer->GetSize();
390  fRank = XContainerType::rank::value;
391  fRawByteSize = elem_size * n_elem;
392  fRawData = reinterpret_cast< const char* >(fContainer->GetData());
393  fRawDataDescriptor = MHO_NumpyTypeCode< typename XContainerType::value_type >();
394  };
395 
402  template< typename XCheckType = XContainerType >
403  typename std::enable_if< std::is_base_of< MHO_TableContainerBase, XCheckType >::value, void >::type
404  ConstructJSON(const XContainerType* obj)
405  {
406  fJSON.clear();
407  std::string class_name = MHO_ClassIdentity::ClassName< XContainerType >();
408  std::string class_uuid = MHO_ClassIdentity::GetUUIDFromClass< XContainerType >().as_string();
409  if(fLOD >= eJSONBasic)
410  {
411  fJSON["class_name"] = class_name;
412  fJSON["class_uuid"] = class_uuid;
413  fJSON["rank"] = fContainer->GetRank();
414  fJSON["total_size"] = fContainer->GetSize();
415  fJSON["dimensions"] = fContainer->GetDimensionArray();
416  fJSON["strides"] = fContainer->GetStrideArray();
417  }
418 
419  if(fLOD >= eJSONTags)
420  {
421  mho_json jtags;
422  FillJSONFromTaggable(fContainer, jtags);
423  //FillJSONFromCommonMap(fContainer, jtags);
424  fJSON["tags"] = jtags;
425  }
426 
427  //data goes out flat-packed into 1-d array
428  if(fLOD >= eJSONAll)
429  {
430  mho_json data;
431  for(auto it = fContainer->cbegin(); it != fContainer->cend(); it++)
432  {
433  InsertElement(*it, data);
434  }
435  fJSON["data"] = data;
436  }
437 
438  if(fLOD >= eJSONWithAxes)
439  {
440  AxisDumper axis_dumper(&fJSON, fLOD);
441  for(std::size_t idx = 0; idx < obj->GetRank(); idx++)
442  {
443  axis_dumper.SetIndex(idx);
444  apply_at< typename XContainerType::axis_pack_tuple_type, AxisDumper >(*obj, idx, axis_dumper);
445  }
446  }
447 
448  //for raw data extraction
449  std::size_t elem_size = sizeof(typename XContainerType::value_type);
450  std::size_t n_elem = fContainer->GetSize();
451  fRank = XContainerType::rank::value;
452  fRawByteSize = elem_size * n_elem;
453  fRawData = reinterpret_cast< const char* >(fContainer->GetData());
454  fRawDataDescriptor = MHO_NumpyTypeCode< typename XContainerType::value_type >();
455  };
456 
461  {
462  public:
463  AxisDumper(mho_json* json_ptr, int level): fAxisJSON(json_ptr), fIndex(0), fLOD(level){};
465 
471  void SetIndex(std::size_t idx) { fIndex = idx; }
472 
473  template< typename XAxisType > void operator()(const XAxisType& axis)
474  {
475  mho_json j;
476  std::string class_name = MHO_ClassIdentity::ClassName< XAxisType >();
477  std::string class_uuid = MHO_ClassIdentity::GetUUIDFromClass< XAxisType >().as_string();
478  if(fLOD >= eJSONBasic)
479  {
480  j["class_name"] = class_name;
481  j["class_uuid"] = class_uuid;
482  j["rank"] = axis.GetRank();
483  j["total_size"] = axis.GetSize();
484  j["dimensions"] = axis.GetDimensionArray();
485  }
486 
487  if(fLOD >= eJSONTags)
488  {
489  mho_json jtags;
490  FillJSONFromTaggable(&axis, jtags);
491  //FillJSONFromCommonMap(&axis, jtags);
492  j["tags"] = jtags;
493  }
494 
495  //data goes out flat-packed into 1-d array
496  mho_json data;
497  for(auto it = axis.cbegin(); it != axis.cend(); it++)
498  {
499  data.push_back(*it);
500  }
501  j["data"] = data;
502 
503  // if(fLOD >= eJSONWithLabels)
504  // {
505  // //dump the axis labels too
506  // mho_json jilabels;
507  // MHO_Interval<std::size_t> all(0, axis.GetSize() );
508  // std::vector< MHO_IntervalLabel > labels = axis.GetIntervalsWhichIntersect(all);
509  // for(auto it = labels.begin(); it != labels.end(); it++)
510  // {
511  // mho_json label_obj;
512  // label_obj["lower_bound"] = it->GetLowerBound();
513  // label_obj["upper_bound"] = it->GetUpperBound();
514  // FillJSONFromCommonMap(&(*it), label_obj);
515  // jilabels.push_back(label_obj);
516  // }
517  // j["labels"] = jilabels;
518  // }
519 
520  std::stringstream ss;
521  ss << "axis_" << fIndex;
522  (*fAxisJSON)[ss.str().c_str()] = j;
523  };
524 
525  private:
526  mho_json* fAxisJSON;
527  std::size_t fIndex;
528  int fLOD;
529  };
530 };
531 
536 {
537  public:
538  MHO_ContainerJSONConverter(): MHO_JSONConverter() { fContainer = nullptr; }
539 
541  {
542  fContainer = dynamic_cast< MHO_ObjectTags* >(element);
543  }
544 
546 
553  virtual void SetObjectToConvert(MHO_Serializable* obj) { fContainer = dynamic_cast< MHO_ObjectTags* >(obj); };
554 
560  {
561  if(fContainer != nullptr)
562  {
563  ConstructJSON(fContainer);
564  }
565  }
566 
567  private:
573  void ConstructJSON(MHO_ObjectTags* obj)
574  {
575  fJSON.clear();
576  std::string class_name = MHO_ClassIdentity::ClassName< MHO_ObjectTags >();
577  std::string class_uuid = MHO_ClassIdentity::GetUUIDFromClass< MHO_ObjectTags >().as_string();
578  fJSON["class_name"] = class_name;
579  fJSON["class_uuid"] = class_uuid;
580 
581  std::vector< MHO_UUID > obj_uuids = obj->GetAllObjectUUIDs();
582  for(std::size_t i = 0; i < obj_uuids.size(); i++)
583  {
584  fJSON["object_uuids"].push_back(obj_uuids[i].as_string());
585  }
586 
587  mho_json jtags;
588  FillJSONFromTaggable(obj, jtags);
589  fJSON["tags"] = jtags;
590 
591  //for raw data extraction (not possible)
592  fRank = 0;
593  fRawByteSize = 0;
594  fRawData = nullptr;
595  fRawDataDescriptor = "tags";
596  };
597 
598  MHO_ObjectTags* fContainer;
599 };
600 
601 } // namespace hops
602 
603 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
int axis(char *y_axis, char *x_axis)
Definition: axis.c:22
Class AxisDumper - helper class needed to extract MHO_Axis objects from MHO_AxisPack objects inside M...
Definition: MHO_ContainerJSONConverter.hh:461
~AxisDumper()
Definition: MHO_ContainerJSONConverter.hh:464
void SetIndex(std::size_t idx)
Setter for index.
Definition: MHO_ContainerJSONConverter.hh:471
void operator()(const XAxisType &axis)
Definition: MHO_ContainerJSONConverter.hh:473
AxisDumper(mho_json *json_ptr, int level)
Definition: MHO_ContainerJSONConverter.hh:463
MHO_ContainerJSONConverter()
Definition: MHO_ContainerJSONConverter.hh:538
virtual ~MHO_ContainerJSONConverter()
Definition: MHO_ContainerJSONConverter.hh:545
virtual void ConstructJSONRepresentation()
Constructs a JSON representation if fContainer is not nullptr.
Definition: MHO_ContainerJSONConverter.hh:559
virtual void SetObjectToConvert(MHO_Serializable *obj)
Setter for object to convert.
Definition: MHO_ContainerJSONConverter.hh:553
MHO_ContainerJSONConverter(MHO_ExtensibleElement *element)
Definition: MHO_ContainerJSONConverter.hh:540
Class MHO_ContainerJSONConverter.
Definition: MHO_ContainerJSONConverter.hh:195
std::enable_if<(std::is_base_of< MHO_VectorContainerBase, XCheckType >::value &&!std::is_base_of< MHO_AxisBase, XCheckType >::value), void >::type ConstructJSON(const XContainerType *obj)
Constructs a JSON representation for an XContainerType object (vector specialization (but not an axis...
Definition: MHO_ContainerJSONConverter.hh:304
MHO_ContainerJSONConverter(MHO_ExtensibleElement *element)
Definition: MHO_ContainerJSONConverter.hh:199
std::enable_if< std::is_base_of< MHO_AxisBase, XCheckType >::value, void >::type ConstructJSON(const XContainerType *obj)
Constructs a JSON representation for an XContainerType object (axis specialization)
Definition: MHO_ContainerJSONConverter.hh:354
virtual void SetObjectToConvert(MHO_Serializable *obj)
Setter for object to convert.
Definition: MHO_ContainerJSONConverter.hh:212
std::enable_if< std::is_base_of< MHO_TableContainerBase, XCheckType >::value, void >::type ConstructJSON(const XContainerType *obj)
Constructs a JSON representation for the given XContainerType object (table specialization).
Definition: MHO_ContainerJSONConverter.hh:404
MHO_ContainerJSONConverter()
Definition: MHO_ContainerJSONConverter.hh:197
std::enable_if< std::is_base_of< MHO_ScalarContainerBase, XCheckType >::value, void >::type ConstructJSON(const XContainerType *obj)
Constructs a JSON representation for an XContainerType object (scalar specialization)
Definition: MHO_ContainerJSONConverter.hh:258
virtual void ConstructJSONRepresentation()
Constructs a JSON representation if fContainer is not nullptr.
Definition: MHO_ContainerJSONConverter.hh:218
virtual ~MHO_ContainerJSONConverter()
Definition: MHO_ContainerJSONConverter.hh:204
void ConstructJSON(const XCheckType *obj)
Constructs a JSON representation for an object of type XCheckType (unspecialized template doesn't do ...
Definition: MHO_ContainerJSONConverter.hh:236
Class MHO_ExtensibleElement.
Definition: MHO_ExtensibleElement.hh:60
Class MHO_JSONConverter.
Definition: MHO_ContainerJSONConverter.hh:76
void InsertElement(const std::complex< long double > &value, mho_json &data)
Inserts a complex value into a JSON data array, this is a specialization for complex<> element data i...
Definition: MHO_ContainerJSONConverter.hh:156
std::size_t GetRank() const
Getter for rank, needed for access to raw data in table containers this is a bit of a hack for 'hops2...
Definition: MHO_ContainerJSONConverter.hh:113
mho_json fJSON
Definition: MHO_ContainerJSONConverter.hh:182
std::size_t fRawByteSize
Definition: MHO_ContainerJSONConverter.hh:186
const char * fRawData
Definition: MHO_ContainerJSONConverter.hh:187
virtual void SetObjectToConvert(MHO_Serializable *)=0
Setter for object to convert.
void InsertElement(const XValueType &value, mho_json &data)
Inserts an element into a JSON data list (helper functions for generic data insertion for elements of...
Definition: MHO_ContainerJSONConverter.hh:144
std::string GetRawDataDescriptor() const
Getter for raw data descriptor.
Definition: MHO_ContainerJSONConverter.hh:134
virtual void ConstructJSONRepresentation()=0
Constructs a JSON representation if fContainer is not nullptr.
MHO_JSONConverter()
Definition: MHO_ContainerJSONConverter.hh:78
std::size_t fRank
Definition: MHO_ContainerJSONConverter.hh:185
std::size_t GetRawByteSize() const
Getter for raw byte size.
Definition: MHO_ContainerJSONConverter.hh:120
std::string fRawDataDescriptor
Definition: MHO_ContainerJSONConverter.hh:188
mho_json * GetJSON()
Getter for json.
Definition: MHO_ContainerJSONConverter.hh:93
const char * GetRawData() const
Getter for raw data.
Definition: MHO_ContainerJSONConverter.hh:127
void SetLevelOfDetail(int level)
Setter for level of detail.
Definition: MHO_ContainerJSONConverter.hh:86
void InsertElement(const std::complex< float > &value, mho_json &data)
Inserts a complex float value into the given mho_json data structure.
Definition: MHO_ContainerJSONConverter.hh:178
virtual ~MHO_JSONConverter()
Definition: MHO_ContainerJSONConverter.hh:79
int fLOD
Definition: MHO_ContainerJSONConverter.hh:181
void InsertElement(const std::complex< double > &value, mho_json &data)
Inserts a complex double value into an mho_json data structure.
Definition: MHO_ContainerJSONConverter.hh:167
Class MHO_ObjectTags.
Definition: MHO_ObjectTags.hh:28
std::vector< MHO_UUID > GetAllObjectUUIDs() const
Getter for all object uuids at once.
Definition: MHO_ObjectTags.hh:83
Class MHO_Serializable.
Definition: MHO_Serializable.hh:26
Class MHO_Taggable.
Definition: MHO_Taggable.hh:26
mho_json GetMetaDataAsJSON() const
Getter for all meta data stored in this object as a json object.
Definition: MHO_Taggable.hh:95
Definition: fit_gsl.h:54
Definition: MHO_AdhocFlagging.hh:18
void FillJSONFromTaggable(const MHO_Taggable *map, mho_json &obj_tags)
Fills a JSON object with metadata from a Taggable map.
Definition: MHO_ContainerJSONConverter.hh:66
MHO_JSONVerbosityLevel
Definition: MHO_ContainerJSONConverter.hh:39
@ eJSONAxesWithLabelsLevel
Definition: MHO_ContainerJSONConverter.hh:43
@ eJSONAxesLevel
Definition: MHO_ContainerJSONConverter.hh:42
@ eJSONAllLevel
Definition: MHO_ContainerJSONConverter.hh:44
@ eJSONBasicLevel
Definition: MHO_ContainerJSONConverter.hh:40
@ eJSONTagsLevel
Definition: MHO_ContainerJSONConverter.hh:41
Definition: vex.h:175