diff --git a/geojson.cpp b/geojson.cpp index 609e45cb..4d50fbc5 100644 --- a/geojson.cpp +++ b/geojson.cpp @@ -189,13 +189,10 @@ int serialize_geojson_feature(struct serialization_state *sst, json_object *geom for (size_t i = 0; i < nprop; i++) { if (properties->value.object.keys[i]->type == JSON_STRING) { - int type = -1; - - std::string val; - stringify_value(properties->value.object.values[i], type, val, sst->fname, sst->line, feature); + serial_val sv = stringify_value(properties->value.object.values[i], sst->fname, sst->line, feature); keys.emplace_back(properties->value.object.keys[i]->value.string.string); - values.emplace_back(serial_val(type, val)); + values.push_back(std::move(sv)); } } diff --git a/main.cpp b/main.cpp index 82621b59..3c5eeb9a 100644 --- a/main.cpp +++ b/main.cpp @@ -2898,10 +2898,8 @@ void set_attribute_value(const char *arg) { exit(EXIT_JSON); } - serial_val val; - stringify_value(v, val.type, val.s, "json", 1, o); - - set_attributes.insert(std::pair(k->value.string.string, val)); + serial_val val = stringify_value(v, "json", 1, o); + set_attributes.emplace(k->value.string.string, val); } json_free(o); diff --git a/plugin.cpp b/plugin.cpp index 2428bcb9..50d77c03 100644 --- a/plugin.cpp +++ b/plugin.cpp @@ -264,23 +264,16 @@ std::vector parse_layers(int fd, int z, unsigned x, unsigned y, std:: } for (size_t i = 0; i < properties->value.object.length; i++) { - int tp = -1; - std::string s; - - stringify_value(properties->value.object.values[i], tp, s, "Filter output", jp->line, j); + serial_val sv = stringify_value(properties->value.object.values[i], "Filter output", jp->line, j); // Nulls can be excluded here because this is the postfilter // and it is nearly time to create the vector representation - if (tp >= 0 && tp != mvt_null) { - mvt_value v = stringified_to_mvt_value(tp, s.c_str(), tile_stringpool); + if (sv.type != mvt_null) { + mvt_value v = stringified_to_mvt_value(sv.type, sv.s.c_str(), tile_stringpool); l->second.tag(feature, std::string(properties->value.object.keys[i]->value.string.string), v); - serial_val attrib; - attrib.type = tp; - attrib.s = s; - - add_to_tilestats(ts->second.tilestats, std::string(properties->value.object.keys[i]->value.string.string), attrib); + add_to_tilestats(ts->second.tilestats, std::string(properties->value.object.keys[i]->value.string.string), sv); } } @@ -505,24 +498,17 @@ serial_feature parse_feature(json_pull *jp, int z, unsigned x, unsigned y, std:: } for (size_t i = 0; i < properties->value.object.length; i++) { - serial_val v; - v.type = -1; - - stringify_value(properties->value.object.values[i], v.type, v.s, "Filter output", jp->line, j); + serial_val v = stringify_value(properties->value.object.values[i], "Filter output", jp->line, j); // Nulls can be excluded here because the expression evaluation filter // would have already run before prefiltering - if (v.type >= 0 && v.type != mvt_null) { + if (v.type != mvt_null) { sf.full_keys.push_back(std::string(properties->value.object.keys[i]->value.string.string)); sf.full_values.push_back(v); - serial_val attrib; - attrib.s = v.s; - attrib.type = v.type; - if (!postfilter) { - add_to_tilestats(ts->second.tilestats, std::string(properties->value.object.keys[i]->value.string.string), attrib); + add_to_tilestats(ts->second.tilestats, std::string(properties->value.object.keys[i]->value.string.string), v); } } } diff --git a/read_json.cpp b/read_json.cpp index c02e0383..45e13658 100644 --- a/read_json.cpp +++ b/read_json.cpp @@ -13,6 +13,7 @@ #include "mvt.hpp" #include "milo/dtoa_milo.h" #include "errors.hpp" +#include "serial.hpp" const char *geometry_names[GEOM_TYPES] = { "Point", @@ -120,44 +121,48 @@ void parse_geometry(int t, json_object *j, drawvec &out, int op, const char *fna // type and stringified value. All numeric values, even if they are integers, // even integers that are too large to fit in a double but will still be // stringified with their original precision, are recorded here as mvt_double. -void stringify_value(json_object *value, int &type, std::string &stringified, const char *reading, int line, json_object *feature) { +serial_val stringify_value(json_object *value, const char *reading, int line, json_object *feature) { + serial_val sv; + if (value != NULL) { int vt = value->type; if (vt == JSON_STRING) { - type = mvt_string; - stringified = value->value.string.string; + sv.type = mvt_string; + sv.s = value->value.string.string; - std::string err = check_utf8(stringified); + std::string err = check_utf8(sv.s); if (err.size() > 0) { fprintf(stderr, "%s:%d: %s: ", reading, line, err.c_str()); json_context(feature); exit(EXIT_UTF8); } } else if (vt == JSON_NUMBER) { - type = mvt_double; + sv.type = mvt_double; if (value->value.number.large_unsigned != 0) { - stringified = std::to_string(value->value.number.large_unsigned); + sv.s = std::to_string(value->value.number.large_unsigned); } else if (value->value.number.large_signed != 0) { - stringified = std::to_string(value->value.number.large_signed); + sv.s = std::to_string(value->value.number.large_signed); } else { - stringified = milo::dtoa_milo(value->value.number.number); + sv.s = milo::dtoa_milo(value->value.number.number); } } else if (vt == JSON_TRUE) { - type = mvt_bool; - stringified = "true"; + sv.type = mvt_bool; + sv.s = "true"; } else if (vt == JSON_FALSE) { - type = mvt_bool; - stringified = "false"; + sv.type = mvt_bool; + sv.s = "false"; } else if (vt == JSON_NULL) { - type = mvt_null; - stringified = "null"; + sv.type = mvt_null; + sv.s = "null"; } else { - type = mvt_string; + sv.type = mvt_string; const char *v = json_stringify(value); - stringified = std::string(v); + sv.s = std::string(v); free((void *) v); // stringify } } + + return sv; } diff --git a/read_json.hpp b/read_json.hpp index 539497e2..1d9374ab 100644 --- a/read_json.hpp +++ b/read_json.hpp @@ -13,4 +13,4 @@ extern int mb_geometry[GEOM_TYPES]; void json_context(json_object *j); void parse_geometry(int t, json_object *j, drawvec &out, int op, const char *fname, int line, json_object *feature); -void stringify_value(json_object *value, int &type, std::string &stringified, const char *reading, int line, json_object *feature); +serial_val stringify_value(json_object *value, const char *reading, int line, json_object *feature);