DLPrimitives
json.hpp
1 #pragma once
2 //
4 // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
5 //
6 // See accompanying file COPYING.TXT file for licensing details.
7 //
9 #include <vector>
10 #include <map>
11 #include <string>
12 #include <iostream>
13 #include <limits>
14 #include <string.h>
15 #include <typeinfo>
16 
17 #ifndef DLPRIM_API
18 #define DLPRIM_API
19 #endif
20 
21 namespace dlprim {
25 namespace json {
34  template<typename T>
35  class copy_ptr {
36  T *ptr_;
37  public:
38  copy_ptr() : ptr_(0) {}
39  explicit copy_ptr(T *v) : ptr_(v) {}
40  copy_ptr(copy_ptr const &other) :
41  ptr_(other.ptr_ ? new T(*other.ptr_) : 0)
42  {
43  }
44  copy_ptr(copy_ptr &&other) : ptr_(other.ptr_)
45  {
46  other.ptr_ = 0;
47  }
48  copy_ptr &operator=(copy_ptr &&other)
49  {
50  if(this!=&other) {
51  this->swap(other);
52  other.reset();
53  }
54  return *this;
55  }
56  copy_ptr const &operator=(copy_ptr const &other)
57  {
58  if(this != &other) {
59  copy_ptr tmp(other);
60  swap(tmp);
61  }
62  return *this;
63  }
64  ~copy_ptr() {
65  if(ptr_) delete ptr_;
66  }
67 
68  T const *get() const { return ptr_; }
69  T *get() { return ptr_; }
70 
71  T const &operator *() const { return *ptr_; }
72  T &operator *() { return *ptr_; }
73  T const *operator->() const { return ptr_; }
74  T *operator->() { return ptr_; }
75  T *release() { T *tmp=ptr_; ptr_=0; return tmp; }
76  void reset(T *p=0)
77  {
78  if(ptr_) delete ptr_;
79  ptr_=p;
80  }
81  void swap(copy_ptr &other)
82  {
83  T *tmp=other.ptr_;
84  other.ptr_=ptr_;
85  ptr_=tmp;
86  }
87  };
99  class string_key {
100  public:
101 
105  typedef char const *const_iterator;
106 
110  static const size_t npos = -1;
111 
116  begin_(0),
117  end_(0)
118  {
119  }
120 
124  string_key(char const *key) :
125  begin_(0),
126  end_(0),
127  key_(key)
128  {
129  }
133  string_key(std::string const &key) :
134  begin_(0),
135  end_(0),
136  key_(key)
137  {
138  }
142  size_t size() const
143  {
144  return end() - begin();
145  }
149  size_t length() const
150  {
151  return size();
152  }
156  void clear()
157  {
158  begin_ = end_ = 0;
159  key_.clear();
160  }
164  bool empty() const
165  {
166  return end() == begin();
167  }
172  size_t find(char c,size_t pos = 0) const
173  {
174  size_t s = size();
175  if(pos >= s)
176  return npos;
177  char const *p=begin() + pos;
178  while(pos <= s && *p!=c) {
179  pos++;
180  p++;
181  }
182  if(pos >= s)
183  return npos;
184  return pos;
185  }
186 
190  string_key substr(size_t pos = 0,size_t n=npos) const
191  {
192  string_key tmp = unowned_substr(pos,n);
193  return string_key(std::string(tmp.begin(),tmp.end()));
194  }
199  string_key unowned_substr(size_t pos = 0,size_t n=npos) const
200  {
201  if(pos >= size()) {
202  return string_key();
203  }
204  char const *p=begin() + pos;
205  char const *e=end();
206  if(n > size_t(e-p)) {
207  return string_key(p,e);
208  }
209  else {
210  return string_key(p,p+n);
211  }
212  }
213 
217  char const &operator[](size_t n) const
218  {
219  return *(begin() + n);
220  }
224  char const &at(size_t n) const
225  {
226  if(n > size())
227  throw std::out_of_range("dlprim::string_key::at() range error");
228  return *(begin() + n);
229  }
230 
235  static string_key unowned(std::string const &v)
236  {
237  return string_key(v.c_str(),v.c_str()+v.size());
238  }
243  static string_key unowned(char const *str)
244  {
245  char const *end = str;
246  while(*end)
247  end++;
248  return string_key(str,end);
249  }
254  static string_key unowned(char const *begin,char const *end)
255  {
256  return string_key(begin,end);
257  }
258 
262  char const *begin() const
263  {
264  if(begin_)
265  return begin_;
266  return key_.c_str();
267  }
271  char const *end() const
272  {
273  if(begin_)
274  return end_;
275  return key_.c_str() + key_.size();
276  }
280  bool operator<(string_key const &other) const
281  {
282  return std::lexicographical_compare( begin(),end(),
283  other.begin(),other.end(),
284  std::char_traits<char>::lt);
285  }
289  bool operator>(string_key const &other) const
290  {
291  return other < *this;
292  }
296  bool operator>=(string_key const &other) const
297  {
298  return !(*this < other);
299  }
303  bool operator<=(string_key const &other) const
304  {
305  return !(*this > other);
306  }
310  bool operator==(string_key const &other) const
311  {
312  return (end() - begin() == other.end() - other.begin())
313  && memcmp(begin(),other.begin(),end()-begin()) == 0;
314  }
318  bool operator!=(string_key const &other) const
319  {
320  return !(*this==other);
321  }
322 
326  char const *data() const
327  {
328  return begin();
329  }
330 
334  std::string str() const
335  {
336  if(begin_)
337  return std::string(begin_,end_-begin_);
338  else
339  return key_;
340  }
344  operator std::string() const
345  {
346  return str();
347  }
348  private:
349  string_key(char const *b,char const *e) :
350  begin_(b),
351  end_(e)
352  {
353  }
354 
355  char const *begin_;
356  char const *end_;
357  std::string key_;
358  };
359 
363  inline std::ostream &operator<<(std::ostream &out,string_key const &s)
364  {
365  out.write(s.data(),s.size());
366  return out;
367  }
368 
372  inline bool operator==(string_key const &l,char const *r)
373  {
374  return l==string_key::unowned(r);
375  }
376 
380  inline bool operator==(char const *l,string_key const &r)
381  {
382  return string_key::unowned(l) == r;
383  }
384 
388  inline bool operator==(string_key const &l,std::string const &r)
389  {
390  return l==string_key::unowned(r);
391  }
392 
393 
397  inline bool operator==(std::string const &l,string_key const &r)
398  {
399  return string_key::unowned(l) == r;
400  }
401 
405  inline bool operator!=(string_key const &l,char const *r)
406  {
407  return l!=string_key::unowned(r);
408  }
409 
413  inline bool operator!=(char const *l,string_key const &r)
414  {
415  return string_key::unowned(l) != r;
416  }
417 
421  inline bool operator!=(string_key const &l,std::string const &r)
422  {
423  return l!=string_key::unowned(r);
424  }
425 
429  inline bool operator!=(std::string const &l,string_key const &r)
430  {
431  return string_key::unowned(l) != r;
432  }
436  inline bool operator<=(string_key const &l,char const *r)
437  {
438  return l<=string_key::unowned(r);
439  }
440 
444  inline bool operator<=(char const *l,string_key const &r)
445  {
446  return string_key::unowned(l) <= r;
447  }
448 
452  inline bool operator<=(string_key const &l,std::string const &r)
453  {
454  return l<=string_key::unowned(r);
455  }
456 
460  inline bool operator<=(std::string const &l,string_key const &r)
461  {
462  return string_key::unowned(l) <= r;
463  }
467  inline bool operator>=(string_key const &l,char const *r)
468  {
469  return l>=string_key::unowned(r);
470  }
471 
475  inline bool operator>=(char const *l,string_key const &r)
476  {
477  return string_key::unowned(l) >= r;
478  }
479 
483  inline bool operator>=(string_key const &l,std::string const &r)
484  {
485  return l>=string_key::unowned(r);
486  }
487 
491  inline bool operator>=(std::string const &l,string_key const &r)
492  {
493  return string_key::unowned(l) >= r;
494  }
495 
496 
500  inline bool operator<(string_key const &l,char const *r)
501  {
502  return l<string_key::unowned(r);
503  }
504 
508  inline bool operator<(char const *l,string_key const &r)
509  {
510  return string_key::unowned(l) < r;
511  }
512 
516  inline bool operator<(string_key const &l,std::string const &r)
517  {
518  return l<string_key::unowned(r);
519  }
520 
524  inline bool operator<(std::string const &l,string_key const &r)
525  {
526  return string_key::unowned(l) < r;
527  }
531  inline bool operator>(string_key const &l,char const *r)
532  {
533  return l>string_key::unowned(r);
534  }
535 
539  inline bool operator>(char const *l,string_key const &r)
540  {
541  return string_key::unowned(l) > r;
542  }
543 
547  inline bool operator>(string_key const &l,std::string const &r)
548  {
549  return l>string_key::unowned(r);
550  }
551 
555  inline bool operator>(std::string const &l,string_key const &r)
556  {
557  return string_key::unowned(l) > r;
558  }
559 
560 
561  class value;
562 
566  struct null {};
570  struct undefined {};
571 
572  inline bool operator==(undefined const &/*l*/,undefined const &/*r*/) {return true;}
573  inline bool operator!=(undefined const &/*l*/,undefined const &/*r*/) {return false;}
574  inline bool operator==(null const &/*l*/,null const &/*r*/) {return true;}
575  inline bool operator!=(null const &/*l*/,null const &/*r*/) {return false;}
576 
580  typedef std::vector<value> array;
584  typedef std::map<string_key,value> object;
585 
586 
587  template<typename T>
588  struct traits;
589 
590 
594  typedef enum {
602  } json_type;
603 
604 
605  enum {
606  compact = 0,
607  readable = 1
608  };
609 
616  class DLPRIM_API bad_value_cast : public std::bad_cast {
617  public:
618  bad_value_cast();
619  bad_value_cast(std::string const &s);
620  bad_value_cast(std::string const &s,json_type actual);
621  bad_value_cast(std::string const &s,json_type expected, json_type actual);
622 
623  virtual ~bad_value_cast() throw();
624  virtual const char* what() const throw();
625  private:
626  std::string msg_;
627  };
628 
629  class value;
630 
634  std::istream DLPRIM_API &operator>>(std::istream &in,value &v);
635 
639  std::ostream DLPRIM_API &operator<<(std::ostream &out,value const &v);
640 
644  std::ostream DLPRIM_API &operator<<(std::ostream &out,json_type);
645 
652  class DLPRIM_API value {
653  public:
654 
658  json_type type() const;
659 
663  bool is_undefined() const;
667  bool is_null() const;
668 
672  bool const &boolean() const;
676  double const &number() const;
680  std::string const &str() const;
684  json::object const &object() const;
688  json::array const &array() const;
689 
693  bool &boolean();
697  double &number();
701  std::string &str();
705  json::object &object();
709  json::array &array();
710 
714  void undefined();
718  void null();
719 
723  void boolean(bool);
727  void number(double );
731  void str(std::string const &);
735  void object(json::object const &);
739  void array(json::array const &);
740 
741 
745  template<typename T>
746  T get_value() const
747  {
748  return traits<T>::get(*this);
749  }
750 
754  template<typename T>
755  void set_value(T const &v)
756  {
757  traits<T>::set(*this,v);
758  }
759 
767  value const &find(std::string const &path) const;
775  value const &find(char const *path) const;
776 
784  value const &at(std::string const &path) const;
792  value const &at(char const *path) const;
800  value &at(std::string const &path);
808  value &at(char const *path);
809 
813  void at(std::string const &path,value const &v);
817  void at(char const *path,value const &v);
818 
819 
823  template<typename T>
824  value(T const &v)
825  {
826  set_value(v);
827  }
828 
834  json_type type(std::string const &path) const
835  {
836  return find(path).type();
837  }
843  json_type type(char const *path) const
844  {
845  return find(path).type();
846  }
847 
851  template<typename T>
852  void set(std::string const &path,T const &v)
853  {
854  at(path,value(v));
855  }
859  template<typename T>
860  void set(char const *path,T const &v)
861  {
862  at(path,value(v));
863  }
864 
869  std::string get(std::string const &path,char const *def) const
870  {
871  value const &v=find(path);
872  if(v.is_undefined())
873  return def;
874  try {
875  return v.get_value<std::string>();
876  }
877  catch(std::bad_cast const &e) {
878  return def;
879  }
880  }
885  std::string get(char const *path,char const *def) const
886  {
887  value const &v=find(path);
888  if(v.is_undefined())
889  return def;
890  try {
891  return v.get_value<std::string>();
892  }
893  catch(std::bad_cast const &e) {
894  return def;
895  }
896  }
897 
902  template<typename T>
903  T get(std::string const &path) const
904  {
905  return at(path).get_value<T>();
906  }
911  template<typename T>
912  T get(char const *path) const
913  {
914  return at(path).get_value<T>();
915  }
916 
921  template<typename T>
922  T get(char const *path,T const &def) const
923  {
924  value const &v=find(path);
925  if(v.is_undefined())
926  return def;
927  try {
928  return v.get_value<T>();
929  }
930  catch(std::bad_cast const &e) {
931  return def;
932  }
933  }
938  template<typename T>
939  T get(std::string const &path,T const &def) const
940  {
941  value const &v=find(path);
942  if(v.is_undefined())
943  return def;
944  try {
945  return v.get_value<T>();
946  }
947  catch(std::bad_cast const &e) {
948  return def;
949  }
950  }
951 
959  value &operator[](std::string const &name);
960 
967  value const &operator[](std::string const &name) const;
968 
973  value &operator[](size_t n);
978  value const &operator[](size_t n) const;
979 
983  std::string save(int how=compact) const;
987  void save(std::ostream &out,int how=compact) const;
998  bool load(std::istream &in,bool full,int *line_number=0);
999 
1011  bool load(char const *&begin,char const *end,bool full,int *line_number=0);
1012 
1016  bool operator==(value const &other) const;
1020  bool operator!=(value const &other) const;
1021 
1022 
1027  {
1028  d=std::move(other.d);
1029  return *this;
1030  }
1034  value(value &&other) : d(std::move(other.d))
1035  {
1036 
1037  }
1041  value(value const &other) :
1042  d(other.d)
1043  {
1044  }
1048  value const &operator=(value const &other)
1049  {
1050  d=other.d;
1051  return *this;
1052  }
1057  {
1058  }
1059 
1063 
1065  {
1066  }
1067 
1071  void swap(value &other)
1072  {
1073  d.swap(other.d);
1074  }
1075 
1076  private:
1077 
1078  void write(std::ostream &out,int tabs) const;
1079  void write_value(std::ostream &out,int tabs) const;
1080 
1081  struct _data;
1082  struct DLPRIM_API copyable {
1083 
1084  _data *operator->() { return &*d; }
1085  _data &operator*() { return *d; }
1086  _data const *operator->() const { return &*d; }
1087  _data const &operator*() const { return *d; }
1088 
1089  copyable();
1090  copyable(copyable const &r);
1091  copyable(copyable &&);
1092  copyable &operator=(copyable &&r);
1093  copyable const &operator=(copyable const &r);
1094  ~copyable();
1095 
1096  void swap(copyable &other)
1097  {
1098  d.swap(other.d);
1099  }
1100  private:
1101  copy_ptr<_data> d;
1102  } d;
1103 
1104  friend struct copyable;
1105  };
1106 
1107 
1112  std::string DLPRIM_API to_json(std::string const &utf);
1117  std::string DLPRIM_API to_json(char const *begin,char const *end);
1122  void DLPRIM_API to_json(char const *begin,char const *end,std::ostream &out);
1123  //
1127  void DLPRIM_API to_json(std::string const &str,std::ostream &out);
1128 
1129 
1131 
1132  template<typename T1,typename T2>
1133  struct traits<std::pair<T1,T2> > {
1134  static std::pair<T1,T2> get(value const &v)
1135  {
1136  if(v.object().size()!=2)
1137  throw bad_value_cast("Object with two members expected");
1138  std::pair<T1,T2> pair(v.get_value<T1>("first"),v.get_value<T2>("second"));
1139  return pair;
1140  }
1141  static void set(value &v,std::pair<T1,T2> const &in)
1142  {
1143  v=json::object();
1144  v.set_value("first",in.first);
1145  v.set_value("second",in.second);
1146  }
1147  };
1148 
1149  template<typename T>
1150  struct traits<std::vector<T> > {
1151  static std::vector<T> get(value const &v)
1152  {
1153  std::vector<T> result;
1154  json::array const &a=v.array();
1155  result.resize(a.size());
1156  for(unsigned i=0;i<a.size();i++)
1157  result[i]=a[i].get_value<T>();
1158  return result;
1159  }
1160  static void set(value &v,std::vector<T> const &in)
1161  {
1162  v=json::array();
1163  json::array &a=v.array();
1164  a.resize(in.size());
1165  for(unsigned i=0;i<in.size();i++)
1166  a[i].set_value(in[i]);
1167  }
1168  };
1169 
1170 
1171  #define DLPRIM_JSON_SPECIALIZE(type,method) \
1172  template<> \
1173  struct traits<type> { \
1174  static type get(value const &v) \
1175  { \
1176  return v.method(); \
1177  } \
1178  static void set(value &v,type const &in)\
1179  { \
1180  v.method(in); \
1181  } \
1182  };
1183 
1184  DLPRIM_JSON_SPECIALIZE(bool,boolean);
1185  DLPRIM_JSON_SPECIALIZE(double,number);
1186  DLPRIM_JSON_SPECIALIZE(std::string,str);
1187  DLPRIM_JSON_SPECIALIZE(json::object,object);
1188  DLPRIM_JSON_SPECIALIZE(json::array,array);
1189 
1190  #undef DLPRIM_JSON_SPECIALIZE
1191 
1192  #define DLPRIM_JSON_SPECIALIZE_INT(type) \
1193  template<> \
1194  struct traits<type> { \
1195  static type get(value const &v) \
1196  { \
1197  type res=static_cast<type>(v.number()); \
1198  if(res!=v.number()) \
1199  throw bad_value_cast(); \
1200  return res; \
1201  } \
1202  static void set(value &v,type const &in) \
1203  { \
1204  if(std::numeric_limits<type>::digits > \
1205  std::numeric_limits<double>::digits \
1206  && static_cast<double>(in)!=in) \
1207  { \
1208  throw bad_value_cast(); \
1209  } \
1210  v.number(static_cast<double>(in)); \
1211  } \
1212  };
1213 
1214  DLPRIM_JSON_SPECIALIZE_INT(char)
1215  DLPRIM_JSON_SPECIALIZE_INT(unsigned char)
1216  DLPRIM_JSON_SPECIALIZE_INT(signed char)
1217  DLPRIM_JSON_SPECIALIZE_INT(wchar_t)
1218  DLPRIM_JSON_SPECIALIZE_INT(short)
1219  DLPRIM_JSON_SPECIALIZE_INT(unsigned short)
1220  DLPRIM_JSON_SPECIALIZE_INT(int)
1221  DLPRIM_JSON_SPECIALIZE_INT(unsigned int)
1222  DLPRIM_JSON_SPECIALIZE_INT(long)
1223  DLPRIM_JSON_SPECIALIZE_INT(unsigned long)
1224  DLPRIM_JSON_SPECIALIZE_INT(long long)
1225  DLPRIM_JSON_SPECIALIZE_INT(unsigned long long)
1226 
1227  #undef DLPRIM_JSON_SPECIALIZE_INT
1228 
1229  template<>
1230  struct traits<float> {
1231  static float get(value const &v)
1232  {
1233  double r=v.number();
1234  if( r < (-std::numeric_limits<float>::max()) // actually should be C++11 lowest, but it should be under IEEE float lowest()=-max()
1235  || std::numeric_limits<float>::max() < r )
1236  {
1237  throw bad_value_cast();
1238  }
1239  return static_cast<float>(r);
1240  }
1241  static void set(value &v,float const &in)
1242  {
1243  v.number(in);
1244  }
1245  };
1246 
1247  template<>
1248  struct traits<long double> {
1249  static long double get(value const &v)
1250  {
1251  return v.number();
1252  }
1253  static void set(value &v,long double const &in)
1254  {
1255  if( in < -std::numeric_limits<double>::max() // should actually be std::numeric_limits<float>::lowest() but it is ==-max()
1256  || std::numeric_limits<double>::max() < in )
1257  {
1258  throw bad_value_cast();
1259  }
1260  v.number(static_cast<double>(in));
1261  }
1262  };
1263 
1264  template<>
1265  struct traits<json::null> {
1266  static void set(value &v,json::null const &/*in*/)
1267  {
1268  v.null();
1269  }
1270  };
1271 
1272  template<int n>
1273  struct traits<char[n]> {
1274  typedef char vtype[n];
1275  static void set(value &v,vtype const &in)
1276  {
1277  v.str(in);
1278  }
1279  };
1280  template<int n>
1281  struct traits<char const [n]> {
1282  typedef char const vtype[n];
1283  static void set(value &v,vtype const &in)
1284  {
1285  v.str(in);
1286  }
1287  };
1288 
1289 
1290  template<>
1291  struct traits<char const *> {
1292  static void set(value &v,char const * const &in)
1293  {
1294  v.str(in);
1295  }
1296  };
1297 
1299 
1300 
1301 } // json
1302 } // dlprim
1303 
value & operator=(value &&other)
Move assignment.
Definition: json.hpp:1026
Special object that is convertible to null json value.
Definition: json.hpp:566
This is a special object that may hold an std::string or alternatively reference to external (unowned...
Definition: json.hpp:99
std::ostream & operator<<(std::ostream &out, string_key const &s)
Write the string to the stream.
Definition: json.hpp:363
bool operator!=(string_key const &l, char const *r)
Compare two strings.
Definition: json.hpp:405
bool operator!=(string_key const &other) const
Compare two strings.
Definition: json.hpp:318
bool empty() const
Check if the string is empty.
Definition: json.hpp:164
bool operator>(string_key const &l, char const *r)
Compare two strings.
Definition: json.hpp:531
json_type
The type of json value.
Definition: json.hpp:594
string_key substr(size_t pos=0, size_t n=npos) const
Create a substring from this string starting from character pos of size at most n.
Definition: json.hpp:190
bool operator>(string_key const &other) const
Compare two strings.
Definition: json.hpp:289
static string_key unowned(std::string const &v)
Create a string from v without copying the memory. v should remain valid as long as this object is us...
Definition: json.hpp:235
T get_value() const
Convert the value to type T, using json::traits, throws bad_value_cast if conversion is not possible...
Definition: json.hpp:746
a smart pointer similar to std::unique_ptr but it copies underlying object on pointer copy instead of...
Definition: json.hpp:35
void swap(value &other)
Swaps two values, does not throw.
Definition: json.hpp:1071
char const * const_iterator
Iterator type.
Definition: json.hpp:105
json_type type() const
Get the type of the value.
string_key(char const *key)
Create a new string copying the key.
Definition: json.hpp:124
void set_value(T const &v)
Convert the object v of type T to the value.
Definition: json.hpp:755
std::vector< value > array
The json::array - std::vector of json::value&#39;s.
Definition: json.hpp:580
bool is_undefined() const
Returns true if type()==json::is_undefined.
Print JSON values in human readable format (with identention)
Definition: json.hpp:607
~value()
Destructor.
Definition: json.hpp:1064
The error that is thrown in case of bad conversion of json::value to ordinary value.
Definition: json.hpp:616
value()
Default value - creates a value of undefined type.
Definition: json.hpp:1056
string_key(std::string const &key)
Create a new string copying the key.
Definition: json.hpp:133
std::map< string_key, value > object
The json::object - std::map of json::value&#39;s.
Definition: json.hpp:584
double const & number() const
Convert value to double, throws bad_value_cast if value&#39;s type is not number.
value const & operator=(value const &other)
Assignment operator.
Definition: json.hpp:1048
char const * begin() const
Get a pointer to the first character in the string.
Definition: json.hpp:262
size_t size() const
String size in bytes.
Definition: json.hpp:142
json_type type(std::string const &path) const
Returns the type of variable in path, if not found returns undefined.
Definition: json.hpp:834
This class is central representation of json objects.
Definition: json.hpp:652
Print JSON values in most compact format.
Definition: json.hpp:606
Special object that is convertible to undefined json value.
Definition: json.hpp:570
json_type type(char const *path) const
Returns the type of variable in path, if not found returns undefined.
Definition: json.hpp:843
Undefined value.
Definition: json.hpp:595
array value
Definition: json.hpp:601
static string_key unowned(char const *str)
Create a string from str without copying the memory. str should remain valid as long as this object i...
Definition: json.hpp:243
size_t length() const
Same as size()
Definition: json.hpp:149
string_key()
Default constructor - empty key.
Definition: json.hpp:115
value(value const &other)
Copy constructor.
Definition: json.hpp:1041
json::object const & object() const
Convert value to json::object, throws bad_value_cast if value&#39;s type is not object.
value(T const &v)
Creates a value from and object v of type T.
Definition: json.hpp:824
char const * data() const
Get the pointer to the first character in the string. Note it should not be NUL terminated.
Definition: json.hpp:326
bool operator>=(string_key const &l, char const *r)
Compare two strings.
Definition: json.hpp:467
string value
Definition: json.hpp:599
bool operator<=(string_key const &other) const
Compare two strings.
Definition: json.hpp:303
json::array const & array() const
Convert value to json::array, throws bad_value_cast if value&#39;s type is not array. ...
char const & at(size_t n) const
Get a character at position n, if n is not valid position, throws std::out_of_range exception...
Definition: json.hpp:224
Mane namespace.
Definition: context.hpp:9
std::string DLPRIM_API to_json(std::string const &utf)
Convert UTF-8 string to JSON string, i.e. a sring foo is converted to "foo", a string bar"baz is conv...
char const & operator[](size_t n) const
Get a character at position n.
Definition: json.hpp:217
bool operator<=(string_key const &l, char const *r)
Compare two strings.
Definition: json.hpp:436
value(value &&other)
Move constructor.
Definition: json.hpp:1034
numeric value
Definition: json.hpp:598
bool operator<(string_key const &other) const
Compare two strings.
Definition: json.hpp:280
std::string str() const
Create std::string from the key.
Definition: json.hpp:334
object value
Definition: json.hpp:600
std::istream DLPRIM_API & operator>>(std::istream &in, value &v)
Read json object from input stream.
bool operator<(string_key const &l, char const *r)
Compare two strings.
Definition: json.hpp:500
size_t find(char c, size_t pos=0) const
Find first occurrence of a character in the string starting from position pos. Returns npos if not ch...
Definition: json.hpp:172
boolean value
Definition: json.hpp:597
bool operator>=(string_key const &other) const
Compare two strings.
Definition: json.hpp:296
Definition: json.hpp:588
bool operator==(string_key const &l, char const *r)
Compare two strings.
Definition: json.hpp:372
null value
Definition: json.hpp:596
void clear()
Clear the string.
Definition: json.hpp:156
string_key unowned_substr(size_t pos=0, size_t n=npos) const
Create a substring from this string starting from character pos of size at most n such that the memor...
Definition: json.hpp:199
static string_key unowned(char const *begin, char const *end)
Create a string from characters at rang [begin,end) without copying the memory. The range should rema...
Definition: json.hpp:254
bool operator==(string_key const &other) const
Compare two strings.
Definition: json.hpp:310
char const * end() const
Get a pointer to the one past last character in the string.
Definition: json.hpp:271