0

I am having an issue when trying to convert a class object to JSON format. Actually, I have an ECG class object and my expectation is to convert that object to a string in JSON format.

Ex: { "Source": "MIT", "FileName": "100", "Channel": 2, "Record": 11520000, "Time": 1800, "SampleRate": 500 }

ECGModel.py

class ECG(object):
@classmethod
def __init__(self, source, fileName, channel, record, time, sampleRate, ecg):
    self.Source = source
    self.FileName = fileName
    self.Channel = channel
    self.Record = record
    self.Time = time
    self.SampleRate = sampleRate
    self.ECG = ecg

# getting the values
@property
def value(self):
    print('Getting value')
    return self.Source, self.FileName, self.Channel, self.Record, self.Time, self.SampleRate, self.ECG

# setting the values
@value.setter
def value(self, source, fileName, channel, record, time, sampleRate, ecg):
    print('Setting value to ' + source)
    self.Source = source
    self.FileName = fileName
    self.Channel = channel
    self.Record = record
    self.Time = time
    self.SampleRate = sampleRate
    self.ECG = ecg

# deleting the values
@value.deleter
def value(self):
    print('Deleting value')
    del self.Source, self.Source, self.FileName, self.Channel, self.Record, self.Time, self.SampleRate, self.ECG

Main.py

import streamlit as st
import Processor as processor
import json

ecgProperty = processor.GetSourceProperty(r"C:\Users\100.dat")

st.write(type(ecgProperty))
st.write(ecgProperty)
jsonECGPropertyStr = json.dumps(ecgProperty.__dict__)
st.write(jsonECGPropertyStr)

Processor.py

import streamlit as st
import Controllers.ECGModel as ecgModel

def GetSourceProperty(filePath):
    ecg = ecgModel.ECG
    ecg.Source = "MIT"
    ecg.FileName = "100"
    ecg.Channel = 2
    ecg.Record = 11520000
    ecg.Time = 1800
    ecg.SampleRate = 500
    return ecg

Log:

<class 'type'>
<class 'Controllers.ECGModel.ECG'>
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\site-packages\streamlit\script_runner.py", line 354, in _run_script
    exec(code, module.__dict__)
File "D:\SourceCode\BIS2019_MasterThesis\ECG_Evaluation\Main.py", line 27, in <module>
    jsonECGPropertyStr = json.dumps(ecgProperty.__dict__)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type mappingproxy is not JSON serializable
Huy DQ
  • 121
  • 1
  • 11
  • What is your expected output from `json.dumps(ecgProperty.__dict__)` ? A json string that holds the value of `source, FileName, ...` ? – Rad Oct 24 '21 at 11:21
  • @ShihabusSakibRad Yes, that's correct – Huy DQ Oct 24 '21 at 11:58

1 Answers1

0

I tried to guess what you are trying to achieve. See if the following codes work for you

ECGModel.py

class ECG:
    def __init__(self, source, file_name, channel, record, time, sample_rate):
        self.source = source
        self.file_name = file_name
        self.channel = channel
        self.record = record
        self.time = time
        self.sample_rate = sample_rate

Processor.py

from ECGModel import ECG


def GetSourceProperty():
    return ECG(source="MIT", file_name="100", channel=2, record=11520000, time=1800, sample_rate=500)

Main.py: You may replace print with your st.write

import Processor
import json

ecg = Processor.GetSourceProperty()

print(type(ecg))
print(ecg)
print(json.dumps(ecg.__dict__))

Output

<class 'ECGModel.ECG'>
<ECGModel.ECG object at 0x000002ED4E82B670>
{"source": "MIT", "file_name": "100", "channel": 2, "record": 11520000, "time": 1800, "sample_rate": 500}
Rad
  • 113
  • 9
  • Thanks. I found 2 issues in my code. 1. The `@classmethod` caused the issue (if I put it there, the output will be empty). 2. All attributes must be like their parameters - `self.source = source` - should be "source" instead of "Source" – Huy DQ Oct 24 '21 at 23:12
  • 1
    @HuyDQ No, It's not mandatory for attributes name to be same as parameters name. You could do `self.source_name = source`, In that case, Json would contain `source_name`. If you are new to python, you can read about [classmethod](https://stackoverflow.com/questions/136097/difference-between-staticmethod-and-classmethod), [init](https://stackoverflow.com/questions/625083/what-init-and-self-do-in-python), [attribute name](https://stackoverflow.com/questions/159720/what-is-the-naming-convention-in-python-for-variable-and-function-names) – Rad Oct 25 '21 at 08:18