diff --git a/gitlab.py b/gitlab.py index e8b6765a8..85ec1c9a3 100644 --- a/gitlab.py +++ b/gitlab.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see . +import json import requests __title__ = 'python-gitlab' @@ -26,6 +27,14 @@ __copyright__ = 'Copyright 2013 Gauvain Pocentek' +class jsonEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, GitlabObject): + return obj.__dict__ + elif isinstance(obj, Gitlab): + return {'url': obj._url} + return json.JSONEncoder.default(self, obj) + class GitlabConnectionError(Exception): pass @@ -421,6 +430,9 @@ def __init__(self, gl, data=None, **kwargs): def __str__(self): return '%s => %s' % (type(self), str(self.__dict__)) + def json(self): + return json.dumps(self.__dict__, cls=jsonEncoder) + class User(GitlabObject): _url = '/users'