To simplify it part a) of my previous email:

def tester(d):
>   Window.alert("YAY")
>     inst = 'yay'
>     return inst


from json import loads

bar = loads('["foo", {"bar":["baz", null, 1.0, 2]}]',
> object_hook=tester) self.mainPanel.add(HTML(bar))


On the browser:

> ['foo', {'bar': ['baz', None, 1, 2]}]


It looks like loads works, but the object_hook parameter does not. I can
see the tester function being mapped in the browser debugger. But I am not
sure where it breaks. its kind of hard to follow whats going on. Just
wondering if we are going down the right path here..

On Tue, Feb 28, 2012 at 1:32 PM, Rahul Rajan <[email protected]> wrote:
>
> Basically we are having trouble decoding a JSON-encoded custom python
object..
>
> a) We tried using python's json.loads but the browser can't find that
function. I'm guessing that is because the pyjamas compiler has trouble
converting the native json library to javascript?
> Would it be possible to use the native json loads function somehow?
>
> b) We looked at the dynamictable example. Does the automatic decoding
happens inside here?
>>
>> def onRemoteResponse(self, response, requestInfo):
>>
>>      people = response
>
>
> ______________________________________
>
> This is what our server sends:
>>
>> treeJSON = '[{"__jsonclass__": "Candidate.Candidate", "name": "Barack
Obama"}]'
>>
>> self.mainPanel.add(HTML('pleasework2 %s' %
JSONResponseTextHandler(response)))
>>
>> return treeJSON
>
>
> client's onRemoteResponse:
>>
>> from pyjamas.JSONService import JSONResponseTextHandler
>>
>> from Candidate import Candidate
>>
>> def onRemoteResponse(self, response, request_info):
>>         self.mainPanel.add(HTML('pleasework: %s' % response))
>>
>>         self.mainPanel.add(HTML('pleasework2 %s' %
JSONResponseTextHandler(response)))
>
>
> What we see in the browser:
>>
>> pleasework: [{"__jsonclass__": "Candidate.Candidate", "name": "Barack
Obama"}]
>>
>> pleasework2: instance of pyjamas.JSONService.JSONResponseTextHandler
>
>
> What we want to see: instance of Candidate.Candidate.
>
> We would really appreciate your advice...
>
> On Tue, Feb 28, 2012 at 3:54 AM, lkcl luke <[email protected]>
wrote:
> >
> > On Tue, Feb 28, 2012 at 11:46 AM, Rahul Rajan <[email protected]>
wrote:
> > > Hello,
> > >
> > > I am a newbie. I am having a lot of fun with pyjamas -- thanks for the
> > > awesome project :)
> >
> >  :)
> >
> > > I have a couple of questions:
> > > I am trying to send an object from the server to the client using
JSON.
> >
> >  then
> > > Server: json.dumps(tree, default=convert_to_builtin_type)
> > > Client: json.loads(response, object_hook=dict_to_object)
> > >
> > > But it looks like 'jsObjectToPyObject' in pyjs/json does not actually
take
> > > in an object_hook argument? Is that right?
> >
> >  it's automatic [in both pyjd and pyjs].  what you have to do is to
> > make sure that the classes exist and are imported into the
> > application.
> >
> >  take a look at the dynamictable example.  pay attention to line 87 of
> > public/SchoolCalendarService.php and the fact that there exists a
> > module in the pyjs application named "Professor" with a class named
> > "Professor" in it.
> >
> > l.
>

Reply via email to