Ok. The Array 'class' is changed in pyjslib. So, Array and $wnd.Array
differ.
Change isArray in pyjslib and see what happens. Change is to:
def isArray(a):
JS("""
return @{{isObject}}(@{{a}}) && (@{{a}}['constructor'] === Array ||
@{{a}}['constructor'] === $wnd['Array']);
""")
On Fri, Jun 28, 2013 at 8:54 AM, Kees Bos <[email protected]> wrote:
> On Tue, 2013-06-25 at 10:09 +0100, Apexi 200sx wrote:
> > Hi,
> >
> >
> > might be one for Kees or C Anthony
> >
> >
> >
> > I want to use the msgpack (serialization format) JS library from
> > pyjs/pyjamas
> >
> >
> > I am tried to use builtin conversion routines
> >
> >
> > """my_msgpack module"""
> >
> > from __pyjamas__ import JS
> > from pyjslib import toJSObjects, isArray, isObject
> > import json
> >
> > parser = json.JSONParser()
> > js_to_py = getattr(parser, 'jsObjectToPyObject')
> >
> >
> > def pack(obj):
> > js_obj = toJSObjects(obj)
> > print js_obj
>
> What you get here, is an array. Try this:
> is_array = JS("(js_obj instanceof Array? true : false)")
> print "pack js_obj instanceof Array:", is_array
>
>
> > JS("""
> > return $wnd.msgpack.pack(@{{js_obj}});
> > """)
> >
> > def unpack_to_js(packed_js):
> > JS("""
> > tmp = $wnd.msgpack.unpack(@{{packed_js}});
> > console.log(@{{isArray}}(tmp));
> > console.log(@{{isObject}}(tmp));
> > console.log(tmp[0]);
> > return tmp;
> > """)
> >
> > def unpack(packed):
> > js_obj = unpack_to_js(packed)
>
> What you get here, looks like an array, but isn't. Try this:
> is_array = JS("(js_obj instanceof Array? true : false)")
> print "pack js_obj instanceof Array:", is_array
>
> > print js_obj
> > py_obj = js_to_py(js_obj)
> > return py_obj
>
>
> My guess is that something weird happening in msgpack.unpack. The js
> object to be packed is an instance of Array, while the unpacked js
> object is not an instance of Array.
>
>
> > then from another module i do...
> >
> >
> > list_dat = [[1,2,3,4], ["a","b","c","d"]]
> >
> >
> > packed = my_msgpack.pack(list_dat)
> >
> > my_msgpack.unpack(packed)
> >
> >
> > results in...
> >
> > {'0': {'0': 1, '1': 2, '2': 3, '3': 4}, '1': {'0': 'a', '1': 'b', '2':
> > 'c', '3': 'd'}}
> >
> >
> > from the above unpack_to_js function...
> >
> > console.log(@{{isArray}}(tmp)); results in false
> > console.log(@{{isObject}}(tmp)); results in true
> > console.log(tmp[0]); results in [1,2,3,4]
> >
> >
> > so it seems toJSObjects is constructing the correct javascript
> > objects/types and msgpack is able to convert to/from msgpack format
> > fine but I cannot turn the resulting JS into the correct python types.
> >
> >
> > Note also that I have tried...
> > js_to_py = getattr(parser, 'jsObjectToPyObject')
> > and
> > js_to_py = getattr(parser, 'jsObjectToPy')
> >
> >
> >
> >
> > attached is the msgpack.js lib
> >
> > --
> >
> > ---
> > You received this message because you are subscribed to the Google
> > Groups "Pyjs.org Users" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> > an email to [email protected].
> > For more options, visit https://groups.google.com/groups/opt_out.
> >
> >
>
>
>
--
---
You received this message because you are subscribed to the Google Groups
"Pyjs.org Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.