OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
omnisci::JSON Class Referencefinal

#include <json.h>

Public Member Functions

 JSON ()
 
 JSON (const JSON &peer)
 
 JSON (JSON &&)=default
 
 JSON (const std::string &json)
 
 JSON (const char *json)
 
 JSON (const char *json, size_t len)
 
void parse (const std::string &json)
 
void parse (const char *json)
 
void parse (const char *json, size_t len)
 
std::string stringify () const
 
std::string getType () const
 
bool isString () const
 
bool isNumber () const
 
bool isBoolean () const
 
bool isObject () const
 
bool isArray () const
 
bool isNull () const
 
bool hasMember (const std::string &name) const
 
 operator std::string () const
 
 operator bool () const
 
template<typename T >
 operator T () const
 
JSONoperator= (const JSON &peer)
 
JSONoperator= (const std::string &item)
 
JSONoperator= (const char *item)
 
JSONoperator= (bool item)
 
JSONoperator= (int32_t item)
 
JSONoperator= (int64_t item)
 
JSONoperator= (uint32_t item)
 
JSONoperator= (uint64_t item)
 
JSON operator[] (const std::string &name)
 
JSON operator[] (const char *name)
 
JSON operator[] (const std::string &name) const
 
JSON operator[] (const char *name) const
 
template<typename T >
JSON operator[] (T index)
 
JSON operator[] (size_t index)
 
template<typename T >
JSON operator[] (T index) const
 
JSON operator[] (size_t index) const
 

Private Member Functions

 JSON (std::shared_ptr< rapidjson::Document > doc, rapidjson::Value *vptr, rapidjson::Document::AllocatorType &allo, const std::string &name)
 

Private Attributes

std::shared_ptr
< rapidjson::Document > 
doc_
 
rapidjson::Value * vptr_
 
rapidjson::Document::AllocatorType & allo_
 
const std::string name_
 

Static Private Attributes

static std::string kTypeNames []
 

Friends

bool operator== (const JSON &json1, const JSON &json2)
 
bool operator!= (const JSON &json1, const JSON &json2)
 
template<typename T >
bool operator== (const JSON &json, const T &value)
 
template<typename T >
bool operator== (const T &value, const JSON &json)
 
template<typename T >
bool operator!= (const JSON &json, const T &value)
 
template<typename T >
bool operator!= (const T &value, const JSON &json)
 

Detailed Description

Definition at line 65 of file json.h.

Constructor & Destructor Documentation

omnisci::JSON::JSON ( )
inline

Definition at line 73 of file json.h.

Referenced by operator[]().

74  : doc_(std::make_shared<rapidjson::Document>())
75  , vptr_(&*doc_)
76  , allo_(doc_->GetAllocator())
77  , name_("JSON") {}
rapidjson::Document::AllocatorType & allo_
Definition: json.h:68
std::shared_ptr< rapidjson::Document > doc_
Definition: json.h:66
const std::string name_
Definition: json.h:69
rapidjson::Value * vptr_
Definition: json.h:67

+ Here is the caller graph for this function:

omnisci::JSON::JSON ( const JSON peer)
inline

Definition at line 80 of file json.h.

References allo_, and vptr_.

80 : JSON() { vptr_->CopyFrom(*peer.vptr_, allo_); }
rapidjson::Document::AllocatorType & allo_
Definition: json.h:68
rapidjson::Value * vptr_
Definition: json.h:67
omnisci::JSON::JSON ( JSON &&  )
default
omnisci::JSON::JSON ( const std::string &  json)
inline

Definition at line 86 of file json.h.

References parse().

86 : JSON() { parse(json); }
void parse(const std::string &json)
Definition: json.h:95

+ Here is the call graph for this function:

omnisci::JSON::JSON ( const char *  json)
inline

Definition at line 89 of file json.h.

References parse().

89 : JSON() { parse(json); }
void parse(const std::string &json)
Definition: json.h:95

+ Here is the call graph for this function:

omnisci::JSON::JSON ( const char *  json,
size_t  len 
)
inline

Definition at line 93 of file json.h.

References parse().

93 : JSON() { parse(json, len); }
void parse(const std::string &json)
Definition: json.h:95

+ Here is the call graph for this function:

omnisci::JSON::JSON ( std::shared_ptr< rapidjson::Document >  doc,
rapidjson::Value *  vptr,
rapidjson::Document::AllocatorType &  allo,
const std::string &  name 
)
inlineprivate

Definition at line 301 of file json.h.

305  : doc_(doc), vptr_(vptr), allo_(allo), name_(name) {}
rapidjson::Document::AllocatorType & allo_
Definition: json.h:68
string name
Definition: setup.in.py:72
std::shared_ptr< rapidjson::Document > doc_
Definition: json.h:66
const std::string name_
Definition: json.h:69
rapidjson::Value * vptr_
Definition: json.h:67

Member Function Documentation

std::string omnisci::JSON::getType ( ) const
inline

Definition at line 120 of file json.h.

References kTypeNames, and vptr_.

120 { return kTypeNames[vptr_->GetType()]; }
static std::string kTypeNames[]
Definition: json.h:297
rapidjson::Value * vptr_
Definition: json.h:67
bool omnisci::JSON::hasMember ( const std::string &  name) const
inline

Definition at line 129 of file json.h.

References vptr_.

129 { return vptr_->HasMember(name); }
string name
Definition: setup.in.py:72
rapidjson::Value * vptr_
Definition: json.h:67
bool omnisci::JSON::isArray ( ) const
inline

Definition at line 126 of file json.h.

References vptr_.

126 { return vptr_->IsArray(); }
rapidjson::Value * vptr_
Definition: json.h:67
bool omnisci::JSON::isBoolean ( ) const
inline

Definition at line 124 of file json.h.

References vptr_.

124 { return vptr_->IsBool(); }
rapidjson::Value * vptr_
Definition: json.h:67
bool omnisci::JSON::isNull ( ) const
inline

Definition at line 127 of file json.h.

References vptr_.

127 { return vptr_->IsNull(); }
rapidjson::Value * vptr_
Definition: json.h:67
bool omnisci::JSON::isNumber ( ) const
inline

Definition at line 123 of file json.h.

References vptr_.

123 { return vptr_->IsNumber(); }
rapidjson::Value * vptr_
Definition: json.h:67
bool omnisci::JSON::isObject ( ) const
inline

Definition at line 125 of file json.h.

References vptr_.

125 { return vptr_->IsObject(); }
rapidjson::Value * vptr_
Definition: json.h:67
bool omnisci::JSON::isString ( ) const
inline

Definition at line 122 of file json.h.

References vptr_.

122 { return vptr_->IsString(); }
rapidjson::Value * vptr_
Definition: json.h:67
omnisci::JSON::operator bool ( ) const
inline

Definition at line 140 of file json.h.

References kTypeNames, name_, and vptr_.

140  {
141  if (!vptr_->IsBool()) {
142  throw std::runtime_error("expected JSON field '" + name_ +
143  "' to be Boolean but got [" +
144  kTypeNames[vptr_->GetType()] + "]");
145  }
146  return vptr_->GetBool();
147  }
static std::string kTypeNames[]
Definition: json.h:297
const std::string name_
Definition: json.h:69
rapidjson::Value * vptr_
Definition: json.h:67
omnisci::JSON::operator std::string ( ) const
inline

Definition at line 131 of file json.h.

References kTypeNames, name_, and vptr_.

131  {
132  if (!vptr_->IsString()) {
133  throw std::runtime_error("expected JSON field '" + name_ +
134  "' to be String but got [" + kTypeNames[vptr_->GetType()] +
135  "]");
136  }
137  return std::string{vptr_->GetString(), vptr_->GetStringLength()};
138  }
static std::string kTypeNames[]
Definition: json.h:297
const std::string name_
Definition: json.h:69
rapidjson::Value * vptr_
Definition: json.h:67
template<typename T >
omnisci::JSON::operator T ( ) const
inline

Definition at line 150 of file json.h.

References kTypeNames, name_, omnisci.dtypes::T, and vptr_.

