Skip to content

Commit ae5f7ed

Browse files
committed
Do not try to deserialize the geojson attribute.
1 parent fc5cd85 commit ae5f7ed

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

ppp_datamodel/nodes/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .abstractnode import AbstractNode
44
from .triple import Triple
55
from .missing import Missing
6-
from .resource import Resource
6+
from .resource import *
77
from .sentence import Sentence
88
from .list import List
99
from .list_operators import Union, Intersection, And, Or, First, Last, Sort

ppp_datamodel/nodes/abstractnode.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,22 @@ def from_json(cls, data):
116116
def from_dict(cls, data):
117117
cls._test_can_import_json(data)
118118

119-
# Create node instances
120-
conv = lambda v: cls.from_dict(v) if isinstance(v, dict) else v
121-
data = {k.replace('-', '_'): conv(v) for (k, v) in data.items()}
119+
# Find a class that will deserialize the dict as specifically
120+
# as possible
122121
while True:
123122
cls2 = cls._select_class(data)
124123
if cls is cls2:
125124
break
126125
cls = cls2
126+
conv = (lambda k,v: cls.deserialize_attribute(k, v)
127+
if isinstance(v, dict) else v)
128+
data = {k.replace('-', '_'): conv(k,v) for (k, v) in data.items()}
127129
return cls(**data)
128130

131+
@classmethod
132+
def deserialize_attribute(cls, key, value):
133+
return AbstractNode.from_dict(value)
134+
129135
@classmethod
130136
def _select_class(cls, data):
131137
return TYPE_TO_CLASS[data['type']]

ppp_datamodel/nodes/resource.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
from ..log import logger
44
from .abstractnode import register, AbstractNode
55

6+
__all__ = ['Resource', 'StringResource', 'MathLatexResource',
7+
'BooleanResource', 'TimeResource', 'GeojsonResource']
8+
69
EXTRA_ATTRIBUTES = {
710
'string': ('language',),
811
'boolean': (),
@@ -28,7 +31,7 @@ class Resource(AbstractNode):
2831

2932
@classmethod
3033
def _select_class(cls, data):
31-
type_ = data.get('value_type', 'string')
34+
type_ = data.get('value-type', 'string')
3235
if type_ not in VALUE_TYPE_TO_CLASS:
3336
logger.warning('Unknown value-type: %s' % type_)
3437
type_ = 'string'
@@ -96,3 +99,14 @@ def _parse_value(value, attributes):
9699
def _format_value(value):
97100
return value
98101

102+
@register_valuetype
103+
class GeojsonResource(Resource):
104+
_value_type = 'geo-json'
105+
_possible_attributes = Resource._possible_attributes + ('geojson',)
106+
107+
@classmethod
108+
def deserialize_attribute(cls, key, value):
109+
if key == 'geojson':
110+
return value
111+
else:
112+
super().deserialize_attribute(key, value)

tests/test_resource.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ppp_datamodel import AbstractNode, Triple, Resource, Missing
2+
from ppp_datamodel import BooleanResource
23

34
from unittest import TestCase
45

@@ -16,6 +17,7 @@ def testValueType(self):
1617
def testBoolean(self):
1718
d = {'type': 'resource', 'value': 'true', 'value-type': 'boolean'}
1819
o = AbstractNode.from_dict(d)
20+
self.assertIsInstance(o, BooleanResource)
1921
self.assertEqual(o.value, True)
2022
self.assertEqual(o.as_dict(), d)
2123

@@ -24,3 +26,16 @@ def testTime(self):
2426
o = AbstractNode.from_dict(d)
2527
self.assertEqual(o.value, '2010-05-08T23:41:54.000Z')
2628

29+
def testGeojson(self):
30+
d = {
31+
"type": "resource",
32+
"value": "+125.6, +10.1",
33+
"geojson": {
34+
"type": "Feature",
35+
"geometry": {"type": "Point", "coordinates": [125.6, 10.1]},
36+
"properties": {"name": "Dinagat Islands"}
37+
},
38+
"value-type": "geo-json"
39+
}
40+
o = AbstractNode.from_dict(d)
41+
self.assertEqual(o.geojson['type'], 'Feature')

0 commit comments

Comments
 (0)