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 
108 
114  std::size_t GetRank() const {return fRank;}
120  std::size_t GetRawByteSize() const {return fRawByteSize;};
126  const char* GetRawData() const {return fRawData;};
132  std::string GetRawDataDescriptor() const {return fRawDataDescriptor;}
133 
134  protected:
135 
143  template< typename XValueType > void InsertElement(const XValueType& value, mho_json& data) { data.push_back(value); }
144 
155  void InsertElement(const std::complex< long double >& value, mho_json& data)
156  {
157  data.push_back({value.real(), value.imag()});
158  }
159 
166  void InsertElement(const std::complex< double >& value, mho_json& data)
167  {
168  data.push_back({value.real(), value.imag()});
169  }
170 
177  void InsertElement(const std::complex< float >& value, mho_json& data) { data.push_back({value.real(), value.imag()}); }
178 
179  //data
180  int fLOD;
182 
183  //for extracting container raw data (with no meta data structures)
184  std::size_t fRank;
185  std::size_t fRawByteSize;
186  const char* fRawData;
187  std::string fRawDataDescriptor;
188 };
189 
193 template< typename XContainerType > class MHO_ContainerJSONConverter: public MHO_JSONConverter
194 {
195  public:
196  MHO_ContainerJSONConverter(): MHO_JSONConverter() { fContainer = nullptr; }
197 
199  {
200  fContainer = dynamic_cast< XContainerType* >(element);
201  }
202 
204 
211  virtual void SetObjectToConvert(MHO_Serializable* obj) { fContainer = dynamic_cast< XContainerType* >(obj); };
212 
218  {
219  if(fContainer != nullptr)
220  {
221  ConstructJSON(fContainer);
222  }
223  }
224 
225  private:
226  XContainerType* fContainer;
227 
228  protected:
235  template< typename XCheckType > void ConstructJSON(const XCheckType* obj)
236  {
237  fJSON.clear();
238  std::string class_name = MHO_ClassIdentity::ClassName< XCheckType >();
239  std::string class_uuid = MHO_ClassIdentity::GetUUIDFromClass< XCheckType >().as_string();
240  fJSON["class_name"] = class_name;
241  fJSON["class_uuid"] = class_uuid;
242 
243  //for raw data extraction (not possible)
244  fRank = 0;
245  fRawByteSize = 0;
246  fRawData = nullptr;
247  fRawDataDescriptor = "";
248  };
249 
255  template< typename XCheckType = XContainerType >
256  typename std::enable_if< std::is_base_of< MHO_ScalarContainerBase, XCheckType >::value, void >::type
257  ConstructJSON(const XContainerType* obj)
258  {
259  fJSON.clear();
260  std::string class_name = MHO_ClassIdentity::ClassName< XContainerType >();
261  std::string class_uuid = MHO_ClassIdentity::GetUUIDFromClass< XContainerType >().as_string();
262  if(fLOD >= eJSONBasic)
263  {
264  fJSON["class_name"] = class_name;
265  fJSON["class_uuid"] = class_uuid;
266  fJSON["rank"] = fContainer->GetRank();
267  fJSON["total_size"] = fContainer->GetSize();
268  }
269 
270  if(fLOD >= eJSONTags)
271  {
272  mho_json jtags;
273  // FillJSONFromCommonMap(fContainer, jtags);
274  FillJSONFromTaggable(fContainer, jtags);
275  fJSON["tags"] = jtags;
276  }
277 
278  //just one element
279  if(fLOD >= eJSONAll)
280  {
281  mho_json data;
282  InsertElement(fContainer->GetData(), data);
283  fJSON["data"] = data;
284  }
285 
286  //for raw data extraction (not possible)
287  fRank = 0;
288  fRawByteSize = 0;
289  fRawData = nullptr;
290  fRawDataDescriptor = "";
291  };
292 
299  template< typename XCheckType = XContainerType >
300  typename std::enable_if< (std::is_base_of< MHO_VectorContainerBase, XCheckType >::value &&
301  !std::is_base_of< MHO_AxisBase, XCheckType >::value),
302  void >::type
303  ConstructJSON(const XContainerType* obj)
304  {
305  fJSON.clear();
306  std::string class_name = MHO_ClassIdentity::ClassName< XContainerType >();
307  std::string class_uuid = MHO_ClassIdentity::GetUUIDFromClass< XContainerType >().as_string();
308  if(fLOD >= eJSONBasic)
309  {
310  fJSON["class_name"] = class_name;
311  fJSON["class_uuid"] = class_uuid;
312  fJSON["rank"] = fContainer->GetRank();
313  fJSON["total_size"] = fContainer->GetSize();
314  fJSON["dimensions"] = fContainer->GetDimensionArray();
315  }
316 
317  if(fLOD >= eJSONTags)
318  {
319  mho_json jtags;
320  FillJSONFromTaggable(fContainer, jtags);
321  //FillJSONFromCommonMap(fContainer ,jtags);
322  fJSON["tags"] = jtags;
323  }
324 
325  //data goes out flat-packed into 1-d array
326  if(fLOD >= eJSONAll)
327  {
328  mho_json data;
329  for(auto it = fContainer->cbegin(); it != fContainer->cend(); it++)
330  {
331  InsertElement(*it, data);
332  }
333  fJSON["data"] = data;
334  }
335 
336  //for raw data extraction
337  std::size_t elem_size = sizeof( typename XContainerType::value_type);
338  std::size_t n_elem = fContainer->GetSize();
339  fRank = XContainerType::rank::value;
340  fRawByteSize = elem_size*n_elem;
341  fRawData = reinterpret_cast<const char*>( fContainer->GetData() );
342  fRawDataDescriptor = MHO_NumpyTypeCode< typename XContainerType::value_type >();
343  };
344 
351  template< typename XCheckType = XContainerType >
352  typename std::enable_if< std::is_base_of< MHO_AxisBase, XCheckType >::value, void >::type
353  ConstructJSON(const XContainerType* obj)
354  {
355  fJSON.clear();
356  std::string class_name = MHO_ClassIdentity::ClassName< XContainerType >();
357  std::string class_uuid = MHO_ClassIdentity::GetUUIDFromClass< XContainerType >().as_string();
358  if(fLOD >= eJSONBasic)
359  {
360  fJSON["class_name"] = class_name;
361  fJSON["class_uuid"] = class_uuid;
362  fJSON["rank"] = fContainer->GetRank();
363  fJSON["total_size"] = fContainer->GetSize();
364  fJSON["dimensions"] = fContainer->GetDimensionArray();
365  }
366 
367  if(fLOD >= eJSONTags)
368  {
369  mho_json jtags;
370  FillJSONFromTaggable(fContainer, jtags);
371  //FillJSONFromCommonMap(fContainer ,jtags);
372  fJSON["tags"] = jtags;
373  }
374 
375  //data goes out flat-packed into 1-d array
376  if(fLOD >= eJSONAll)
377  {
378  mho_json data;
379  for(auto it = fContainer->cbegin(); it != fContainer->cend(); it++)
380  {
381  InsertElement(*it, data);
382  }
383  fJSON["data"] = data;
384  }
385 
386  //for raw data extraction
387  std::size_t elem_size = sizeof( typename XContainerType::value_type);
388  std::size_t n_elem = fContainer->GetSize();
389  fRank = XContainerType::rank::value;
390  fRawByteSize = elem_size*n_elem;
391  fRawData = reinterpret_cast<const char*>( fContainer->GetData() );
392  fRawDataDescriptor = MHO_NumpyTypeCode< typename XContainerType::value_type >();
393  };
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:194
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:303
MHO_ContainerJSONConverter(MHO_ExtensibleElement *element)
Definition: MHO_ContainerJSONConverter.hh:198
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:353
virtual void SetObjectToConvert(MHO_Serializable *obj)
Setter for object to convert.
Definition: MHO_ContainerJSONConverter.hh:211
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:196
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:257
virtual void ConstructJSONRepresentation()
Constructs a JSON representation if fContainer is not nullptr.
Definition: MHO_ContainerJSONConverter.hh:217
virtual ~MHO_ContainerJSONConverter()
Definition: MHO_ContainerJSONConverter.hh:203
void ConstructJSON(const XCheckType *obj)
Constructs a JSON representation for an object of type XCheckType (unspecialized template doesn't do ...
Definition: MHO_ContainerJSONConverter.hh:235
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:155
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:114
mho_json fJSON
Definition: MHO_ContainerJSONConverter.hh:181
std::size_t fRawByteSize
Definition: MHO_ContainerJSONConverter.hh:185
const char * fRawData
Definition: MHO_ContainerJSONConverter.hh:186
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:143
std::string GetRawDataDescriptor() const
Getter for raw data descriptor.
Definition: MHO_ContainerJSONConverter.hh:132
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:184
std::size_t GetRawByteSize() const
Getter for raw byte size.
Definition: MHO_ContainerJSONConverter.hh:120
std::string fRawDataDescriptor
Definition: MHO_ContainerJSONConverter.hh:187
mho_json * GetJSON()
Getter for json.
Definition: MHO_ContainerJSONConverter.hh:93
const char * GetRawData() const
Getter for raw data.
Definition: MHO_ContainerJSONConverter.hh:126
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:177
virtual ~MHO_JSONConverter()
Definition: MHO_ContainerJSONConverter.hh:79
int fLOD
Definition: MHO_ContainerJSONConverter.hh:180
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:166
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:84
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: MHO_ChannelLabeler.hh:17
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