comparison C++/modules/Json/Json.cpp @ 451:902b034df6e3

Json: escape string construct a new string
author David Demelier <markand@malikania.fr>
date Mon, 02 Nov 2015 13:46:56 +0100
parents f5e62f6c1475
children b681299e6987
comparison
equal deleted inserted replaced
450:f5b491a14a28 451:902b034df6e3
171 Document::Document(File file) 171 Document::Document(File file)
172 { 172 {
173 m_value = convert(json_load_file, file.path.c_str(), 0); 173 m_value = convert(json_load_file, file.path.c_str(), 0);
174 } 174 }
175 175
176 std::string escape(std::string value) noexcept 176 std::string escape(const std::string &value)
177 { 177 {
178 std::string result;
179
178 for (auto it = value.begin(); it != value.end(); ++it) { 180 for (auto it = value.begin(); it != value.end(); ++it) {
179 switch (*it) { 181 switch (*it) {
180 case '\\': 182 case '\\':
183 result += "\\\\";
184 break;
181 case '/': 185 case '/':
186 result += "\\/";
187 break;
182 case '"': 188 case '"':
183 it = value.insert(it, '\\'); 189 result += "\\\"";
184 it++;
185 break; 190 break;
186 case '\b': 191 case '\b':
187 value.replace(it, it + 1, "\\b"); 192 result += "\\b";
188 it += 1;
189 break; 193 break;
190 case '\f': 194 case '\f':
191 value.replace(it, it + 1, "\\f"); 195 result += "\\f";
192 it += 1;
193 break; 196 break;
194 case '\n': 197 case '\n':
195 value.replace(it, it + 1, "\\n"); 198 result += "\\n";
196 it += 1;
197 break; 199 break;
198 case '\r': 200 case '\r':
199 value.replace(it, it + 1, "\\r"); 201 result += "\\r";
200 it += 1;
201 break; 202 break;
202 case '\t': 203 case '\t':
203 value.replace(it, it + 1, "\\t"); 204 result += "\\t";
204 it += 1;
205 break; 205 break;
206 default: 206 default:
207 result += *it;
207 break; 208 break;
208 } 209 }
209 } 210 }
210 211
211 return value; 212 return result;
212 } 213 }
213 214
214 } // !json 215 } // !json