Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 03d9977

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 ecc415f commit 03d9977

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

docs/api-usage.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,7 @@ You can print a Gitlab Object. For example:
193193
print(project.pformat())
194194
195195
You can get a dictionary representation copy of the Gitlab Object. Modifications made to
196-
the dictionary will have no impact on the GitLab Object. This can also be used
197-
to create a JSON representation of the object. There are two ways to retrieve a
198-
dictionary representation of the Gitlab Object.
196+
the dictionary will have no impact on the GitLab Object.
199197

200198
* `asdict()` method. Returns a dictionary representation of the Gitlab object.
201199
* `attributes` property. Returns a dictionary representation of the Gitlab object.
@@ -213,13 +211,19 @@ dictionary representation of the Gitlab Object.
213211
214212
project = gl.projects.get(1)
215213
project_dict = project.asdict()
216-
# Do a JSON dump of the object
217-
print(json.dumps(project.asdict()))
218214
219215
# Or a dictionary representation also containing some of the parent attributes
220216
issue = project.issues.get(1)
221217
attribute_dict = issue.attributes
222218
219+
You can get a JSON string represenation of the Gitlab Object. For example:
220+
221+
.. code-block:: python
222+
223+
project = gl.projects.get(1)
224+
print(project.to_json())
225+
# Use arguments supported by `json.dump()`
226+
print(project.to_json(sort_keys=True, indent=4))
223227
224228
Base types
225229
==========

gitlab/base.py

Lines changed: 4 additions & 0 deletions
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 dataclasses import dataclass
@@ -151,6 +152,9 @@ def asdict(self) -> Dict[str, Any]:
151152
data.update(copy.deepcopy(self._updated_attrs))
152153
return data
153154

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

tests/unit/test_base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,7 @@ def test_attributes(self, fake_manager):
298298
result["attr1"].append(10)
299299
assert result == {"attr1": [1, 2, 3, 10]}
300300
assert fake_object.attributes == {"attr1": [1, 2, 3]}
301+
302+
def test_to_json(self, fake_manager):
303+
fake_object = FakeObject(fake_manager, {"attr1": [1, 2, 3]})
304+
assert fake_object.to_json() == '{"attr1": [1, 2, 3]}'

0 commit comments

Comments
 (0)