geojson feature_grammar - parse objects and arrays into json_value before applying "stringifier" #3419 https://github.com/mapnik/node-mapnik/issues/642
This commit is contained in:
parent
7374f82414
commit
e528b433d4
2 changed files with 83 additions and 13 deletions
|
@ -31,6 +31,7 @@
|
|||
#include <mapnik/value.hpp>
|
||||
#include <mapnik/json/generic_json.hpp>
|
||||
#include <mapnik/json/value_converters.hpp>
|
||||
#include <mapnik/util/conversions.hpp>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#include <mapnik/warning_ignore.hpp>
|
||||
|
@ -45,8 +46,72 @@ namespace qi = boost::spirit::qi;
|
|||
namespace phoenix = boost::phoenix;
|
||||
namespace fusion = boost::fusion;
|
||||
|
||||
class attribute_value_visitor
|
||||
namespace {
|
||||
struct stringifier
|
||||
{
|
||||
std::string operator()(std::string const& val) const
|
||||
{
|
||||
return "\"" + val + "\"";
|
||||
}
|
||||
|
||||
std::string operator()(value_null) const
|
||||
{
|
||||
return "null";
|
||||
}
|
||||
|
||||
std::string operator()(value_bool val) const
|
||||
{
|
||||
return val ? "true" : "false";
|
||||
}
|
||||
|
||||
std::string operator()(value_integer val) const
|
||||
{
|
||||
std::string str;
|
||||
util::to_string(str, val);
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string operator()(value_double val) const
|
||||
{
|
||||
std::string str;
|
||||
util::to_string(str, val);
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string operator()(std::vector<mapnik::json::json_value> const& array) const
|
||||
{
|
||||
std::string str = "[";
|
||||
bool first = true;
|
||||
for (auto const& val : array)
|
||||
{
|
||||
if (first) first = false;
|
||||
else str += ",";
|
||||
str += mapnik::util::apply_visitor(*this, val);
|
||||
}
|
||||
str += "]";
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string operator()(std::unordered_map<std::string, mapnik::json::json_value> const& object) const
|
||||
{
|
||||
std::string str = "{";
|
||||
bool first = true;
|
||||
for (auto const& kv : object)
|
||||
{
|
||||
if (first) first = false;
|
||||
else str += ",";
|
||||
str += kv.first;
|
||||
str += ":";
|
||||
str += mapnik::util::apply_visitor(*this, kv.second);
|
||||
}
|
||||
str += "}";
|
||||
return str;
|
||||
}
|
||||
};
|
||||
|
||||
} // anonymous ns
|
||||
|
||||
struct attribute_value_visitor
|
||||
{
|
||||
public:
|
||||
attribute_value_visitor(mapnik::transcoder const& tr)
|
||||
|
@ -57,6 +122,18 @@ public:
|
|||
return mapnik::value(tr_.transcode(val.c_str()));
|
||||
}
|
||||
|
||||
mapnik::value operator()(std::vector<mapnik::json::json_value> const& array) const
|
||||
{
|
||||
std::string str = stringifier()(array);
|
||||
return mapnik::value(tr_.transcode(str.c_str()));
|
||||
}
|
||||
|
||||
mapnik::value operator()(std::unordered_map<std::string, mapnik::json::json_value> const& object) const
|
||||
{
|
||||
std::string str = stringifier()(object);
|
||||
return mapnik::value(tr_.transcode(str.c_str()));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
mapnik::value operator()(T const& val) const
|
||||
{
|
||||
|
@ -102,8 +179,6 @@ struct feature_grammar : qi::grammar<Iterator, void(FeatureType&), space_type>
|
|||
qi::rule<Iterator,void(FeatureType &),space_type> properties;
|
||||
qi::rule<Iterator,qi::locals<std::string>, void(FeatureType &),space_type> attributes;
|
||||
qi::rule<Iterator, json_value(), space_type> attribute_value;
|
||||
qi::rule<Iterator, qi::locals<std::int32_t>, std::string(), space_type> stringify_object;
|
||||
qi::rule<Iterator, qi::locals<std::int32_t>, std::string(), space_type> stringify_array;
|
||||
// functions
|
||||
phoenix::function<put_property> put_property_;
|
||||
phoenix::function<set_geometry_impl> set_geometry;
|
||||
|
|
|
@ -44,7 +44,6 @@ feature_grammar<Iterator,FeatureType,ErrorHandler>::feature_grammar(mapnik::tran
|
|||
qi::_r1_type _r1;
|
||||
qi::eps_type eps;
|
||||
qi::char_type char_;
|
||||
qi::no_skip_type no_skip;
|
||||
using qi::fail;
|
||||
using qi::on_error;
|
||||
using phoenix::new_;
|
||||
|
@ -57,17 +56,19 @@ feature_grammar<Iterator,FeatureType,ErrorHandler>::feature_grammar(mapnik::tran
|
|||
json_.pairs = json_.key_value % lit(',')
|
||||
;
|
||||
|
||||
json_.key_value = (json_.string_ > lit(':') > json_.value)
|
||||
json_.key_value = json_.string_ > lit(':') > json_.value
|
||||
;
|
||||
|
||||
json_.object = lit('{')
|
||||
> *json_.pairs
|
||||
> json_.pairs
|
||||
> lit('}')
|
||||
;
|
||||
|
||||
json_.array = lit('[')
|
||||
> json_.value > *(lit(',') > json_.value)
|
||||
> lit(']')
|
||||
;
|
||||
|
||||
json_.number = json_.strict_double[_val = json_.double_converter(_1)]
|
||||
| json_.int__[_val = json_.integer_converter(_1)]
|
||||
| lit("true") [_val = true]
|
||||
|
@ -99,13 +100,7 @@ feature_grammar<Iterator,FeatureType,ErrorHandler>::feature_grammar(mapnik::tran
|
|||
attributes = (json_.string_ [_a = _1] > lit(':') > attribute_value [put_property_(_r1,_a,_1)]) % lit(',')
|
||||
;
|
||||
|
||||
attribute_value %= json_.number | json_.string_ | stringify_object | stringify_array
|
||||
;
|
||||
|
||||
stringify_object %= char_('{')[_a = 1 ] > no_skip[*(eps(_a > 0) > (char_('{')[_a +=1] | char_('}')[_a -=1] | char_))]
|
||||
;
|
||||
|
||||
stringify_array %= char_('[')[_a = 1 ] > no_skip[*(eps(_a > 0) > (char_('[')[_a +=1] | char_(']')[_a -=1] | char_))]
|
||||
attribute_value %= json_.number | json_.string_ | json_.object | json_.array
|
||||
;
|
||||
|
||||
feature.name("Feature");
|
||||
|
|
Loading…
Reference in a new issue