Skip to content

Instantly share code, notes, and snippets.

@narenaryan
Created September 25, 2022 09:44
Show Gist options
  • Save narenaryan/a12b05866056081837ebb29d01189d3e to your computer and use it in GitHub Desktop.
Save narenaryan/a12b05866056081837ebb29d01189d3e to your computer and use it in GitHub Desktop.
import json
class Color:
name = None
found_in = None
def __init__(self, name) -> None:
self.name = name
def custom_encoder(obj):
json_result = {}
for k,v in obj.__dict__.items():
segments = k.split("_")
if len(segments) == 1:
json_result[k] = v
continue
new_key = segments[0] + segments[1].capitalize()
json_result[new_key] = v
return json_result
red = Color("RED")
red.found_in = "soil"
result = json.dumps(red, default=custom_encoder)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment