Skip to content

Commit 89cf156

Browse files
committed
data copy name
1 parent d60c4c1 commit 89cf156

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
* Added instructions for creating new data types to the dev guide.
13+
* Added `compact=False`, `minimal=False` to `compas.data.Data.to_json()` to `compas.data.Data.to_jsonstring()`.
1314

1415
### Changed
1516

16-
* Fixed `RuntimeError` when using `compas_rhino.unload_modules` in CPython`.
17+
* Fixed `RuntimeError` when using `compas_rhino.unload_modules` in CPython`.
1718
* Fixed bug in `Box.scaled` causing a `TypeError` due to incorrect parameter forwarding.
1819
* Changed argument names of `Box.scale()` to `x`, `y`, `z`, instead of `factor` and made `y` and `z` optional to keep positional arguments backwards compatible.
1920
* Fixed import errors in `compas_rhino.conduits` for Rhino 8.
2021
* Fixed doctest failures.
2122
* Fixed bug in serialization when `compas.datastructures.attributes.AttributeView` is used.
2223
* Fixed bug in the serialisation of empty scenes.
24+
* Fixed bug in serialisation process due to `name` attribute appearing in json representation after copy even if not present before copy.
2325

2426
### Removed
2527

src/compas/data/data.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,22 @@ def from_json(cls, filepath): # type: (...) -> Data
210210
raise TypeError("The data in the file is not a {}.".format(cls))
211211
return data
212212

213-
def to_json(self, filepath, pretty=False):
213+
def to_json(self, filepath, pretty=False, compact=False, minimal=False):
214214
"""Convert an object to its native data representation and save it to a JSON file.
215215
216216
Parameters
217217
----------
218218
filepath : str
219219
The path to the JSON file.
220220
pretty : bool, optional
221-
If True, the JSON file will be pretty printed.
222-
Defaults to False.
221+
If True, format the output with newlines and indentation.
222+
compact : bool, optional
223+
If True, format the output without any whitespace.
224+
minimal : bool, optional
225+
If True, exclude the GUID from the JSON output.
223226
224227
"""
225-
compas.json_dump(self, filepath, pretty=pretty)
228+
compas.json_dump(self, filepath, pretty=pretty, compact=compact, minimal=minimal)
226229

227230
@classmethod
228231
def from_jsonstring(cls, string): # type: (...) -> Data
@@ -249,22 +252,25 @@ def from_jsonstring(cls, string): # type: (...) -> Data
249252
raise TypeError("The data in the string is not a {}.".format(cls))
250253
return data
251254

252-
def to_jsonstring(self, pretty=False):
255+
def to_jsonstring(self, pretty=False, compact=False, minimal=False):
253256
"""Convert an object to its native data representation and save it to a JSON string.
254257
255258
Parameters
256259
----------
257260
pretty : bool, optional
258-
If True, the JSON string will be pretty printed.
259-
Defaults to False.
261+
If True, format the output with newlines and indentation.
262+
compact : bool, optional
263+
If True, format the output without any whitespace.
264+
minimal : bool, optional
265+
If True, exclude the GUID from the JSON output.
260266
261267
Returns
262268
-------
263269
str
264270
The JSON string.
265271
266272
"""
267-
return compas.json_dumps(self, pretty=pretty)
273+
return compas.json_dumps(self, pretty=pretty, compact=compact, minimal=minimal)
268274

269275
def copy(self, cls=None): # type: (...) -> D
270276
"""Make an independent copy of the data object.
@@ -284,7 +290,8 @@ def copy(self, cls=None): # type: (...) -> D
284290
if not cls:
285291
cls = type(self)
286292
obj = cls.__from_data__(deepcopy(self.__data__))
287-
obj.name = self.name
293+
if self._name is not None:
294+
obj._name = self._name
288295
return obj # type: ignore
289296

290297
def sha256(self, as_string=False):

tests/compas/data/test_copy.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from compas.data import Data
2+
3+
4+
class TestData(Data):
5+
@property
6+
def __data__(self):
7+
return {}
8+
9+
10+
def test_copy():
11+
data = TestData()
12+
13+
assert data.name == data.copy().name
14+
assert data.__dtype__ == data.copy().__dtype__
15+
16+
assert data.guid != data.copy().guid
17+
assert data.__jsondump__() != data.copy().__jsondump__()
18+
assert data.to_jsonstring() != data.copy().to_jsonstring()
19+
20+
assert data.__jsondump__(minimal=True) == data.copy().__jsondump__(minimal=True)
21+
assert data.to_jsonstring(minimal=True) == data.copy().to_jsonstring(minimal=True)

0 commit comments

Comments
 (0)