Skip to content

Instantly share code, notes, and snippets.

@dylanwh
Last active May 31, 2022 01:12
Show Gist options
  • Select an option

  • Save dylanwh/c2c4d49ce51118b44b1cc2a158a2fcb4 to your computer and use it in GitHub Desktop.

Select an option

Save dylanwh/c2c4d49ce51118b44b1cc2a158a2fcb4 to your computer and use it in GitHub Desktop.

Revisions

  1. dylanwh revised this gist May 31, 2022. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions ungron.py
    Original file line number Diff line number Diff line change
    @@ -11,13 +11,12 @@ def __init__(self,name="json",val=None):
    my_list = list()
    object.__setattr__(self, '_gron__dict', my_dict)
    object.__setattr__(self, '_gron__list', my_list)
    pass

    def __str__(self):
    import json as _json
    return _json.dumps(self.__dict, default = lambda x: x.__gron_json())
    return _json.dumps(self.__dict, default = lambda x: x.__json())

    def __gron_json(self):
    def __json(self):
    if len(self.__list):
    return self.__list
    else:
  2. dylanwh created this gist May 31, 2022.
    54 changes: 54 additions & 0 deletions ungron.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    #!/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)
    pass

    def __str__(self):
    import json as _json
    return _json.dumps(self.__dict, default = lambda x: x.__gron_json())

    def __gron_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)