I want to get a dictionary out of a Python class object. I'm using JSONEncoder to covert the same. However, what I am getting in return is double jsonified object. All i want is a dictionary.
class DocumentOverlay:
page: int
top: None
left: None
width: None
height: None
def __init__(self, page: int, top: None, left: None, width: None, height: None) -> None:
self.page = page
self.top = top
self.left = left
self.width = width
self.height = height
class DSAElement:
primaryDocument: str
matchesWithDocument: str
primaryDocumentOverlay: DocumentOverlay
matchesWithDocumentOverlay: DocumentOverlay
similarityIndex: float
isSimilar: bool
def __init__(self, primaryDocument: str, matchesWithDocument: str, primaryDocumentOverlay: DocumentOverlay, matchesWithDocumentOverlay: DocumentOverlay, similarityIndex: float, isSimilar: bool) -> None:
self.primaryDocument = primaryDocument
self.matchesWithDocument = matchesWithDocument
self.primaryDocumentOverlay = primaryDocumentOverlay
self.matchesWithDocumentOverlay = matchesWithDocumentOverlay
self.similarityIndex = similarityIndex
self.isSimilar = isSimilar
class dsaEncoder(JSONEncoder):
def default(self, o):
return o.__dict__
output=[]
for term, tdoc in zip(thumbnail, tdocs):
sim = doc.similarity(tdoc)
if sim>0.99:
primary_overlay=DocumentOverlay(term['page'], None, None, None, None)
secondary_overlay=DocumentOverlay(i+1, None, None, None, None)
dsa_overlay=DSAElement(term['fileName'], item['fileName'], primary_overlay, secondary_overlay, sim, True)
output.append(dsaEncoder().encode(dsa_overlay))
What i get in return is:
['{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 1, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 1, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.9993153342654213, "isSimilar": true}', '{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 2, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 2, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.9983211993255433, "isSimilar": true}', '{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 21, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 2, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.997596096801685, "isSimilar": true}', '{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 3, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 3, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.9988208175324497, "isSimilar": true}', '{"primaryDocument": "Aurum1.pdf", "matchesWithDocument": "Aurum2.pdf", "primaryDocumentOverlay": {"page": 4, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 4, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.9960270471399298, "isSimilar": true}']
Any help would be highly appreciated on how to get the return dict like this:
[{"primaryDocument": "test.pdf", "matchesWithDocument": "test2.pdf", "primaryDocumentOverlay": {"page": 1, "top": null, "left": null, "width": null, "height": null}, "matchesWithDocumentOverlay": {"page": 2, "top": null, "left": null, "width": null, "height": null}, "similarityIndex": 0.92, "isSimilar": true},....]