i want to use jsonpickle to serialize custom classes.
In this case, i have an IPv4Address instance where i just want to return the string representation
So i created a jsonpickle hanlder and and registered it
import jsonpickle
from ipaddress import IPv4Address
@jsonpickle.handlers.register(IPv4Address, base=True)
class JsonHandler(jsonpickle.handlers.BaseHandler):
def flatten(self, obj, data):
if isinstance(obj, IPv4Address):
return str(obj)
When i now create an IPv4 Address instance
addr = IPv4Address('1.1.1.1')
encoded = jsonpickle.encode(addr)
I get the output
'"1.1.1.1"'
Because jsonpickle wraps it in quotes again it becomes this “embedded” string with single and double quotes. Is there a way to tell jsonpickle to not process it further and jsu return the string witjhout having to replace the quotes by hand with encoded.replace()?
i could of course also create a custom IPv4Address class and add the __getstate__ method, but just curios if theres a way to do it with pickle
Thx in advance
Getting a string from jsonpickle without wrapped double quotes