Skip to content

Commit fee3643

Browse files
authored
Merge pull request #915 from compas-dev/clr_support_for_encoders
Decode from CLR data types as well as python dictionaries
2 parents d795a8b + a634355 commit fee3643

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
* Added `compas_blender.geometry.booleans` as plugin for boolean pluggables.
2626
* Added version-based installation for Blender.
2727
* Added several shape artists to `compas_ghpython`: `BoxArtist`, `CapsuleArtist`, `ConeArtist`, `CylinderArtist`, `PolygonArtist`, `PolyhedronArtist`, `SphereArtist`, `TorusArtist` and `VectorArtist`.
28-
28+
* Added support for CLR generic dictionaries to the `compas.data` decoders.
2929

3030
### Changed
3131

src/compas/data/encoders.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33
from __future__ import division
44

55
import json
6+
import sys
67

78
from compas.data.exceptions import DecoderError
89

10+
# We don't do this from `compas.IPY` to avoid circular imports
11+
if 'ironpython' in sys.version.lower():
12+
try:
13+
from System.Collections.Generic import IDictionary
14+
except: # noqa: E722
15+
IDictionary = None
16+
else:
17+
IDictionary = None
18+
919

1020
def cls_from_dtype(dtype):
1121
"""Get the class object corresponding to a COMPAS data type specification.
@@ -102,4 +112,10 @@ def object_hook(self, o):
102112
except AttributeError:
103113
raise DecoderError("The data type can't be found in the specified module: {}.".format(o['dtype']))
104114

105-
return cls.from_data(o['value'])
115+
obj_value = o['value']
116+
117+
# Kick-off from_data from a rebuilt Python dictionary instead of the C# data type
118+
if IDictionary and isinstance(o, IDictionary[str, object]):
119+
obj_value = {key: obj_value[key] for key in obj_value.Keys}
120+
121+
return cls.from_data(obj_value)

0 commit comments

Comments
 (0)