I was searching for a way to replace characters in a ostringstream after I've filled it with string content but there are only some very inefficient solutions which extract the string, modify it and put it back into the ostringstream.
Now I wonder if there's a way to automatically replace those characters at the time I add the string. E.g.
ostringstream my_json;
my_json << replace_singlequotes; # modify the stringsteam once
my_json << "{'this':";
my_json << " 'is valid JSON'}";
std::cout << my_json.str();
output:
{"this": "is valid JSON"}
Can you write a custom filter for a ostringstream similar to the format modifiers like std::hex, etc. which modifies a given string before piping it into the stream?
Or is there any other way to replace characters in a ostringstream except running std::replace() on my_json.str() as suggested in other questions and howtos?
You can use an user defined manipulator for this purpose. Please see the example as follows:
#include <iostream>
#include <sstream>
class replace_singlequotes {
friend std::ostream& operator<<(std::ostream &, const replace_singlequotes &);
private:
std::string str;
public:
replace_singlequotes(std::string);
};
replace_singlequotes::replace_singlequotes(std::string str) {
this->str = str;
}
std::ostream& operator<<(std::ostream& os, const replace_singlequotes &value) {
std::string result = value.str;
for (int i = 0; i < result.length(); i++) {
if (result.at(i) == '\'') {
result.at(i) = '\"';
}
}
os << result;
return os;
}
int main() {
std::ostringstream my_json;
my_json << replace_singlequotes("{'this': 'is valid JSON'}");
std::cout << my_json.str() << std::endl;
return 0;
}
The output will be as follows:
{"this": "is valid JSON"}
Update: Here is one another way of doing this using the concept of operator overloading:
#include <iostream>
#include <sstream>
class String {
private:
std::string value;
public:
String operator=(const std::string value);
friend std::ostream & operator<< (std::ostream &out, String const &str);
friend std::istream& operator>>(std::istream& in, String &str);
};
std::ostream & operator<<(std::ostream &out, const String &str) {
std::string result = str.value;
for (int i = 0; i < result.length(); i++) {
if (result.at(i) == '\'') {
result.at(i) = '\"';
}
}
out << result;
return out;
}
std::istream& operator>>(std::istream& in, String &str) {
in >> str.value;
return in;
}
String String::operator=(const std::string value) {
this->value = value;
return *this;
}
int main() {
std::stringstream out;
String str;
str = "{'this': 'is valid JSON'}";
out << str;
std::cout<<out.str();
return 0;
}
Note:
{"this": "is
valid JSON"}insertion operator (<<)
directly to replace the single quote by a double quote.If you would like to use replace_singlequotes as a manipulator and
if you would like to combine the overloading concept with this, I would suggest
that you follow the steps below:
boolean flag called as replace_singlequotes in the
class.static.true/false and decide on if you have
to replace the single quote by a double quote in the overloaded body of the insertion operator(<<).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With