Re: [Cppcms-users] Dots in JSON Value keys
Brought to you by:
artyom-beilis
From: Artyom <art...@ya...> - 2010-10-14 07:56:47
|
> > Hallo, > > as the JSON RFC does not state that dots in names are forbidden, > cppcms::json::value should provide a way to request such named values, > as in: > { "dotted.key": "value" } JSON does not forbid such keys but form common knowledge of JavaScript it is not recommended as "." separates between object and its member. > > Currently, a json::value.find("dotted.key") would return an undefined > JSON value, because it would look whether "dotted" has an object as > value and this object has a name "key". Find uses "." as separator in path to the value, something like JSON XPath. If you want to access arbitrary key, you may just use operator [] or just access json::object directly that is just std::map<std::string,json::value> and search for arbitrary keys. > > If you don't want to remove the possibility to access an sub-object via > a dotted path, Of course I don't as it is very convenient way to access various members similar to XML's XPath > perhaps there would be a way to escape a path like in > std::string path = "dotted\\.key"; > And if you mean a backslash, "dotted\\\\.key" No, it is bad idea for several reasons: 1. The search by such path requires parsing it and it is not cheap, especially when users tend to use it widely, so thy notation should be as simple as possible. 2. It is bad practice to had such keys. 3. You still can access the key using operator [] or by accessing it directly via json::object. Read also this: http://art-blog.no-ip.info/cppcms_ref_v0_99/classcppcms_1_1json_1_1value.html#d521e30dcb5abc912c0e4088faca2304 Artyom |