Boost ptree get() return non printable characters?
Boost ptree get<std::string>() return non printable characters?
I was wondering if it is possible for the ptree get function if called like:get<std::string>("string")
on a JSON string to return non-printable characters? And if so, how do I filter thenm out best?
The problem I'm seeing is that I isnert the strings into a std::map
and every instance thereafter should just update the original entry but what happens is that I end up with multiple entries in the end.
I use if (mymap->insert(std::make_pair(str, dat)).second == false){
(where str
is what comes out of the above get()
function) to check for existing entries - or insert a new one.dat
is a strcture of data associated with the str
that should be updated if it the entry exists already (which happens in the false
case).
get<std::string>("string")
std::map
if (mymap->insert(std::make_pair(str, dat)).second == false){
str
get()
dat
str
false
1 Answer
1
I was wondering if it is possible for the ptree get function if called like:
get("string") on a JSON string to return non-printable characters?
It is not possible to call get<std::string>
on a (JSON) string. You can however call it on a ptree
and of course it can return data. So, if that data contains non-printables, then they're in your data.
get<std::string>
ptree
I use if (mymap->insert(std::make_pair(str, dat)).second == false){
(where str
is what comes out of the above get()
function) to check for existing entries - or insert a new one.
if (mymap->insert(std::make_pair(str, dat)).second == false){
str
get()
That's the correct way - assuming that your keys equivalence¹ is indeed given by std::less<std::string>
.
std::less<std::string>
dat is a strcture of data associated with the str that should be updated if it the entry exists already (which happens in the false case).
In that case, consider not checking for existence, but indeed just updating:
mymap[str] = dat;
This will automatically insert a new element if the key wasn't already there. Note that this will not change the behaviour with regards to the keys.
Simplify to check your understanding:
Live On Coliru
#include <map>
#include <string>
#include <iostream>
#include <iomanip>
struct dat {
int i, j, k;
};
static inline std::ostream& operator<<(std::ostream& os, dat const& d) {
return os << "{" << d.i << "," << d.j << "," << d.k << "}";
}
using Map = std::map<std::string, dat>;
void dump(Map const& m) {
std::cout << " --- Map: n";
for (auto& entry : m) {
std::cout << std::quoted(entry.first) << " -> " << entry.second << "n";
}
}
int main() {
Map mymap {
{ "one", {1,2,3} },
{ "two", {2,4,6} },
{ "three", {3,6,9} },
};
dump(mymap);
mymap["four"] = {4,8,12}; // adds
dump(mymap);
mymap["three"] = {0,0,0}; // overwrites
dump(mymap);
mymap["three "] = {-1,-1,-1}; // adds new key
dump(mymap);
}
Prints
--- Map:
"one" -> {1,2,3}
"three" -> {3,6,9}
"two" -> {2,4,6}
--- Map:
"four" -> {4,8,12}
"one" -> {1,2,3}
"three" -> {3,6,9}
"two" -> {2,4,6}
--- Map:
"four" -> {4,8,12}
"one" -> {1,2,3}
"three" -> {0,0,0}
"two" -> {2,4,6}
--- Map:
"four" -> {4,8,12}
"one" -> {1,2,3}
"three" -> {0,0,0}
"three " -> {-1,-1,-1}
"two" -> {2,4,6}
¹ weak total ordering
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment