python-OBD icon indicating copy to clipboard operation
python-OBD copied to clipboard

Serialize OBDResponse to JSON

Open NaturesProphet opened this issue 4 years ago • 5 comments

Hello folks.

Please, i need some help to serialize OBDResponse objects to JSON, any idea about how can i do it? i'm new to python.

NaturesProphet avatar Mar 25 '21 20:03 NaturesProphet

Being more specific with your request will help you get help:

  • Do you need to serialize an arbitrary OBDResponse object to JSON, or just some specific ones you happen to be using?
  • What is the purpose of the serialization? Are you storing the objects - writing many at once, or streaming them to another service - handling one-by-one?
  • How much data do you need in the serialization? Python's pickle module will serialize entire objects, for example. Do you need that level of serialization in JSON? Or do you need only the measurement type and value for each reading?

ConnorNovak avatar Mar 29 '21 14:03 ConnorNovak

Hello Connor, thanks for reading this.

I want to serialize the entire and arbitrary OBDResponse object to json and use this json as a response on an flask rest endpoint that i am integrating with the obd. my objective is to serve a http rest api that will respond with OBDResponse as a json, and i want all the nested documents inside that object.

well, as i said, i'm new to python, my home language is javascript and it would be really easy to do such task on that language without write a logic for each field. i want something in javascript way if possible.

NaturesProphet avatar Mar 29 '21 17:03 NaturesProphet

My personal opinion: Your best bet would be creating a function that serializes a given OBDResponse object. Looking at the documentation for the object, it appears to have four salient attributes (you should probably include the units property as well, so make it 5): command, messages, value, time, and units. It looks, at first glance, like each of these can be cast to a base type. A single function should then suffice for any OBDResponse.

If you wanted to contribute to the library, you could make an as_json() method for OBDResponse, or something similar. You could also drop your function in utils.py. I'm not associated with this repository in any way, so I can't say what reception if would have.

Salient code points: https://github.com/brendan-w/python-OBD/blob/master/obd/obd.py line 298: Creation of OBDResponse object https://github.com/brendan-w/python-OBD/blob/master/obd/OBDCommand.py line 85: unpacking of response data into OBDResponse object

ConnorNovak avatar Mar 29 '21 22:03 ConnorNovak

Hi, I wrote a JSON encoder which works for most of the python-OBD types.

Check it out at https://github.com/bgunson/obd-socketio

To use it pip install obd-socketio

import obdio

obdio.dumps(...)
obdio.dump(...)  

Contribution is also welcome becuase its not fully implemented but does the job.

bgunson avatar Jul 03 '22 00:07 bgunson

awesome dude

NaturesProphet avatar Jul 03 '22 21:07 NaturesProphet