2020#define CPPCMS_JSON_H
2121
2222#include < cppcms/defs.h>
23+ #include < cppcms/string_key.h>
2324#include < booster/copy_ptr.h>
2425#include < booster/backtrace.h>
2526#include < vector>
@@ -60,7 +61,7 @@ namespace json {
6061 // /
6162 // / The json::object - std::map of json::value's
6263 // /
63- typedef std::map<std::string ,value> object;
64+ typedef std::map<string_key ,value> object;
6465
6566 #ifdef CPPCMS_DOXYGEN_DOCS
6667
@@ -263,6 +264,14 @@ namespace json {
263264 // / that holds an object { "y" : 10 } and find("foo") would return value of undefined type.
264265 // /
265266 value const &find (std::string const &path) const ;
267+ // /
268+ // / Searches a value in the path \a path
269+ // /
270+ // / For example if the json::value represents { "x" : { "y" : 10 } }, then find("x.y") would return
271+ // / a reference to value that hold a number 10, find("x") returns a reference to a value
272+ // / that holds an object { "y" : 10 } and find("foo") would return value of undefined type.
273+ // /
274+ value const &find (char const *path) const ;
266275
267276 // /
268277 // / Searches a value in the path \a path, if not found throw bad_value_cast.
@@ -279,12 +288,32 @@ namespace json {
279288 // / a reference to value that hold a number 10, find("x") returns a reference to a value
280289 // / that holds an object { "y" : 10 } and find("foo") throws
281290 // /
291+ value const &at (char const *path) const ;
292+ // /
293+ // / Searches a value in the path \a path, if not found throw bad_value_cast.
294+ // /
295+ // / For example if the json::value represents { "x" : { "y" : 10 } }, then find("x.y") would return
296+ // / a reference to value that hold a number 10, find("x") returns a reference to a value
297+ // / that holds an object { "y" : 10 } and find("foo") throws
298+ // /
282299 value &at (std::string const &path);
300+ // /
301+ // / Searches a value in the path \a path, if not found throw bad_value_cast.
302+ // /
303+ // / For example if the json::value represents { "x" : { "y" : 10 } }, then find("x.y") would return
304+ // / a reference to value that hold a number 10, find("x") returns a reference to a value
305+ // / that holds an object { "y" : 10 } and find("foo") throws
306+ // /
307+ value &at (char const *path);
283308
284309 // /
285310 // / Sets the value \a v at the path \a path, if the path invalid, creates it.
286311 // /
287312 void at (std::string const &path,value const &v);
313+ // /
314+ // / Sets the value \a v at the path \a path, if the path invalid, creates it.
315+ // /
316+ void at (char const *path,value const &v);
288317
289318
290319 // /
@@ -305,6 +334,15 @@ namespace json {
305334 {
306335 return find (path).type ();
307336 }
337+ // /
338+ // / Returns the type of variable in path, if not found returns undefined
339+ // /
340+ // / Same as find(path).type()
341+ // /
342+ json_type type (char const *path) const
343+ {
344+ return find (path).type ();
345+ }
308346
309347 // /
310348 // / Convert an object \a v of type T to a value at specific path, same as at(path,value(v))
@@ -314,6 +352,14 @@ namespace json {
314352 {
315353 at (path,value (v));
316354 }
355+ // /
356+ // / Convert an object \a v of type T to a value at specific path, same as at(path,value(v))
357+ // /
358+ template <typename T>
359+ void set (char const *path,T const &v)
360+ {
361+ at (path,value (v));
362+ }
317363
318364 // /
319365 // / Get a string value from a path \a path. If the path is not invalid or the object
@@ -331,6 +377,22 @@ namespace json {
331377 return def;
332378 }
333379 }
380+ // /
381+ // / Get a string value from a path \a path. If the path is not invalid or the object
382+ // / is not of type string at this path, returns \a def instead
383+ // /
384+ std::string get (char const *path,char const *def) const
385+ {
386+ value const &v=find (path);
387+ if (v.is_undefined ())
388+ return def;
389+ try {
390+ return v.get_value <std::string>();
391+ }
392+ catch (std::bad_cast const &e) {
393+ return def;
394+ }
395+ }
334396
335397 // /
336398 // / Get an object of type T from the path \a path. Throws bad_value_cast if such path does not
@@ -341,7 +403,33 @@ namespace json {
341403 {
342404 return at (path).get_value <T>();
343405 }
406+ // /
407+ // / Get an object of type T from the path \a path. Throws bad_value_cast if such path does not
408+ // / exists of conversion can't be done
409+ // /
410+ template <typename T>
411+ T get (char const *path) const
412+ {
413+ return at (path).get_value <T>();
414+ }
344415
416+ // /
417+ // / Get an object of type T from the path \a path. Returns \a def if such path does not
418+ // / exists of conversion can't be done
419+ // /
420+ template <typename T>
421+ T get (char const *path,T const &def) const
422+ {
423+ value const &v=find (path);
424+ if (v.is_undefined ())
425+ return def;
426+ try {
427+ return v.get_value <T>();
428+ }
429+ catch (std::bad_cast const &e) {
430+ return def;
431+ }
432+ }
345433 // /
346434 // / Get an object of type T from the path \a path. Returns \a def if such path does not
347435 // / exists of conversion can't be done
0 commit comments