150  {
151  static_assert((std::is_integral_v<T> && !std::is_same_v<bool, std::remove_cv_t<T>>) ||
152  (std::is_floating_point_v<T>));
153  if constexpr (std::is_integral_v<T>) { // NOLINT
154  if constexpr (std::numeric_limits<T>::is_signed) { // NOLINT
155  if constexpr (sizeof(T) < 8) { // NOLINT
156  if (!vptr_->IsInt()) {
157  throw std::runtime_error("can't convert JSON field '" + name_ +
158  "' to be signed integer from [" +
159  kTypeNames[vptr_->GetType()] + "]");
160  }
161  return vptr_->GetInt();
162  } else {
163  if (!vptr_->IsInt64()) {
164  throw std::runtime_error("can't convert JSON field '" + name_ +
165  "' to be signed 64-bit integer from [" +
166  kTypeNames[vptr_->GetType()] + "]");
167  }
168  return vptr_->GetInt64();
169  }
170  } else {
171  if constexpr (sizeof(T) < 8) { // NOLINT
172  if (!vptr_->IsUint()) {
173  throw std::runtime_error("can't convert JSON field '" + name_ +
174  "' to be unsigned integer from [" +
175  kTypeNames[vptr_->GetType()] + "]");
176  }
177  return vptr_->GetUint();
178  } else {
179  if (!vptr_->IsUint64()) {
180  throw std::runtime_error("can't convert JSON field '" + name_ +
181  "' to be unsigned 64-bit integer from [" +
182  kTypeNames[vptr_->GetType()] + "]");
183  }
184  return vptr_->GetUint64();
185  }
186  }
187  } else if constexpr (std::is_floating_point_v<T>) { // NOLINT
188  if (!vptr_->IsDouble()) {
189  throw std::runtime_error("can't convert JSON field '" + name_ +
190  "' to be floating point number from [" +
191  kTypeNames[vptr_->GetType()] + "]");
192  }
193  return vptr_->GetDouble();
194  }
195  }
static std::string kTypeNames[]
Definition: json.h:297
const std::string name_
Definition: json.h:69
rapidjson::Value * vptr_
Definition: json.h:67
JSON& omnisci::JSON::operator= ( const JSON peer)
inline

Definition at line 197 of file json.h.

References allo_, and vptr_.

197  {
198  vptr_->CopyFrom(*peer.vptr_, allo_);
199  return *this;
200  }
rapidjson::Document::AllocatorType & allo_
Definition: json.h:68
rapidjson::Value * vptr_
Definition: json.h:67
JSON& omnisci::JSON::operator= ( const std::string &  item)
inline

Definition at line 202 of file json.h.

References allo_, and vptr_.

202  {
203  *vptr_ = rapidjson::Value().SetString(item, allo_);
204  return *this;
205  }
rapidjson::Document::AllocatorType & allo_
Definition: json.h:68
rapidjson::Value * vptr_
Definition: json.h:67
JSON& omnisci::JSON::operator= ( const char *  item)
inline

Definition at line 207 of file json.h.

References allo_, and vptr_.

207  {
208  *vptr_ = rapidjson::Value().SetString(item, allo_);
209  return *this;
210  }
rapidjson::Document::AllocatorType & allo_
Definition: json.h:68
rapidjson::Value * vptr_
Definition: json.h:67
JSON& omnisci::JSON::operator= ( bool  item)
inline

Definition at line 212 of file json.h.

References vptr_.

212  {
213  vptr_->SetBool(item);
214  return *this;
215  }
rapidjson::Value * vptr_
Definition: json.h:67
JSON& omnisci::JSON::operator= ( int32_t  item)
inline

Definition at line 217 of file json.h.

References vptr_.

217  {
218  vptr_->SetInt(item);
219  return *this;
220  }
rapidjson::Value * vptr_
Definition: json.h:67
JSON& omnisci::JSON::operator= ( int64_t  item)
inline

Definition at line 222 of file json.h.

References vptr_.

222  {
223  vptr_->SetInt64(item);
224  return *this;
225  }
rapidjson::Value * vptr_
Definition: json.h:67
JSON& omnisci::JSON::operator= ( uint32_t  item)
inline

Definition at line 227 of file json.h.

References vptr_.

227  {
228  vptr_->SetUint(item);
229  return *this;
230  }
rapidjson::Value * vptr_
Definition: json.h:67
JSON& omnisci::JSON::operator= ( uint64_t  item)
inline

Definition at line 232 of file json.h.

References vptr_.

232  {
233  vptr_->SetUint64(item);
234  return *this;
235  }
rapidjson::Value * vptr_
Definition: json.h:67
JSON omnisci::JSON::operator[] ( const std::string &  name)
inline

