I'm using json.dump with a custom encoder in python2, and I want to pass an additional parameter to the encoder, this way:
json.dump(data, fp, type, cls=MyEncoder)
Following How to pass parameters to a custom JSONEncoder default() function in Python I wrote the encoder:
class MyEncoder(json.JSONEncoder):
def __init__(self, type, **kwargs):
super(MyEncoder, self).__init__(**kwargs)
self.type = type
def default(self, obj):
type = self.type
if isinstance(obj, datetime):
# there was the line:
but, in init, the type value is assigned to kwargs['skipkeys'], not to type variable.
What am I missing?
Would it be different in python3?