python-dbus-next icon indicating copy to clipboard operation
python-dbus-next copied to clipboard

Add a function to remove all dbus types

Open nicola-lunghi opened this issue 6 years ago • 4 comments

Hi,

I need to encode the result of a dbus message with json. How can I make the dbus object type serializable? for now I am doing (from your example client)

def jsonify_results(result):
    def conv_default(o):
        """handle the variant type for json.dumps"""
        if type(o) is Variant:
            return o.value
        else:
            raise json.JSONDecodeError()

    assert isinstance(result, Message)
    assert result.message_type is MessageType.METHOD_RETURN
    return json.dumps(result.body, default=conv_default)

There's a better way to get a serializable body without all the dbus types?

Thanks Nick

nicola-lunghi avatar Nov 29 '19 12:11 nicola-lunghi

That's the way to do it right now. I believe the only nonserializable type is the Variant. I would like to put a function that turns the result into a plain object because I think that's a common use case. I have implemented this in the test suite somewhere. I can probably just put that into the library.

acrisci avatar Nov 29 '19 16:11 acrisci

FWIW, json.dumps takes cls argument, allowing usage of a custom encoder. Maybe it's worth including such an encoder in the library.

Something similar to this?

import json
from dbus_next import Variant

class DBusEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Variant):
            return obj.value
        # Let the base class default method raise the TypeError
        return json.JSONEncoder.default(self, obj)


json.dumps(data, cls=DBusEncoder)

nicola-lunghi avatar Dec 13 '19 11:12 nicola-lunghi

It would be nice to have this in the dbus-next library.

tzahari avatar Nov 18 '21 08:11 tzahari