Changeset - e14ae8437548
[Not reviewed]
beta
0 2 0
Marcin Kuzminski - 14 years ago 2012-03-21 20:54:30
marcin@python-works.com
extended JSON encoder to use __json__ method if available
2 files changed with 16 insertions and 3 deletions:
0 comments (0 inline, 0 general)
rhodecode/lib/compat.py
Show inline comments
 
@@ -34,7 +34,12 @@ from rhodecode import __platform__, PLAT
 
#==============================================================================
 

	
 

	
 
def __obj_dump(obj):
 
def _obj_dump(obj):
 
    """
 
    Custom function for dumping objects to JSON
 

	
 
    :param obj:
 
    """
 
    DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
 
    DATE_FORMAT = "%Y-%m-%d"
 
    if isinstance(obj, complex):
 
@@ -47,6 +52,11 @@ def __obj_dump(obj):
 
        return list(obj)
 
    elif isinstance(obj, OrderedDict):
 
        return obj.as_dict()
 
    elif hasattr(obj, '__json__'):
 
        if callable(obj.__json__):
 
            return obj.__json__()
 
        else:
 
            return obj.__json__
 
    else:
 
        raise NotImplementedError
 

	
 
@@ -57,7 +67,7 @@ try:
 
    class ExtendedEncoder(json.JSONEncoder):
 
        def default(self, obj):
 
            try:
 
                return __obj_dump(obj)
 
                return _obj_dump(obj)
 
            except NotImplementedError:
 
                pass
 
            return json.JSONEncoder.default(self, obj)
 
@@ -68,7 +78,7 @@ except ImportError:
 

	
 
    def extended_encode(obj):
 
        try:
 
            return __obj_dump(obj)
 
            return _obj_dump(obj)
 
        except NotImplementedError:
 
            pass
 
        raise TypeError("%r is not JSON serializable" % (obj,))
rhodecode/model/db.py
Show inline comments
 
@@ -381,6 +381,9 @@ class User(Base, BaseModel):
 

	
 
    def __json__(self):
 
        return dict(
 
            user_id=self.user_id,
 
            first_name=self.name,
 
            last_name=self.lastname,
 
            email=self.email,
 
            full_name=self.full_name,
 
            full_name_or_username=self.full_name_or_username,
0 comments (0 inline, 0 general)