JSONFormatter.h
Go to the documentation of this file.
1 #ifndef WEBEVD_JSONFORMATTER_H
2 #define WEBEVD_JSONFORMATTER_H
3 
4 #include <cmath>
5 #include <ostream>
6 #include <map>
7 #include <string>
8 
9 #include "TVector3.h"
10 
11 namespace evd
12 {
13 
14 // ----------------------------------------------------------------------------
16 {
17 public:
18  JSONFormatter(std::ostream& os) : fStream(os) {}
19 
20  template<class T> JSONFormatter& operator<<(const T& x)
21  {
22  static_assert(std::is_arithmetic_v<T> ||
23  std::is_enum_v<T> ||
24  std::is_same_v<T, std::string>);
25  fStream << x;
26  return *this;
27  }
28 
30  {
31  // "NaN" and "Infinity" are the javascript syntax, but JSON doesn't include
32  // these concepts at all. Our options are to send a string, a null, or a
33  // number that is unrepresentable and will become Infinity again. I don't
34  // know any way to play the same trick with a NaN.
35  if(isnan(x)) fStream << "1e999";
36  else if(isinf(x)) fStream << "1e999";
37  else fStream << x;
38  return *this;
39  }
40 
41  JSONFormatter& operator<<(float x){return *this << double(x);}
42 
44  {
45  fStream << x;
46  return *this;
47  }
48 
49  JSONFormatter& operator<<(const char* x)
50  {
51  fStream << x;
52  return *this;
53  }
54 
55  template<class T> JSONFormatter& operator<<(const std::vector<T>& v)
56  {
57  fStream << "[";
58  for(const T& x: v){
59  (*this) << x;
60  if(&x != &v.back()) (*this) << ", ";
61  }
62  fStream << "]";
63  return *this;
64  }
65 
66  template<class T, class U>
67  JSONFormatter& operator<<(const std::map<T, U>& m)
68  {
69  fStream << "{\n";
70  unsigned int n = 0;
71  for(auto& it: m){
72  (*this) << " " << it.first << ": " << it.second;
73  ++n;
74  if(n != m.size()) (*this) << ",\n";
75  }
76  fStream << "\n}";
77  return *this;
78  }
79 
80  template<class T>
81  JSONFormatter& operator<<(const std::map<std::string, T>& m)
82  {
83  fStream << "{\n";
84  unsigned int n = 0;
85  for(auto& it: m){
86  (*this) << " \"" << it.first << "\": " << it.second;
87  ++n;
88  if(n != m.size()) (*this) << ",\n";
89  }
90  fStream << "\n}";
91  return *this;
92  }
93 
94  template<class T>
95  JSONFormatter& operator<<(const std::map<int, T>& m)
96  {
97  fStream << "{\n";
98  unsigned int n = 0;
99  for(auto& it: m){
100  (*this) << " \"" << it.first << "\": " << it.second;
101  ++n;
102  if(n != m.size()) (*this) << ",\n";
103  }
104  fStream << "\n}";
105  return *this;
106  }
107 
108  JSONFormatter& operator<<(const TVector3& v)
109  {
110  *this << "["
111  << v.X() << ", "
112  << v.Y() << ", "
113  << v.Z() << "]";
114  return *this;
115  }
116 
117 protected:
118  std::ostream& fStream;
119 };
120 
121 } // namespace
122 
123 #endif
std::ostream & fStream
JSONFormatter(std::ostream &os)
Definition: JSONFormatter.h:18
JSONFormatter & operator<<(const char *x)
Definition: JSONFormatter.h:49
LArSoft includes.
Definition: InfoTransfer.h:33
std::void_t< T > n
JSONFormatter & operator<<(int x)
Definition: JSONFormatter.h:43
list x
Definition: train.py:276
JSONFormatter & operator<<(const TVector3 &v)
JSONFormatter & operator<<(const T &x)
Definition: JSONFormatter.h:20
JSONFormatter & operator<<(float x)
Definition: JSONFormatter.h:41
JSONFormatter & operator<<(double x)
Definition: JSONFormatter.h:29