Last active
May 31, 2022 01:12
-
-
Save dylanwh/c2c4d49ce51118b44b1cc2a158a2fcb4 to your computer and use it in GitHub Desktop.
ungron but preserve the ordering of object keys
This file contains 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
#!/usr/bin/env python3 | |
class gron: | |
def __init__(self,name="json",val=None): | |
if isinstance(val, dict): | |
my_dict = val | |
else: | |
my_dict = dict() | |
if isinstance(val, list): | |
my_list = val | |
else: | |
my_list = list() | |
object.__setattr__(self, '_gron__dict', my_dict) | |
object.__setattr__(self, '_gron__list', my_list) | |
def __str__(self): | |
import json as _json | |
return _json.dumps(self.__dict, default = lambda x: x.__json()) | |
def __json(self): | |
if len(self.__list): | |
return self.__list | |
else: | |
return self.__dict | |
def __setitem__(self, item, val): | |
if isinstance(item, int): | |
l = self.__list | |
while len(l) <= item: | |
l.append(None) | |
l[item] = val | |
else: | |
self.__dict[item] = valddk | |
def __setattr__(self, key, val): | |
if isinstance(val, (dict,list)): | |
val = gron(key, val) | |
self.__dict[key] = val | |
return | |
def __getattr__(self, key): | |
if key in self.__dict: | |
return self.__dict[key] | |
else: | |
self.__dict[key] = gron(key) | |
return self.__dict[key] | |
if __name__ == '__main__': | |
import sys | |
json = gron() | |
for line in sys.stdin: | |
exec(line, None, {"json": json}) | |
print(json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment