diff options
| author | John L. Villalovos <john@sodarock.com> | 2022-07-20 08:38:43 -0700 |
|---|---|---|
| committer | John L. Villalovos <john@sodarock.com> | 2022-07-20 08:38:43 -0700 |
| commit | 08ac071abcbc28af04c0fa655576e25edbdaa4e2 (patch) | |
| tree | 1867dc97f539247ac70018f43894d28d1add0190 /docs/api-usage.rst | |
| parent | e5affc8749797293c1373c6af96334f194875038 (diff) | |
| download | gitlab-08ac071abcbc28af04c0fa655576e25edbdaa4e2.tar.gz | |
feat: add `asdict()` and `to_json()` methods to Gitlab Objects
Add an `asdict()` method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.
The `asdict()` method name was chosen as both the `dataclasses` and
`attrs` libraries have an `asdict()` function which has the similar
purpose of creating a dictionary represenation of an object.
Also add a `to_json()` method that returns a JSON string
representation of the object.
Closes: #1116
Diffstat (limited to 'docs/api-usage.rst')
| -rw-r--r-- | docs/api-usage.rst | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/docs/api-usage.rst b/docs/api-usage.rst index 000633f..24d0890 100644 --- a/docs/api-usage.rst +++ b/docs/api-usage.rst @@ -214,6 +214,38 @@ the value on the object is accepted: issue.my_super_awesome_feature_flag = "random_value" issue.save() +You can get a dictionary representation copy of the Gitlab Object. Modifications made to +the dictionary will have no impact on the GitLab Object. + + * `asdict()` method. Returns a dictionary representation of the Gitlab object. + * `attributes` property. Returns a dictionary representation of the Gitlab + object. Also returns any relevant parent object attributes. + +.. note:: + + `attributes` returns the parent object attributes that are defined in + `object._from_parent_attrs`. What this can mean is that for example a `ProjectIssue` + object will have a `project_id` key in the dictionary returned from `attributes` but + `asdict()` will not. + + +.. code-block:: python + + project = gl.projects.get(1) + project_dict = project.asdict() + + # Or a dictionary representation also containing some of the parent attributes + issue = project.issues.get(1) + attribute_dict = issue.attributes + +You can get a JSON string represenation of the Gitlab Object. For example: + +.. code-block:: python + + project = gl.projects.get(1) + print(project.to_json()) + # Use arguments supported by `json.dump()` + print(project.to_json(sort_keys=True, indent=4)) Base types ========== |
