File tree Expand file tree Collapse file tree 3 files changed +17
-5
lines changed Expand file tree Collapse file tree 3 files changed +17
-5
lines changed Original file line number Diff line number Diff line change @@ -193,9 +193,7 @@ You can print a Gitlab Object. For example:
193
193
print (project.pformat())
194
194
195
195
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.
199
197
200
198
* `asdict() ` method. Returns a dictionary representation of the Gitlab object.
201
199
* `attributes ` property. Returns a dictionary representation of the Gitlab object.
@@ -213,13 +211,19 @@ dictionary representation of the Gitlab Object.
213
211
214
212
project = gl.projects.get(1 )
215
213
project_dict = project.asdict()
216
- # Do a JSON dump of the object
217
- print (json.dumps(project.asdict()))
218
214
219
215
# Or a dictionary representation also containing some of the parent attributes
220
216
issue = project.issues.get(1 )
221
217
attribute_dict = issue.attributes
222
218
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 ))
223
227
224
228
Base types
225
229
==========
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 dataclasses import dataclass
@@ -151,6 +152,9 @@ def asdict(self) -> Dict[str, Any]:
151
152
data .update (copy .deepcopy (self ._updated_attrs ))
152
153
return data
153
154
155
+ def to_json (self , ** kwargs : Any ) -> str :
156
+ return json .dumps (self .asdict (), ** kwargs )
157
+
154
158
def __str__ (self ) -> str :
155
159
return f"{ type (self )} => { self .asdict ()} "
156
160
Original file line number Diff line number Diff line change @@ -298,3 +298,7 @@ def test_attributes(self, fake_manager):
298
298
result ["attr1" ].append (10 )
299
299
assert result == {"attr1" : [1 , 2 , 3 , 10 ]}
300
300
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]}'
You can’t perform that action at this time.
0 commit comments