Request
I was wondering if it's possible to take the default jsons.dump
behavior and make it idempotent (return the input string) for python IPAdresses.
This would enable me to use an object while in python and use the same string in all serializations and deserializations. That way when we load the serialized JSON we don't need different control paths for the first program that loads the data and the second + N programs that load it.
Current Behavior
>>> import ipaddress
>>> import jsons
>>> import ipaddress
>>> ipaddress.IPv4Address("192.0.0.1")
IPv4Address('192.0.0.1')
>>> jsons.dump(ipaddress.IPv4Address("192.0.0.1"))
{'_ip': 3221225473}
>>> jsons.load(jsons.dump(ipaddress.IPv4Address("192.0.0.1")))
{'_ip': 3221225473}
Desired Behavior
>>> jsons.load(jsons.dump(ipaddress.IPv4Address("192.0.0.1")))
"192.0.0.1"
Desired but Probably Asking too Much
>>> jsons.load(jsons.dump(ipaddress.IPv4Address("192.0.0.1")))
IPv4Address('192.0.0.1')
Current workaround
I've changed the __repr__
method to do type conversions to string for now. But this means I have to do jsons.dump(repr(<variable>))
and this means other developers that work with my code have a potential landmine they need to be aware of.