Definition at line 237 of file json.h.

Referenced by operator[]().

237 { return (*this)[name.c_str()]; }
string name
Definition: setup.in.py:72

+ Here is the caller graph for this function:

JSON omnisci::JSON::operator[] ( const char *  name)
inline

Definition at line 238 of file json.h.

References allo_, doc_, f, JSON(), and vptr_.

238  {
239  if (!vptr_->IsObject()) {
240  vptr_->SetObject();
241  }
242  if (!vptr_->HasMember(name)) {
243  vptr_->AddMember(
244  rapidjson::Value(name, allo_).Move(), rapidjson::Value().Move(), allo_);
245  auto f = vptr_->FindMember(name);
246  // f necessary because AddMember inexplicably doesn't return the new member
247  // https://stackoverflow.com/questions/52113291/which-object-reference-does-genericvalueaddmember-return
248  return JSON(doc_, &f->value, allo_, name);
249  }
250  return JSON(doc_, &(*vptr_)[name], allo_, name);
251  }
rapidjson::Document::AllocatorType & allo_
Definition: json.h:68
string name
Definition: setup.in.py:72
std::shared_ptr< rapidjson::Document > doc_
Definition: json.h:66
rapidjson::Value * vptr_
Definition: json.h:67
char * f

+ Here is the call graph for this function:

JSON omnisci::JSON::operator[] ( const std::string &  name) const
inline

Definition at line 253 of file json.h.

253 { return (*this)[name.c_str()]; }
string name
Definition: setup.in.py:72
JSON omnisci::JSON::operator[] ( const char *  name) const
inline

Definition at line 254 of file json.h.

References allo_, doc_, JSON(), kTypeNames, name_, and vptr_.

254  {
255  if (!vptr_->IsObject()) {
256  throw std::runtime_error("JSON " + kTypeNames[vptr_->GetType()] + " field '" +
257  name_ + "' can't use operator []");
258  }
259  if (!vptr_->HasMember(name)) {
260  throw std::runtime_error("JSON field '" + std::string(name) + "' not found");
261  }
262  return JSON(doc_, &(*vptr_)[name], allo_, name);
263  }
rapidjson::Document::AllocatorType & allo_
Definition: json.h:68
static std::string kTypeNames[]
Definition: json.h:297
string name
Definition: setup.in.py:72
std::shared_ptr< rapidjson::Document > doc_
Definition: json.h:66
const std::string name_
Definition: json.h:69
rapidjson::Value * vptr_
Definition: json.h:67

+ Here is the call graph for this function:

template<typename T >
JSON omnisci::JSON::operator[] ( index)
inline

Definition at line 266 of file json.h.

References operator[]().

266  {
267  return operator[](static_cast<size_t>(index));
268  }
JSON operator[](const std::string &name)
Definition: json.h:237

+ Here is the call graph for this function:

JSON omnisci::JSON::operator[] ( size_t  index)
inline

Definition at line 269 of file json.h.

References allo_, doc_, JSON(), to_string(), and vptr_.

269  {
270  if (!vptr_->IsArray()) {
271  vptr_->SetArray();
272  }
273  if (index >= vptr_->Size()) {
274  throw std::runtime_error("JSON array index " + std::to_string(index) +
275  " out of range " + std::to_string(vptr_->Size()));
276  }
277  return JSON(doc_, &(*vptr_)[index], allo_, std::to_string(index));
278  }
rapidjson::Document::AllocatorType & allo_
Definition: json.h:68
std::shared_ptr< rapidjson::Document > doc_
Definition: json.h:66
std::string to_string(char const *&&v)
rapidjson::Value * vptr_
Definition: json.h:67

+ Here is the call graph for this function:

template<typename T >
JSON omnisci::JSON::operator[] ( index) const
inline

Definition at line 281 of file json.h.

References operator[]().

281  {
282  return operator[](static_cast<size_t>(index));
283  }
JSON operator[](const std::string &name)
Definition: json.h:237

+ Here is the call graph for this function:

JSON omnisci::JSON::operator[] ( size_t  index) const
inline

Definition at line 284 of file json.h.

References allo_, doc_, JSON(), kTypeNames, name_, to_string(), and vptr_.

