JSON - make generic_json a proper spirit grammar and reduce code bloat

This commit is contained in:
artemp 2016-08-31 14:36:35 +01:00
parent 3f26a1f23f
commit f360f50eed
7 changed files with 69 additions and 99 deletions

View file

@ -144,28 +144,6 @@ extract_bounding_box_grammar<Iterator, Boxes, ErrorHandler>::extract_bounding_bo
rings_array = lit('[') >> rings(_r1) % lit(',') > lit(']')
;
// generic json types
json.value = json.object | json.array | json.string_ | json.number
;
json.key_value = json.string_ >> lit(':') >> json.value
;
json.object = lit('{') >> json.key_value % lit(',') >> lit('}')
;
json.array = lit('[')
>> json.value >> *(lit(',') >> json.value)
>> lit(']')
;
json.number = json.strict_double
| json.int__
| lit("true")
| lit("false")
| lit("null")
;
coords.name("Coordinates");
pos.name("Position");
ring.name("Ring");

View file

@ -35,7 +35,6 @@ feature_grammar<Iterator,FeatureType,ErrorHandler>::feature_grammar(mapnik::tran
qi::lit_type lit;
qi::long_long_type long_long;
qi::double_type double_;
qi::_val_type _val;
qi::_1_type _1;
qi::_2_type _2;
qi::_3_type _3;
@ -49,30 +48,6 @@ feature_grammar<Iterator,FeatureType,ErrorHandler>::feature_grammar(mapnik::tran
using phoenix::new_;
using phoenix::construct;
// generic json types
json_.value = json_.object | json_.array | json_.string_ | json_.number
;
json_.key_value = json_.string_ > lit(':') > json_.value
;
json_.object = lit('{')
> -(json_.key_value % lit(','))
> lit('}')
;
json_.array = lit('[')
> -(json_.value % lit(','))
> lit(']')
;
json_.number = json_.strict_double[_val = json_.double_converter(_1)]
| json_.int__[_val = json_.integer_converter(_1)]
| lit("true") [_val = true]
| lit ("false") [_val = false]
| lit("null")[_val = construct<value_null>()]
;
// geojson types
feature_type = lit("\"type\"") > lit(':') > lit("\"Feature\"")
;

View file

@ -175,8 +175,9 @@ unicode_string<Iterator>::unicode_string()
}
template <typename Iterator>
struct generic_json
struct generic_json : qi::grammar<Iterator, json_value(), space_type>
{
generic_json();
qi::rule<Iterator, json_value(), space_type> value;
qi::int_parser<mapnik::value_integer, 10, 1, -1> int__;
unicode_string<Iterator> string_;

View file

@ -52,31 +52,6 @@ geometry_grammar<Iterator, ErrorHandler>::geometry_grammar()
using phoenix::push_back;
start = geometry.alias() | lit("null");
// generic json types
json_.value = json_.object | json_.array | json_.string_ | json_.number
;
json_.key_value = json_.string_ > lit(':') > json_.value
;
json_.object = lit('{')
> -(json_.key_value % lit(','))
> lit('}')
;
json_.array = lit('[')
> -(json_.value % lit(','))
> lit(']')
;
json_.number = json_.strict_double
| json_.int__
| lit("true")
| lit ("false")
| lit("null")
;
geometry = lit('{')[_a = 0]
> (((lit("\"type\"") > lit(':') > geometry_type_dispatch[_a = _1])
|

View file

@ -26,7 +26,6 @@
// mapnik
#include <mapnik/util/variant.hpp>
#include <mapnik/json/positions.hpp>
#include <mapnik/json/generic_json.hpp>
#include <mapnik/json/error_handler.hpp>
#include <mapnik/geometry.hpp>
#pragma GCC diagnostic push
@ -38,6 +37,8 @@
namespace mapnik { namespace json {
namespace qi = boost::spirit::qi;
namespace standard = boost::spirit::standard;
using space_type = standard::space_type;
template <typename Iterator, typename ErrorHandler = error_handler<Iterator> >
struct positions_grammar :

View file

@ -94,7 +94,6 @@ topojson_grammar<Iterator, ErrorHandler>::topojson_grammar()
using qi::fail;
using qi::on_error;
using phoenix::push_back;
using phoenix::construct;
geometry_type_dispatch.add
("\"Point\"",1)
@ -109,29 +108,6 @@ topojson_grammar<Iterator, ErrorHandler>::topojson_grammar()
// error handler
boost::phoenix::function<ErrorHandler> const error_handler;
boost::phoenix::function<create_geometry_impl> const create_geometry;
// generic JSON types
json.value = json.object | json.array | json.string_ | json.number
;
json.key_value = json.string_ > lit(':') > json.value
;
json.object = lit('{')
> -(json.key_value % lit(','))
> lit('}')
;
json.array = lit('[')
> -(json.value % lit(','))
> lit(']')
;
json.number = json.strict_double[_val = json.double_converter(_1)]
| json.int__[_val = json.integer_converter(_1)]
| lit("true")[_val = true]
| lit("false")[_val = false]
| lit("null")[_val = construct<value_null>()]
;
// topo json
topology = lit('{') >> lit("\"type\"") >> lit(':') >> lit("\"Topology\"")

64
src/json/generic_json.cpp Normal file
View file

@ -0,0 +1,64 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#include <mapnik/json/generic_json.hpp>
namespace mapnik { namespace json {
template <typename Iterator>
generic_json<Iterator>::generic_json()
: generic_json::base_type(value)
{
qi::lit_type lit;
qi::_val_type _val;
qi::_1_type _1;
using phoenix::construct;
// generic json types
value = object | array | string_ | number
;
key_value = string_ > lit(':') > value
;
object = lit('{')
> -(key_value % lit(','))
> lit('}')
;
array = lit('[')
> -(value % lit(','))
> lit(']')
;
number = strict_double[_val = double_converter(_1)]
| int__[_val = integer_converter(_1)]
| lit("true") [_val = true]
| lit ("false") [_val = false]
| lit("null")[_val = construct<value_null>()]
;
}
}}
using iterator_type = char const*;
template struct mapnik::json::generic_json<iterator_type>;
template struct mapnik::json::generic_json<std::string::const_iterator>;