diff C++/modules/Json/Json.cpp @ 445:f5e62f6c1475

Json: add escape free function
author David Demelier <markand@malikania.fr>
date Mon, 26 Oct 2015 19:29:34 +0100
parents d485d36a8de1
children 902b034df6e3
line wrap: on
line diff
--- a/C++/modules/Json/Json.cpp	Fri Oct 23 10:11:34 2015 +0200
+++ b/C++/modules/Json/Json.cpp	Mon Oct 26 19:29:34 2015 +0100
@@ -173,4 +173,42 @@
 	m_value = convert(json_load_file, file.path.c_str(), 0);
 }
 
+std::string escape(std::string value) noexcept
+{
+	for (auto it = value.begin(); it != value.end(); ++it) {
+		switch (*it) {
+		case '\\':
+		case '/':
+		case '"':
+			it = value.insert(it, '\\');
+			it++;
+			break;
+		case '\b':
+			value.replace(it, it + 1, "\\b");
+			it += 1;
+			break;
+		case '\f':
+			value.replace(it, it + 1, "\\f");
+			it += 1;
+			break;
+		case '\n':
+			value.replace(it, it + 1, "\\n");
+			it += 1;
+			break;
+		case '\r':
+			value.replace(it, it + 1, "\\r");
+			it += 1;
+			break;
+		case '\t':
+			value.replace(it, it + 1, "\\t");
+			it += 1;
+			break;
+		default:
+			break;
+		}
+	}
+
+	return value;
+}
+
 } // !json