File tree 3 files changed +20
-6
lines changed
3 files changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -215,9 +215,7 @@ the value on the object is accepted:
215
215
issue.save()
216
216
217
217
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.
221
219
222
220
* `asdict() ` method. Returns a dictionary representation of the Gitlab object.
223
221
* `attributes ` property. Returns a dictionary representation of the Gitlab object.
@@ -235,13 +233,19 @@ dictionary representation of the Gitlab Object.
235
233
236
234
project = gl.projects.get(1 )
237
235
project_dict = project.asdict()
238
- # Do a JSON dump of the object
239
- print (json.dumps(project.asdict()))
240
236
241
237
# Or a dictionary representation also containing some of the parent attributes
242
238
issue = project.issues.get(1 )
243
239
attribute_dict = issue.attributes
244
240
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 ))
245
249
246
250
Base types
247
251
==========
Original file line number Diff line number Diff line change 17
17
18
18
import copy
19
19
import importlib
20
+ import json
20
21
import pprint
21
22
import textwrap
22
23
from types import ModuleType
@@ -143,12 +144,17 @@ def __getattr__(self, name: str) -> Any:
143
144
def __setattr__ (self , name : str , value : Any ) -> None :
144
145
self .__dict__ ["_updated_attrs" ][name ] = value
145
146
146
- def asdict (self ) -> Dict [str , Any ]:
147
+ def asdict (self , with_parent_attrs : bool = False ) -> Dict [str , Any ]:
147
148
data = {}
149
+ if with_parent_attrs :
150
+ data ["_parent_attrs" ] = copy .deepcopy (self ._parent_attrs )
148
151
data .update (copy .deepcopy (self ._attrs ))
149
152
data .update (copy .deepcopy (self ._updated_attrs ))
150
153
return data
151
154
155
+ def to_json (self , ** kwargs : Any ) -> str :
156
+ return json .dumps (self .asdict (), ** kwargs )
157
+
152
158
def __str__ (self ) -> str :
153
159
return f"{ type (self )} => { self .asdict ()} "
154
160
Original file line number Diff line number Diff line change @@ -355,3 +355,7 @@ def test_attributes(self, fake_manager):
355
355
result ["attr1" ].append (10 )
356
356
assert result == {"attr1" : [1 , 2 , 3 , 10 ]}
357
357
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]}'
You can’t perform that action at this time.
0 commit comments