With Python and JsonPickle, How do I serialize the object with a Certain Casing, eg Camel Case, Pascal, etc? The following answer below does it manually, however looking for a specific Jsonpickle solution, since it can handle complex object types .
JSON serialize a class and change property casing with Python
https://stackoverflow.com/a/8614096/15435022
class HardwareSystem:
def __init__(self, vm_size):
self.vm_size = vm_size
self.some_other_thing = 42
self.a = 'a'
def snake_to_camel(s):
a = s.split('_')
a[0] = a[0].lower()
if len(a) > 1:
a[1:] = [u.title() for u in a[1:]]
return ''.join(a)
def serialise(obj):
return {snake_to_camel(k): v for k, v in obj.__dict__.items()}
hp = HardwareSystem('Large')
print(json.dumps(serialise(hp), indent=4, default=serialise))
Here's my attempt.
This has some assumptions:
snake_to_camelfunction, I've put up acamel_to_snakeon decoding, that assumes only the first uppercase letter after a lowercase letter will have prepended the_char (soawesomeABCwill translate toawesome_abc, and therefore if you translate it back again it will be incorrectlyawesomeAbc)__init__(see for examplehw.software_instanceabove).debug_output/__repr__functions, you may throw these away (or customize :))