pieper / Chronicle

An ordered record of events and data
Other
7 stars 6 forks source link

Keep getting MultiValue not JSON serializable errors #7

Open bbearce opened 4 years ago

bbearce commented 4 years ago

Hi,

I'm getting this error when I try to upload my dicom images to couchdb: "TypeError: Object of type MultiValue is not JSON serializable" image

What I've done:

Offending line: couchdb.json.encode(value).encode('utf-8')

My first try at fixing this was to use this advice for casting a MultiValue object to a list: https://bitbucket.org/openrem/openrem/issues/803 Example: self.modalities_in_study = json.dumps(x)

Into

self.modalities_in_study = json.dumps(list(x))

Now in this example it was (approx. line: 100 inside a try block):

if isinstance(value, pydicom.multival.MultiValue):
    couchdb.json.encode(list(value)).encode('utf-8')
else:
    couchdb.json.encode(value).encode('utf-8')

This works for a bit but feels hacky. It works until we get to line ~262: doc_id, doc_rev = self.db.save(document)

Then I get the error again: image

I've even printed the data to be saved as a document and it's a simple dictionary: image ...

I don't feel like I should have to edit "couchdb", "json" or "pydicom" to get this to work. Is there something with record.py that I"m configuring wrong? Could it be our data?

pieper commented 4 years ago

Hi -

Thanks for reporting 👍

These are probably issues related to python3 and pydicom 1.x. As I recall the code originally was written against python2.x and pydicom 0.9.x, so maybe one easy workaround would be to install these and try again (they should be pretty easy to find and specify in the paths).

Beyond that, it would be better to fix this code to use the up-to-date packages. The json conversion code from chronicle was actually extracted from here and contributed to the upstream pydicom repository (see https://github.com/pydicom/pydicom/pull/862) and that pull request is where a lot of the porting took places.

So probably the best solution overall would be to find a way to switch over to using the new pydicom json converters as described here: https://pydicom.github.io/pydicom/stable/tutorials/dicom_json.html