rundel / carto-generator

C++ Parser for the Carto stylesheet language
15 stars 1 forks source link

json and escaping characters #10

Open springmeyer opened 13 years ago

springmeyer commented 13 years ago

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.

springmeyer commented 13 years ago

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();
springmeyer commented 13 years ago

@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/