Created
September 25, 2022 09:44
-
-
Save narenaryan/a12b05866056081837ebb29d01189d3e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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