On 05/15/13 09:15, Jesper Larsen wrote:
So what I intend to do is: Subclass JsonDocument. Override __init__ to take an 
optional callback function (default=None => behaviour as JsonDocument) and 
override create_out_string so that it optionally (if callback function is not 
None) wraps the JSON output in:

callback_function_name + '(' + json + ')'



Hi Jesper,

That's the idea, but I'd rather do:

from itertools import chain

class JsonP(JsonDocument):
    def __init__(self, callback_name, *args, **kwargs):
        super(JsonP, self).__init__(*args, **kwargs)
        self.callback_name = callback_name

    def create_out_string(self, ctx):
        super(JsonP, self).create_out_string(ctx)

ctx.out_string = chain([self.callback_name, '('], ctx.out_string, [')'])

... because:

1) out_string is always a sequence, but not necessarily a list. (With Json, as of now, it's always a list but that's an implementation quirk)
2) string concatanations are costly.
3) Using JsonP does not make sense (does it?) without callback_name so I made it mandatory.

If you like this implementation, I'll commit it and it will be out in Spyne-2.11. In case you don't want to wait (I got a lot to do for 2.11), you can use this as it is in your own code (If it works, the above is not tested) with 2.10.

Other things like making callback_name dynamic could also be made by trying to get it first from context (e.g. ctx.protocol.callback_name) before getting it from protocol-wide setting.

Best,
Burak

_______________________________________________
Soap mailing list
[email protected]
http://mail.python.org/mailman/listinfo/soap

Reply via email to