Replacing json with jsonpickle

#1

Hello Tobias and others,

Recently, I’ve been facing an interesting problem that I suspect has affected others and I wanted to bring it up to the group to see if my solution might be incorporated into the main branch of Autobhan.

We’re using Autobahn in conjunction with Django as an application server. We have an application with many custom classes and a few Django classes (such as Models), all of which are designed to be returned to a requesting user either over RPC or as a brokered response in a Pub/Sub channel.

The largest issue we’ve had to face with this approach has been in passing these custom instances into Autobahn and having it throw exceptions because the underlying JSON serializer can’t understand classes which aren’t inherited from basic Python types like ‘dict’, ‘str’, etc.

What I’ve done to make this much easier is to replace the serializer inside Autobhan serializer with the one provided by the JSON-pickle project [https://github.com/jsonpickle/jsonpickle].

This serializer handles custom classes with ease and performs extremely well. All serializer errors have vanished.

So, my question is this:

Is there interest in having Autobahn support the option to use different JSON serializers? I’m happy to produce a basic patch for this functionality but wanted to gauge the interest level and get feedback from Tobias.

Thanks all,

-Mike Place

mike....@wearetopsecret.com

0 Likes

#2

Mike,

sure, you can do that today by overriding _serialize/_unserialize that is mixed in from WampFactory:

https://github.com/tavendo/AutobahnPython/blob/master/autobahn/autobahn/wamp.py#L456

- Tobias

···

Am 27.02.2013 01:01, schrieb mike....@wearetopsecret.com:

Hello Tobias and others,

Recently, I've been facing an interesting problem that I suspect has
affected others and I wanted to bring it up to the group to see if my
solution might be incorporated into the main branch of Autobhan.

We're using Autobahn in conjunction with Django as an application
server. We have an application with many custom classes and a few Django
classes (such as Models), all of which are designed to be returned to a
requesting user either over RPC or as a brokered response in a Pub/Sub
channel.

The largest issue we've had to face with this approach has been in
passing these custom instances into Autobahn and having it throw
exceptions because the underlying JSON serializer can't understand
classes which aren't inherited from basic Python types like 'dict',
'str', etc.

What I've done to make this much easier is to replace the serializer
inside Autobhan serializer with the one provided by the JSON-pickle
project [https://github.com/jsonpickle/jsonpickle].

This serializer handles custom classes with ease and performs extremely
well. All serializer errors have vanished.

So, my question is this:

Is there interest in having Autobahn support the option to use different
JSON serializers? I'm happy to produce a basic patch for this
functionality but wanted to gauge the interest level and get feedback
from Tobias.

Thanks all,

-Mike Place
mike....@wearetopsecret.com

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Likes

#3

Hi,

I have similar problem, in which I am sending a class object over wamp protocol.

the class looks like this,

Message(object):

	self.msgId=msgId 

	self.reqId = reqId

	self.msgType=msgType

	self.flag = flag

	self.priority = priority

	self.messageData = messageData

And I am trying to send it with,

wampclient.publish(tRI, message)

here wampclient is object of

class WampClient(WampClientProtocol):

But I get error as,

invalid type for parameter ‘event’ - not JSON serializable

I tried overriding, _serializable and _unserializable in above WampClient class with,

def _serialize(self, obj):

return cPickle.dumps(obj)

def _unserialize(self, obj):

return cPickle.loads(obj)

Still no luck, I get the same JSON error.

I even tries setting encoder with,

class WampJSonEncoder(JSONEncoder):

def default(self, obj):

return obj.dict

Any help will be deeply appreciated.

As I am new to Python as well, I might be wrong with some basic fundamentals of Python.

Thanks,

Rahul

···

On Wednesday, February 27, 2013 2:00:08 PM UTC+5:30, Tobias Oberstein wrote:

Mike,

sure, you can do that today by overriding _serialize/_unserialize that
is mixed in from WampFactory:

https://github.com/tavendo/AutobahnPython/blob/master/autobahn/autobahn/wamp.py#L456

  • Tobias

Am 27.02.2013 01:01, schrieb mike...@wearetopsecret.com:

Hello Tobias and others,

Recently, I’ve been facing an interesting problem that I suspect has

affected others and I wanted to bring it up to the group to see if my

solution might be incorporated into the main branch of Autobhan.

We’re using Autobahn in conjunction with Django as an application

server. We have an application with many custom classes and a few Django

classes (such as Models), all of which are designed to be returned to a

requesting user either over RPC or as a brokered response in a Pub/Sub

channel.

The largest issue we’ve had to face with this approach has been in

passing these custom instances into Autobahn and having it throw

exceptions because the underlying JSON serializer can’t understand

classes which aren’t inherited from basic Python types like ‘dict’,

‘str’, etc.

What I’ve done to make this much easier is to replace the serializer

inside Autobhan serializer with the one provided by the JSON-pickle

project [https://github.com/jsonpickle/jsonpickle].

This serializer handles custom classes with ease and performs extremely

well. All serializer errors have vanished.

So, my question is this:

Is there interest in having Autobahn support the option to use different

JSON serializers? I’m happy to produce a basic patch for this

functionality but wanted to gauge the interest level and get feedback

from Tobias.

Thanks all,

-Mike Place

mike...@wearetopsecret.com

You received this message because you are subscribed to the Google

Groups “Autobahn” group.

To unsubscribe from this group and stop receiving emails from it, send

an email to autobahnws+...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

0 Likes

#4

There is (not yet official) support for overriding JSON serializer/unserializer .. override:

https://github.com/tavendo/AutobahnPython/blob/master/autobahn/autobahn/wamp.py#L492

I've added an issue for providing official support:
https://github.com/tavendo/AutobahnPython/issues/163

/Tobias

···

Am 21.10.2013 10:33, schrieb rahul....@gslab.com:

Hi,

I have similar problem, in which I am sending a class object over wamp
protocol.
the class looks like this,
             Message(object):
self.msgId=msgId
self.reqId = reqId
self.msgType=msgType
self.flag = flag
self.priority = priority
self.messageData = messageData

And I am trying to send it with,
              wampclient.publish(tRI, message)
here wampclient is object of
              class WampClient(WampClientProtocol):

But I get error as,
      invalid type for parameter 'event' - not JSON serializable

I tried overriding, _serializable and _unserializable in above
WampClient class with,
    def _serialize(self, obj):
       return cPickle.dumps(obj)
    def _unserialize(self, obj):
       return cPickle.loads(obj)

Still no luck, I get the same JSON error.

I even tries setting encoder with,
  class WampJSonEncoder(JSONEncoder):
     def default(self, obj):
        return obj.__dict__

Any help will be deeply appreciated.
As I am new to Python as well, I might be wrong with some basic
fundamentals of Python.

Thanks,
Rahul

On Wednesday, February 27, 2013 2:00:08 PM UTC+5:30, Tobias Oberstein wrote:

    Mike,

    sure, you can do that today by overriding _serialize/_unserialize that
    is mixed in from WampFactory:

    https://github.com/tavendo/AutobahnPython/blob/master/autobahn/autobahn/wamp.py#L456
    <https://github.com/tavendo/AutobahnPython/blob/master/autobahn/autobahn/wamp.py#L456>

    - Tobias

    Am 27.02.2013 01:01, schrieb mike...@wearetopsecret.com <javascript:>:
     > Hello Tobias and others,
     >
     > Recently, I've been facing an interesting problem that I suspect has
     > affected others and I wanted to bring it up to the group to see
    if my
     > solution might be incorporated into the main branch of Autobhan.
     >
     > We're using Autobahn in conjunction with Django as an application
     > server. We have an application with many custom classes and a few
    Django
     > classes (such as Models), all of which are designed to be
    returned to a
     > requesting user either over RPC or as a brokered response in a
    Pub/Sub
     > channel.
     >
     > The largest issue we've had to face with this approach has been in
     > passing these custom instances into Autobahn and having it throw
     > exceptions because the underlying JSON serializer can't understand
     > classes which aren't inherited from basic Python types like 'dict',
     > 'str', etc.
     >
     > What I've done to make this much easier is to replace the serializer
     > inside Autobhan serializer with the one provided by the JSON-pickle
     > project [https://github.com/jsonpickle/jsonpickle
    <https://github.com/jsonpickle/jsonpickle>].
     >
     > This serializer handles custom classes with ease and performs
    extremely
     > well. All serializer errors have vanished.
     >
     > So, my question is this:
     >
     > Is there interest in having Autobahn support the option to use
    different
     > JSON serializers? I'm happy to produce a basic patch for this
     > functionality but wanted to gauge the interest level and get
    feedback
     > from Tobias.
     >
     > Thanks all,
     >
     > -Mike Place
     > mike...@wearetopsecret.com <javascript:>
     >
     > --
     > You received this message because you are subscribed to the Google
     > Groups "Autobahn" group.
     > To unsubscribe from this group and stop receiving emails from it,
    send
     > an email to autobah...@googlegroups.com <javascript:>.
     > For more options, visit https://groups.google.com/groups/opt_out
    <https://groups.google.com/groups/opt_out>.
     >

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Likes