From dbb5ca2b3bc174f06a38a8587ab845020424de41 Mon Sep 17 00:00:00 2001 From: Jonathan Branan Date: Tue, 25 Jun 2024 13:40:52 -0500 Subject: [PATCH] Updated json encoder to change sql unsupported object types to supported versions --- .gitignore | 3 ++- inex.py | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index ab8b69c..d92f5e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -config.toml \ No newline at end of file +config.toml +*.json \ No newline at end of file diff --git a/inex.py b/inex.py index 329aa8e..04e4340 100644 --- a/inex.py +++ b/inex.py @@ -1,11 +1,12 @@ import pandas import pyodbc import os -from datetime import datetime +import datetime from tomllib import load import inexLogging import inexConnect -from json import dump,dumps +import json +import decimal class Inex: def __init__(self): @@ -35,13 +36,19 @@ class Inex: self.cursor = self.ic.connectDatabase(self.db, self.dbDriver, self.dbServer, self.dbDatabase, self.dbUser, self.dbPassword) self.data = self.ic.databaseQuery(self.cursor, self.dbQuery) - - # self.jsonData = dumps(self.data) - # print(self.jsonData) + + # print(self.data) with open(self.outputFile, "w") as f: - dump(self.data, f) + json.dump(self.data, f, cls=Encoder) +class Encoder(json.JSONEncoder): + def default(self, o): + if isinstance(o, decimal.Decimal): + return int(o) + if isinstance(o, datetime.datetime): + return str(o) + return super().default(o) # Run if __name__== "__main__":