41 ptr_(other.ptr_ ?
new T(*other.ptr_) : 0)
68 T
const *
get()
const {
return ptr_; }
69 T *
get() {
return ptr_; }
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; }
110 static const size_t npos = -1;
144 return end() - begin();
166 return end() == begin();
172 size_t find(
char c,
size_t pos = 0)
const 177 char const *p=begin() + pos;
178 while(pos <= s && *p!=c) {
204 char const *p=begin() + pos;
206 if(n >
size_t(e-p)) {
219 return *(begin() + n);
224 char const &
at(
size_t n)
const 227 throw std::out_of_range(
"dlprim::string_key::at() range error");
228 return *(begin() + n);
237 return string_key(v.c_str(),v.c_str()+v.size());
245 char const *end = str;
275 return key_.c_str() + key_.size();
282 return std::lexicographical_compare( begin(),end(),
284 std::char_traits<char>::lt);
291 return other < *
this;
298 return !(*
this < other);
305 return !(*
this > other);
312 return (end() - begin() == other.
end() - other.
begin())
313 && memcmp(begin(),other.
begin(),end()-begin()) == 0;
320 return !(*
this==other);
337 return std::string(begin_,end_-begin_);
344 operator std::string()
const 584 typedef std::map<string_key,value>
object;
621 bad_value_cast(std::string
const &s,json_type expected, json_type actual);
624 virtual const char* what()
const throw();
644 std::ostream DLPRIM_API &
operator<<(std::ostream &out,json_type);
658 json_type type()
const;
672 bool const &boolean()
const;
676 double const &number()
const;
680 std::string
const &str()
const;
727 void number(
double );
731 void str(std::string
const &);
767 value const &find(std::string
const &path)
const;
775 value const &find(
char const *path)
const;
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);
813 void at(std::string
const &path,
value const &v);
817 void at(
char const *path,
value const &v);
834 json_type
type(std::string
const &path)
const 836 return find(path).
type();
843 json_type
type(
char const *path)
const 845 return find(path).
type();
852 void set(std::string
const &path,T
const &v)
860 void set(
char const *path,T
const &v)
869 std::string
get(std::string
const &path,
char const *def)
const 871 value const &v=find(path);
877 catch(std::bad_cast
const &e) {
885 std::string
get(
char const *path,
char const *def)
const 887 value const &v=find(path);
893 catch(std::bad_cast
const &e) {
903 T
get(std::string
const &path)
const 905 return at(path).get_value<T>();
912 T
get(
char const *path)
const 914 return at(path).get_value<T>();
922 T
get(
char const *path,T
const &def)
const 924 value const &v=find(path);
930 catch(std::bad_cast
const &e) {
939 T
get(std::string
const &path,T
const &def)
const 941 value const &v=find(path);
947 catch(std::bad_cast
const &e) {
959 value &operator[](std::string
const &name);
967 value const &operator[](std::string
const &name)
const;
973 value &operator[](
size_t n);
978 value const &operator[](
size_t n)
const;
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);
1011 bool load(
char const *&begin,
char const *end,
bool full,
int *line_number=0);
1028 d=std::move(other.d);
1078 void write(std::ostream &out,
int tabs)
const;
1079 void write_value(std::ostream &out,
int tabs)
const;
1082 struct DLPRIM_API copyable {
1084 _data *operator->() {
return &*d; }
1085 _data &operator*() {
return *d; }
1086 _data
const *operator->()
const {
return &*d; }
1087 _data
const &operator*()
const {
return *d; }
1090 copyable(copyable
const &r);
1091 copyable(copyable &&);
1092 copyable &operator=(copyable &&r);
1093 copyable
const &operator=(copyable
const &r);
1096 void swap(copyable &other)
1104 friend struct copyable;
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);
1127 void DLPRIM_API
to_json(std::string
const &str,std::ostream &out);
1132 template<
typename T1,
typename T2>
1133 struct traits<std::pair<T1,T2> > {
1134 static std::pair<T1,T2>
get(
value const &v)
1141 static void set(
value &v,std::pair<T1,T2>
const &in)
1144 v.set_value(
"first",in.first);
1145 v.set_value(
"second",in.second);
1149 template<
typename T>
1150 struct traits<std::vector<T> > {
1151 static std::vector<T>
get(
value const &v)
1153 std::vector<T> result;
1155 result.resize(a.size());
1156 for(
unsigned i=0;i<a.size();i++)
1157 result[i]=a[i].get_value<T>();
1160 static void set(
value &v,std::vector<T>
const &in)
1164 a.resize(in.size());
1165 for(
unsigned i=0;i<in.size();i++)
1166 a[i].set_value(in[i]);
1171 #define DLPRIM_JSON_SPECIALIZE(type,method) \ 1173 struct traits<type> { \ 1174 static type get(value const &v) \ 1176 return v.method(); \ 1178 static void set(value &v,type const &in)\ 1184 DLPRIM_JSON_SPECIALIZE(
bool,
boolean);
1185 DLPRIM_JSON_SPECIALIZE(
double,number);
1186 DLPRIM_JSON_SPECIALIZE(std::string,str);
1190 #undef DLPRIM_JSON_SPECIALIZE 1192 #define DLPRIM_JSON_SPECIALIZE_INT(type) \ 1194 struct traits<type> { \ 1195 static type get(value const &v) \ 1197 type res=static_cast<type>(v.number()); \ 1198 if(res!=v.number()) \ 1199 throw bad_value_cast(); \ 1202 static void set(value &v,type const &in) \ 1204 if(std::numeric_limits<type>::digits > \ 1205 std::numeric_limits<double>::digits \ 1206 && static_cast<double>(in)!=in) \ 1208 throw bad_value_cast(); \ 1210 v.number(static_cast<double>(in)); \ 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)
1227 #undef DLPRIM_JSON_SPECIALIZE_INT 1231 static float get(
value const &v)
1234 if( r < (-std::numeric_limits<float>::max())
1235 || std::numeric_limits<float>::max() < r )
1239 return static_cast<float>(r);
1241 static void set(
value &v,
float const &in)
1248 struct traits<long double> {
1249 static long double get(
value const &v)
1253 static void set(
value &v,
long double const &in)
1255 if( in < -std::numeric_limits<double>::max()
1256 || std::numeric_limits<double>::max() < in )
1260 v.number(static_cast<double>(in));
1274 typedef char vtype[n];
1275 static void set(
value &v,vtype
const &in)
1281 struct traits<char const [n]> {
1282 typedef char const vtype[n];
1283 static void set(
value &v,vtype
const &in)
1291 struct traits<char const *> {
1292 static void set(
value &v,
char const *
const &in)
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'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's.
Definition: json.hpp:584
double const & number() const
Convert value to double, throws bad_value_cast if value'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'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'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
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