JsonWeb icon indicating copy to clipboard operation
JsonWeb copied to clipboard

Stacking to_object and from_object decorators.

Open boris317 opened this issue 13 years ago • 1 comments

This kind of bothers me


@to_object()
@from_object()
class Foo(object):
    pass

The decorators are separate because each accepts arguments that modify the encode/decode behavior of the object. Stil, there is something about the above code I dislike. So how about something like this


@jsonable
class Foo(object):
    pass

jsonable accepts no arguments. If you want to pass arguments then you must still use from_object and to_object. Essentially jsonable becomes a short cut for the first example. Its implementation being quite simple

def jsonable(cls):
    to_object()(cls)
    from_object()(cls)

Is this use case common enough to warrant the additional of jsonable?

boris317 avatar Sep 11 '12 05:09 boris317

:+1: I think that in complex projects when you have a lot of DB models, it would be useful to put only jsonable instead of @to_object/@from_object.

20 lines of @to_object/@from_object are now 10 lines of jsonable, more readable and more dev-friendly, imo.

RaitoBezarius avatar Mar 20 '15 18:03 RaitoBezarius