284  {
285  if (!vptr_->IsArray()) {
286  throw std::runtime_error("JSON " + kTypeNames[vptr_->GetType()] + " field '" +
287  name_ + "' can't use operator []");
288  }
289  if (index >= vptr_->Size()) {
290  throw std::runtime_error("JSON array index " + std::to_string(index) +
291  " out of range " + std::to_string(vptr_->Size()));
292  }
293  return JSON(doc_, &(*vptr_)[index], allo_, std::to_string(index));
294  }
rapidjson::Document::AllocatorType & allo_
Definition: json.h:68
static std::string kTypeNames[]
Definition: json.h:297
std::shared_ptr< rapidjson::Document > doc_
Definition: json.h:66
std::string to_string(char const *&&v)
const std::string name_
Definition: json.h:69
rapidjson::Value * vptr_
Definition: json.h:67

+ Here is the call graph for this function:

void omnisci::JSON::parse ( const std::string &  json)
inline

Definition at line 95 of file json.h.

References doc_.

Referenced by JSON().

95  {
96  if (doc_->Parse(json).HasParseError()) {
97  throw std::runtime_error("failed to parse json");
98  }
99  }
std::shared_ptr< rapidjson::Document > doc_
Definition: json.h:66

+ Here is the caller graph for this function:

void omnisci::JSON::parse ( const char *  json)
inline

Definition at line 101 of file json.h.

References doc_.

101  {
102  if (doc_->Parse(json).HasParseError()) {
103  throw std::runtime_error("failed to parse json");
104  }
105  }
std::shared_ptr< rapidjson::Document > doc_
Definition: json.h:66
void omnisci::JSON::parse ( const char *  json,
size_t  len 
)
inline

Definition at line 107 of file json.h.

References doc_.

107  {
108  if (doc_->Parse(json, len).HasParseError()) {
109  throw std::runtime_error("failed to parse json");
110  }
111  }
std::shared_ptr< rapidjson::Document > doc_
Definition: json.h:66
std::string omnisci::JSON::stringify ( ) const
inline

Definition at line 113 of file json.h.

References vptr_.

113  {
114  rapidjson::StringBuffer buf;
115  rapidjson::Writer<rapidjson::StringBuffer> wr(buf);
116  vptr_->Accept(wr);
117  return buf.GetString();
118  }
rapidjson::Value * vptr_
Definition: json.h:67

Friends And Related Function Documentation

bool operator!= ( const JSON json1,
const JSON json2 
)
friend

Definition at line 327 of file json.h.

327  {
328  return (*json1.vptr_ != *json2.vptr_);
329 }
template<typename T >
bool operator!= ( const JSON json,
const T &  value 
)
friend

Definition at line 343 of file json.h.

343  {
344  return (*json.vptr_ != value);
345 }
template<typename T >
bool operator!= ( const T &  value,
const JSON json 
)
friend

Definition at line 347 of file json.h.

347  {
348  return (json != value);
349 }
bool operator== ( const JSON json1,
const JSON json2 
)
friend

Definition at line 323 of file json.h.

323  {
324  return (*json1.vptr_ == *json2.vptr_);
325 }
template<typename T >
bool operator== ( const JSON json,
const T &  value 
)
friend

Definition at line 334 of file json.h.

334  {
335  return (*json.vptr_ == value);
336 }
template<typename T >
bool operator== ( const T &  value,
const JSON json 
)
friend

Definition at line 338 of file json.h.

338  {
339  return (json == value);
340 }

Member Data Documentation

rapidjson::Document::AllocatorType& omnisci::JSON::allo_
private

Definition at line 68 of file json.h.

Referenced by JSON(), operator=(), and operator[]().

std::shared_ptr<rapidjson::Document> omnisci::JSON::doc_
private

Definition at line 66 of file json.h.

Referenced by operator[](), and parse().

std::string omnisci::JSON::kTypeNames[]
inlinestaticprivate
Initial value:
=
{"Null", "False", "True", "Object", "Array", "String", "Number"}

Definition at line 297 of file json.h.

Referenced by getType(), operator bool(), operator std::string(), operator T(), and operator[]().

const std::string omnisci::JSON::name_
private

Definition at line 69 of file json.h.

Referenced by operator bool(), operator std::string(), operator T(), and operator[]().


The documentation for this class was generated from the following file: