diff -ur v3/ezyaml.py v4/ezyaml.py --- v3/ezyaml.py 2018-08-31 01:39:44.000000000 +0900 +++ v4/ezyaml.py 2018-08-31 02:40:11.000000000 +0900 @@ -90,8 +90,7 @@ k = get_value( k.strip() ) v = get_value( v.strip() ) - if not v: - v = None + if v == None: if lines: v = get_dict_v(idt, lines) dic = { k: v } @@ -124,6 +123,13 @@ except ValueError: pass + if s == '[]': + return [] + if s == '{}': + return {} + if s == '': + return None + lst = parse(s) if len(lst) == 1 and s[:1] in qts: s = s[1:-1] @@ -158,10 +164,10 @@ lines = [] for (k, v) in dic.items(): s = dump_other(k, idt) + ':' - if type(v) == list: + if type(v) == list and v: lines.append(s) s = dump_list(v, idt) - elif type(v) == dict: + elif type(v) == dict and v: lines.append(s) s = dump_dict(v, idt+2) else: @@ -184,11 +190,17 @@ if q2 and q1: s = s.replace("'", "\\'") q1 = False - if q2 or q1 or cl or get_value(s) != obj: + if q2 or q1 or cl or get_value(s) != obj or s == '': qt = '"' if q1 else "'" s = qt + s + qt return s + if type(obj) == list: + return '[]' + + if type(obj) == dict: + return '{}' + return '{}'.format(obj) def dump_other(obj, idt): @@ -196,9 +208,9 @@ def dump_obj(obj, idt): t = type(obj) - if t == dict: + if t == dict and obj: return dump_dict(obj, idt) - if t == list: + if t == list and obj: return dump_list(obj, idt) return dump_other(obj, idt)