carto-generator
carto-generator copied to clipboard
json and escaping characters
Mapnik XML can have linebreaks in strings, especially common in things like the sql subselects often used in the postgres 'table' parameter. These are ultimately stripped by mapnik before sending to postgres to avoid any issues: http://trac.mapnik.org/browser/trunk/include/mapnik/sql_utils.hpp#L58
Cascadenik uses XML for the mml and supports linebreaks as well:
https://github.com/mapnik/Cascadenik/blob/master/openstreetmap/style.mml#L97-105
But json need them to be escaped.
Also " can appear in sql selects, so those also need to be escaped.
this solves the two issues I encountered where the output json was not valid, but surely we should find a better overall approach to escaping:
diff --git a/include/gen_layer.hpp b/include/gen_layer.hpp
index 23e7554..301b4a9 100644
--- a/include/gen_layer.hpp
+++ b/include/gen_layer.hpp
@@ -53,7 +53,13 @@ struct layer_data {
style_names_ = rhs.styles();
datasource_params_ = rhs.datasource()->params();
-
+ boost::optional<std::string> table = datasource_params_.get<std::string>("table");
+ if (table) {
+ std::string table_val = *table;
+ boost::replace_all(table_val,"\n","\\n");
+ boost::replace_all(table_val,"\"","\\\"");
+ datasource_params_["table"] = table_val;
+ }
if (rhs.title() != "")
title_ = rhs.title();
@rundel - any thoughts on this one? This is the only major blocker to converted the massive "osm.xml" at http://svn.openstreetmap.org/applications/rendering/mapnik/