Skip to content

Commit

Permalink
closes #177
Browse files Browse the repository at this point in the history
  • Loading branch information
xrotwang committed Apr 26, 2024
1 parent 2ea08a7 commit 18f5fe4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The `pycldf` package adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

Fixed bug whereby `dict` returned by `orm.Language.as_geojson_feature` could not be serialized
by `json.dumps`.


## [1.37.1] - 2024-03-18

Fixed bug whereby component names where the CSV filenames contain underscores were not translated
Expand Down
23 changes: 19 additions & 4 deletions src/pycldf/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def custom_method(self):
"""
import types
import typing
import decimal
import functools
import collections

Expand All @@ -61,6 +62,20 @@ def custom_method(self):
from pycldf import Dataset # pragma: no cover


def to_json(s):
if isinstance(s, (list, tuple)):
return [to_json(ss) for ss in s]
if isinstance(s, dict):
return {k: to_json(v) for k, v in s.items()}
if isinstance(s, decimal.Decimal):
return float(s)
if s is None:
return None
if isinstance(s, (str, int, float, bool)):
return s
return str(s)


class Object:
"""
Represents a row of a CLDF component table.
Expand Down Expand Up @@ -345,11 +360,11 @@ def lonlat(self):
@property
def as_geojson_feature(self):
if self.lonlat:
return {
return to_json({
"type": "Feature",
"geometry": {"type": "Point", "coordinates": list(self.lonlat)},
"properties": self.cldf,
}
"geometry": {"type": "Point", "coordinates": self.lonlat},
"properties": vars(self.cldf),
})

@functools.cached_property
def speaker_area(self):
Expand Down
22 changes: 21 additions & 1 deletion tests/test_orm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import types
import decimal

import pytest

Expand All @@ -8,6 +10,22 @@
from pycldf.orm import Language


@pytest.mark.parametrize(
'input,output',
[
(None, None),
([], []),
({}, {}),
(decimal.Decimal(1), 1),
({'k': [None]}, {'k': [None]}),
(types.SimpleNamespace(a=3), 'namespace(a=3)')
]
)
def test_to_json(input, output):
from pycldf.orm import to_json
assert to_json(input) == output


@pytest.fixture
def dataset2(data):
dsdir = data / 'dataset_with_listvalued_foreign_keys_to_component'
Expand Down Expand Up @@ -78,6 +96,8 @@ def test_examples(structuredataset_with_examples):
assert ex.language != ex.metaLanguage
assert v.code.name == 'Yes' and v.cldf.value == 'ja'
assert isinstance(v.language.as_geojson_feature, dict)
assert v.language.as_geojson_feature['properties']['name']
assert json.dumps(v.language.as_geojson_feature)
assert len(v.language.values) == 2
assert len(v.parameter.values) == 1

Expand Down Expand Up @@ -229,5 +249,5 @@ def test_speakerArea(dataset_with_media):
assert sa
assert sa.mimetype.subtype == 'geo+json'
assert 'properties' in lang.speaker_area_as_geojson_feature

assert json.dumps(lang.speaker_area_as_geojson_feature)
assert dataset_with_media.objects('LanguageTable')[1].speaker_area_as_geojson_feature

0 comments on commit 18f5fe4

Please sign in to comment.