Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit03d9977

Browse files
feat: add a 'to_json()` method to the Gitlab Objects
Add a `to_json()` method that returns a JSON string representation ofthe object.
1 parentecc415f commit03d9977

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
importcopy
1919
importimportlib
20+
importjson
2021
importpprint
2122
importtextwrap
2223
fromdataclassesimportdataclass
@@ -151,6 +152,9 @@ def asdict(self) -> Dict[str, Any]:
151152
data.update(copy.deepcopy(self._updated_attrs))
152153
returndata
153154

155+
defto_json(self,**kwargs:Any)->str:
156+
returnjson.dumps(self.asdict(),**kwargs)
157+
154158
def__str__(self)->str:
155159
returnf"{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
assertresult== {"attr1": [1,2,3,10]}
300300
assertfake_object.attributes== {"attr1": [1,2,3]}
301+
302+
deftest_to_json(self,fake_manager):
303+
fake_object=FakeObject(fake_manager, {"attr1": [1,2,3]})
304+
assertfake_object.to_json()=='{"attr1": [1, 2, 3]}'

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp