I am trying to serialize a python class into json, and it works fine and great in python 2.7 and 3.8
But when I try to test it out in the script console it shows an issue with the stringmap
type containing the __dict__
method. I dont see the same issue in any regular python interpreter, so I am curious if this is a Jython issue and something I am potentially missing.
import json
class Operation():
def __init__(self, name):
self.name = name
def __repr__(self):
return json.dumps(operation, default=ComplexEncoder)
def ComplexEncoder(obj):
print('Encoding the json...')
return obj.__dict__
operation = Operation("Part 1: Base")
#Encode the operation object into a json
jsonStr = json.dumps(operation, default=ComplexEncoder)
print("Encoded JSON:",jsonStr)
What else is interesting, is in Jython it actually runs the encoder function twice for some reason, printing “Encoding the json…” twice? Whereas in regular python it only runs the function once.
Any ideas would be greatly appreciated!
EDIT: To clarify, it runs the ComplexEncoder
method twice even when I comment out the __repr__
method and the line printing at the end like so:
import json
class Operation():
def __init__(self, name):
self.name = name
# def __repr__(self):
# return json.dumps(operation, default=ComplexEncoder)
def ComplexEncoder(obj):
print('Encoding the json...')
return obj.__dict__
operation = Operation("Part 1: Base")
#Encode the operation object into a json
jsonStr = json.dumps(operation, default=ComplexEncoder)
#print("Encoded JSON:",jsonStr)