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

Commit4a64a56

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 parent3b95671 commit4a64a56

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

‎docs/api-usage.rst‎

Lines changed: 9 additions & 5 deletions
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‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
importcopy
1919
importimportlib
20+
importjson
2021
importpprint
2122
importtextwrap
2223
fromtypesimportModuleType
@@ -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-
defasdict(self)->Dict[str,Any]:
147+
defasdict(self,with_parent_attrs:bool=False)->Dict[str,Any]:
147148
data= {}
149+
ifwith_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
returndata
151154

155+
defto_json(self,**kwargs:Any)->str:
156+
returnjson.dumps(self.asdict(),**kwargs)
157+
152158
def__str__(self)->str:
153159
returnf"{type(self)} =>{self.asdict()}"
154160

‎tests/unit/test_base.py‎

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp