From 671c0046ee2f9f00b7b6c38d4bba42609ed0ec0c Mon Sep 17 00:00:00 2001 From: artemp Date: Wed, 4 Feb 2015 18:23:43 +0100 Subject: [PATCH] allow "stringified" nested objects and arrays as feature property --- include/mapnik/json/feature_grammar.hpp | 4 +++- include/mapnik/json/feature_grammar_impl.hpp | 14 ++++++++++---- tests/data/json/escaped.geojson | 4 +++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/mapnik/json/feature_grammar.hpp b/include/mapnik/json/feature_grammar.hpp index b00e2b686..6728f0e7d 100644 --- a/include/mapnik/json/feature_grammar.hpp +++ b/include/mapnik/json/feature_grammar.hpp @@ -104,7 +104,9 @@ struct feature_grammar : qi::rule properties; qi::rule, void(FeatureType &),space_type> attributes; qi::rule attribute_value; - + qi::rule, std::string(), space_type> stringify_object; + qi::rule, std::string(), space_type> stringify_array; + // functions phoenix::function put_property_; phoenix::function extract_geometry_; // error handler diff --git a/include/mapnik/json/feature_grammar_impl.hpp b/include/mapnik/json/feature_grammar_impl.hpp index ae1532052..c5cc89798 100644 --- a/include/mapnik/json/feature_grammar_impl.hpp +++ b/include/mapnik/json/feature_grammar_impl.hpp @@ -43,15 +43,14 @@ feature_grammar::feature_grammar(mapnik::tran qi::_a_type _a; qi::_r1_type _r1; qi::eps_type eps; - + qi::char_type char_; using qi::fail; using qi::on_error; using phoenix::new_; using phoenix::construct; // generic json types - json_.value = json_.object | json_.array | json_.string_ - | json_.number + json_.value = json_.object | json_.array | json_.string_ | json_.number ; json_.pairs = json_.key_value % lit(',') @@ -94,7 +93,14 @@ feature_grammar::feature_grammar(mapnik::tran attributes = (json_.string_ [_a = _1] > lit(':') > attribute_value [put_property_(_r1,_a,_1)]) % lit(',') ; - attribute_value %= json_.number | json_.string_ ; + attribute_value %= json_.number | json_.string_ | stringify_object | stringify_array + ; + + stringify_object %= char_('{')[_a = 1 ] >> *(eps(_a > 0) >> (char_('{')[_a +=1] | char_('}')[_a -=1] | char_)) + ; + + stringify_array %= char_('[')[_a = 1 ] >> *(eps(_a > 0) >> (char_('[')[_a +=1] | char_(']')[_a -=1] | char_)) + ; feature.name("Feature"); feature_type.name("type"); diff --git a/tests/data/json/escaped.geojson b/tests/data/json/escaped.geojson index bb7ce918b..4ac210c08 100644 --- a/tests/data/json/escaped.geojson +++ b/tests/data/json/escaped.geojson @@ -21,7 +21,9 @@ "spaces":"this has spaces", "double":1.1, "boolean":true, - "NOM_FR":"Québec" + "NOM_FR":"Québec", + "object": {"value":{"type":"u'\u041c\u0430pni\u043a'","array": [3,0,"x"]}}, + "array" : [ [ [1], ["deux"]],[[3,"four","\u4e94"]]] } } ]