Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #639 (closed defect: wontfix)

Opened 13 years ago

Last modified 12 years ago

[PATCH] Prevent conversion to JSON format from raising NoApplicableMethods exception

Reported by: bjourne Owned by: anonymous
Priority: normal Milestone:
Component: TurboGears Version:
Severity: normal Keywords:
Cc: simon

Description

You have an exposed method like this:

@expose(format = "json")
def foo(self, obj, func, param):
    theobj = getattr(module, obj)
    func2call = getattr(theobj, func)
    somevar = func2call(param)
    return somevar

In case somevar is not a type that is convertable to JSON a NoApplicableMethods? exception is raised. I have never found a use for that functionality so I think it would be nicer if repr(somevar) would be returned instead. It would be useful for cases such as in the above example.

Attachments

jsonify-repr-by-default.patch Download (760 bytes) - added by bjourne 13 years ago.

Change History

Changed 13 years ago by bjourne

comment:1 Changed 13 years ago by michele

  • Cc simon added

comment:2 Changed 13 years ago by godoy

  • Summary changed from [PATH] Prevent conversion to JSON format from raising NoApplicableMethods exception to [PATCH] Prevent conversion to JSON format from raising NoApplicableMethods exception

I prefer the exception. Why? Because then I know exactly what will be returned, without any "magic". If I get the exception, then I'll have to rework the code so that it is consistent and works correctly; if magic happens I might get something that is not what I was expecting and have no clue to where things have gone wrong.

IMHO, Python's "explicit is better than implicit" should be followed here.

comment:3 Changed 13 years ago by simon

I agree.

We should however document this behaviour along with instructions on how to use strategy.default to create a fail-safe default if desired.

comment:4 Changed 13 years ago by bjourne

The "magic" comes from source:trunk/turbogears/jsonify.py. This patch merely tries to make the automagic coercion to JSON parsable types more consistent. The current code coerces SQLObject instances, primitive data types and iterables. Makes sense to me that it should make a best effort attempt to convert all types thrown at it. And IMHO "document behaviour, add instructions on how to change it" is a clear sign something's wrong. :)

comment:5 Changed 13 years ago by simon

I honestly do not see how getting

'<__main__.MyClass object at 0x00BBB730>'

could be usefull.

comment:6 Changed 13 years ago by bjourne

If you do a JSON request, I'd prefer if an item with <__main__.MyClass object at 0x00BBB730> would be returned instead of an exception raised and nothing at all being returned. But maybe having the default be str(obj) instead of repr(obj) would be more useful?

comment:7 Changed 13 years ago by simon

  • Status changed from new to closed
  • Resolution set to wontfix

To make it actually usable one would still have to implement some form of serialisation, which incidentally is exactly what one needs to do now.

I am not convinced.

Note: See TracTickets for help on using tickets.