Skip to content

Commit

Permalink
v2 to v3 conversion: avoid json-in-json for metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Oct 27, 2022
1 parent d6e18eb commit 79c9f52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pmtiles/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,18 @@ func v2_to_header_json(v2_json_metadata map[string]interface{}, first4 []byte) (
}
}

// deserialize embedded JSON and lift keys to top-level
// to avoid "json-in-json"
if val, ok := v2_json_metadata["json"]; ok {
string_val := val.(string)
var inside map[string]interface{}
json.Unmarshal([]byte(string_val), &inside)
for k, v := range inside {
v2_json_metadata[k] = v
}
delete(v2_json_metadata, "json")
}

return header, v2_json_metadata, nil
}

Expand Down
4 changes: 4 additions & 0 deletions pmtiles/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestV2UpgradeExtra(t *testing.T) {
"center": "-122.1906,37.7599,11",
"format": "pbf",
"compression": "gzip",
"json": "{\"abc\":\"def\"}",
}, []byte{0x0, 0x0, 0x0, 0x0})
if err != nil {
t.Fatalf("parsing error %s", err)
Expand All @@ -89,6 +90,9 @@ func TestV2UpgradeExtra(t *testing.T) {
if _, ok := json_metadata["center"]; ok {
t.Fatalf("expected center not in result")
}
if _, ok := json_metadata["abc"]; !ok {
t.Fatalf("expected abc in result")
}
}

func TestZoomCenterDefaults(t *testing.T) {
Expand Down

0 comments on commit 79c9f52

Please sign in to comment.