Skip to content

Commit 8d65870

Browse files
author
Gauvain Pocentek
committed
add a json() method to GitlabObject's
1 parent 1625e55 commit 8d65870

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

gitlab.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import json
1920
import requests
2021

2122
__title__ = 'python-gitlab'
@@ -26,6 +27,14 @@
2627
__copyright__ = 'Copyright 2013 Gauvain Pocentek'
2728

2829

30+
class jsonEncoder(json.JSONEncoder):
31+
def default(self, obj):
32+
if isinstance(obj, GitlabObject):
33+
return obj.__dict__
34+
elif isinstance(obj, Gitlab):
35+
return {'url': obj._url}
36+
return json.JSONEncoder.default(self, obj)
37+
2938
class GitlabConnectionError(Exception):
3039
pass
3140

@@ -421,6 +430,9 @@ def __init__(self, gl, data=None, **kwargs):
421430
def __str__(self):
422431
return '%s => %s' % (type(self), str(self.__dict__))
423432

433+
def json(self):
434+
return json.dumps(self.__dict__, cls=jsonEncoder)
435+
424436

425437
class User(GitlabObject):
426438
_url = '/users'

0 commit comments

Comments
 (0)