mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-04 01:15:41 +02:00
Convert jsonpull to C++ with shared_ptr and std::vector/std::string
Replace the manual malloc/realloc/free memory management in jsonpull with std::shared_ptr ownership. Each json_object now owns its children through std::vector<json_object_ptr>; raw back-pointers to parent and parser remain valid by structural invariant and are cleared on json_disconnect so detached subtrees can outlive their parser. Strings become std::string, child arrays become std::vector, and the old union becomes a struct so non-trivial members can coexist while preserving the existing o->value.xxx access paths. The old jsonpull.c is replaced by jsonpull.cpp, json_stringify now returns std::string, and all callers across tippecanoe, tile-join, tippecanoe-decode, tippecanoe-json-tool, tippecanoe-overzoom and the unit tests are updated to use json_object_ptr / json_pull_ptr. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+40
-40
@@ -25,35 +25,35 @@ static const char *geometry_names[GEOM_TYPES] = {
|
||||
};
|
||||
|
||||
// XXX duplicated
|
||||
static void json_context(json_object *j) {
|
||||
char *s = json_stringify(j);
|
||||
static void json_context(json_object_ptr j) {
|
||||
std::string s = json_stringify(j);
|
||||
|
||||
if (strlen(s) >= 500) {
|
||||
snprintf(s + 497, strlen(s) + 1 - 497, "...");
|
||||
if (s.size() >= 500) {
|
||||
s.resize(497);
|
||||
s.append("...");
|
||||
}
|
||||
|
||||
fprintf(stderr, "in JSON object %s\n", s);
|
||||
free(s); // stringify
|
||||
fprintf(stderr, "in JSON object %s\n", s.c_str());
|
||||
}
|
||||
|
||||
void parse_json(json_feature_action *jfa, json_pull *jp) {
|
||||
void parse_json(json_feature_action *jfa, json_pull_ptr jp) {
|
||||
long long found_hashes = 0;
|
||||
long long found_features = 0;
|
||||
long long found_geometries = 0;
|
||||
|
||||
while (1) {
|
||||
json_object *j = json_read(jp);
|
||||
if (j == NULL) {
|
||||
if (jp->error != NULL) {
|
||||
json_object_ptr j = json_read(jp);
|
||||
if (j == nullptr) {
|
||||
if (jp->error != nullptr) {
|
||||
fprintf(stderr, "%s:%d: %s: ", jfa->fname.c_str(), jp->line, jp->error);
|
||||
if (jp->root != NULL) {
|
||||
if (jp->root != nullptr) {
|
||||
json_context(jp->root);
|
||||
} else {
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
json_free(jp->root);
|
||||
jp->root.reset();
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -65,8 +65,8 @@ void parse_json(json_feature_action *jfa, json_pull *jp) {
|
||||
}
|
||||
}
|
||||
|
||||
json_object *type = json_hash_get(j, "type");
|
||||
if (type == NULL || type->type != JSON_STRING) {
|
||||
json_object_ptr type = json_hash_get(j, "type");
|
||||
if (type == nullptr || type->type != JSON_STRING) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -74,25 +74,25 @@ void parse_json(json_feature_action *jfa, json_pull *jp) {
|
||||
int i;
|
||||
int is_geometry = 0;
|
||||
for (i = 0; i < GEOM_TYPES; i++) {
|
||||
if (strcmp(type->value.string.string, geometry_names[i]) == 0) {
|
||||
if (type->value.string.string == geometry_names[i]) {
|
||||
is_geometry = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_geometry) {
|
||||
if (j->parent != NULL) {
|
||||
if (j->parent->type == JSON_ARRAY && j->parent->parent != NULL) {
|
||||
if (j->parent != nullptr) {
|
||||
if (j->parent->type == JSON_ARRAY && j->parent->parent != nullptr) {
|
||||
if (j->parent->parent->type == JSON_HASH) {
|
||||
json_object *geometries = json_hash_get(j->parent->parent, "geometries");
|
||||
if (geometries != NULL) {
|
||||
json_object_ptr geometries = json_hash_get(j->parent->parent->shared_from_this(), "geometries");
|
||||
if (geometries != nullptr) {
|
||||
// Parent of Parent must be a GeometryCollection
|
||||
is_geometry = 0;
|
||||
}
|
||||
}
|
||||
} else if (j->parent->type == JSON_HASH) {
|
||||
json_object *geometry = json_hash_get(j->parent, "geometry");
|
||||
if (geometry != NULL) {
|
||||
json_object_ptr geometry = json_hash_get(j->parent->shared_from_this(), "geometry");
|
||||
if (geometry != nullptr) {
|
||||
// Parent must be a Feature
|
||||
is_geometry = 0;
|
||||
}
|
||||
@@ -101,10 +101,10 @@ void parse_json(json_feature_action *jfa, json_pull *jp) {
|
||||
}
|
||||
|
||||
if (is_geometry) {
|
||||
json_object *jo = j;
|
||||
while (jo != NULL) {
|
||||
if (jo->parent != NULL && jo->parent->type == JSON_HASH) {
|
||||
if (json_hash_get(jo->parent, "properties") == jo) {
|
||||
json_object *jo = j.get();
|
||||
while (jo != nullptr) {
|
||||
if (jo->parent != nullptr && jo->parent->type == JSON_HASH) {
|
||||
if (json_hash_get(jo->parent->shared_from_this(), "properties").get() == jo) {
|
||||
// Ancestor is the value corresponding to a properties key
|
||||
is_geometry = 0;
|
||||
break;
|
||||
@@ -120,14 +120,14 @@ void parse_json(json_feature_action *jfa, json_pull *jp) {
|
||||
}
|
||||
found_geometries++;
|
||||
|
||||
jfa->add_feature(j, false, NULL, NULL, NULL, j);
|
||||
jfa->add_feature(j, false, nullptr, nullptr, nullptr, j);
|
||||
json_free(j);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(type->value.string.string, "Feature") != 0) {
|
||||
if (strcmp(type->value.string.string, "FeatureCollection") == 0) {
|
||||
if (type->value.string.string != "Feature") {
|
||||
if (type->value.string.string == "FeatureCollection") {
|
||||
jfa->check_crs(j);
|
||||
json_free(j);
|
||||
}
|
||||
@@ -140,16 +140,16 @@ void parse_json(json_feature_action *jfa, json_pull *jp) {
|
||||
}
|
||||
found_features++;
|
||||
|
||||
json_object *geometry = json_hash_get(j, "geometry");
|
||||
if (geometry == NULL) {
|
||||
json_object_ptr geometry = json_hash_get(j, "geometry");
|
||||
if (geometry == nullptr) {
|
||||
fprintf(stderr, "%s:%d: feature with no geometry: ", jfa->fname.c_str(), jp->line);
|
||||
json_context(j);
|
||||
json_free(j);
|
||||
continue;
|
||||
}
|
||||
|
||||
json_object *properties = json_hash_get(j, "properties");
|
||||
if (properties == NULL || (properties->type != JSON_HASH && properties->type != JSON_NULL)) {
|
||||
json_object_ptr properties = json_hash_get(j, "properties");
|
||||
if (properties == nullptr || (properties->type != JSON_HASH && properties->type != JSON_NULL)) {
|
||||
fprintf(stderr, "%s:%d: feature without properties hash: ", jfa->fname.c_str(), jp->line);
|
||||
json_context(j);
|
||||
json_free(j);
|
||||
@@ -158,10 +158,10 @@ void parse_json(json_feature_action *jfa, json_pull *jp) {
|
||||
|
||||
bool is_feature = true;
|
||||
{
|
||||
json_object *jo = j;
|
||||
while (jo != NULL) {
|
||||
if (jo->parent != NULL && jo->parent->type == JSON_HASH) {
|
||||
if (json_hash_get(jo->parent, "properties") == jo) {
|
||||
json_object *jo = j.get();
|
||||
while (jo != nullptr) {
|
||||
if (jo->parent != nullptr && jo->parent->type == JSON_HASH) {
|
||||
if (json_hash_get(jo->parent->shared_from_this(), "properties").get() == jo) {
|
||||
// Ancestor is the value corresponding to a properties key
|
||||
is_feature = false;
|
||||
break;
|
||||
@@ -174,11 +174,11 @@ void parse_json(json_feature_action *jfa, json_pull *jp) {
|
||||
continue;
|
||||
}
|
||||
|
||||
json_object *tippecanoe = json_hash_get(j, "tippecanoe");
|
||||
json_object *id = json_hash_get(j, "id");
|
||||
json_object_ptr tippecanoe = json_hash_get(j, "tippecanoe");
|
||||
json_object_ptr id = json_hash_get(j, "id");
|
||||
|
||||
json_object *geometries = json_hash_get(geometry, "geometries");
|
||||
if (geometries != NULL && geometries->type == JSON_ARRAY) {
|
||||
json_object_ptr geometries = json_hash_get(geometry, "geometries");
|
||||
if (geometries != nullptr && geometries->type == JSON_ARRAY) {
|
||||
jfa->add_feature(geometries, true, properties, id, tippecanoe, j);
|
||||
} else {
|
||||
jfa->add_feature(geometry, false, properties, id, tippecanoe, j);
|
||||
|
||||
Reference in New Issue
Block a user