Skip to content

Commit 4a64a56

Browse files
feat: add a 'to_json()` method to the Gitlab Objects
Add a `to_json()` method that returns a JSON string representation of the object.
1 parent 3b95671 commit 4a64a56

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

docs/api-usage.rst

+9-5
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ the value on the object is accepted:
215215
issue.save()
216216
217217
You can get a dictionary representation copy of the Gitlab Object. Modifications made to
218-
the dictionary will have no impact on the GitLab Object. This can also be used
219-
to create a JSON representation of the object. There are two ways to retrieve a
220-
dictionary representation of the Gitlab Object.
218+
the dictionary will have no impact on the GitLab Object.
221219

222220
* `asdict()` method. Returns a dictionary representation of the Gitlab object.
223221
* `attributes` property. Returns a dictionary representation of the Gitlab object.
@@ -235,13 +233,19 @@ dictionary representation of the Gitlab Object.
235233
236234
project = gl.projects.get(1)
237235
project_dict = project.asdict()
238-
# Do a JSON dump of the object
239-
print(json.dumps(project.asdict()))
240236
241237
# Or a dictionary representation also containing some of the parent attributes
242238
issue = project.issues.get(1)
243239
attribute_dict = issue.attributes
244240
241+
You can get a JSON string represenation of the Gitlab Object. For example:
242+
243+
.. code-block:: python
244+
245+
project = gl.projects.get(1)
246+
print(project.to_json())
247+
# Use arguments supported by `json.dump()`
248+
print(project.to_json(sort_keys=True, indent=4))
245249
246250
Base types
247251
==========

gitlab/base.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import copy
1919
import importlib
20+
import json
2021
import pprint
2122
import textwrap
2223
from types import ModuleType
@@ -143,12 +144,17 @@ def __getattr__(self, name: str) -> Any:
143144
def __setattr__(self, name: str, value: Any) -> None:
144145
self.__dict__["_updated_attrs"][name] = value
145146

146-
def asdict(self) -> Dict[str, Any]:
147+
def asdict(self, with_parent_attrs: bool = False) -> Dict[str, Any]:
147148
data = {}
149+
if with_parent_attrs:
150+
data["_parent_attrs"] = copy.deepcopy(self._parent_attrs)
148151
data.update(copy.deepcopy(self._attrs))
149152
data.update(copy.deepcopy(self._updated_attrs))
150153
return data
151154

155+
def to_json(self, **kwargs: Any) -> str:
156+
return json.dumps(self.asdict(), **kwargs)
157+
152158
def __str__(self) -> str:
153159
return f"{type(self)} => {self.asdict()}"
154160

tests/unit/test_base.py

+4
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,7 @@ def test_attributes(self, fake_manager):
355355
result["attr1"].append(10)
356356
assert result == {"attr1": [1, 2, 3, 10]}
357357
assert fake_object.attributes == {"attr1": [1, 2, 3]}
358+
359+
def test_to_json(self, fake_manager):
360+
fake_object = FakeObject(fake_manager, {"attr1": [1, 2, 3]})
361+
assert fake_object.to_json() == '{"attr1": [1, 2, 3]}'

0 commit comments

Comments
 (0)