[d565449] | 1 | from flatted import stringify as _stringify, parse
|
---|
| 2 |
|
---|
| 3 | def stringify(value):
|
---|
| 4 | return _stringify(value, separators=(',', ':'))
|
---|
| 5 |
|
---|
| 6 | assert stringify([None, None]) == '[[null,null]]'
|
---|
| 7 |
|
---|
| 8 | a = []
|
---|
| 9 | o = {}
|
---|
| 10 |
|
---|
| 11 | assert stringify(a) == '[[]]'
|
---|
| 12 | assert stringify(o) == '[{}]'
|
---|
| 13 |
|
---|
| 14 | a.append(a)
|
---|
| 15 | o['o'] = o
|
---|
| 16 |
|
---|
| 17 | assert stringify(a) == '[["0"]]'
|
---|
| 18 | assert stringify(o) == '[{"o":"0"}]'
|
---|
| 19 |
|
---|
| 20 | b = parse(stringify(a))
|
---|
| 21 | assert isinstance(b, list) and b[0] == b
|
---|
| 22 |
|
---|
| 23 | a.append(1)
|
---|
| 24 | a.append('two')
|
---|
| 25 | a.append(True)
|
---|
| 26 | o['one'] = 1
|
---|
| 27 | o['two'] = 'two'
|
---|
| 28 | o['three'] = True
|
---|
| 29 |
|
---|
| 30 | assert stringify(a) == '[["0",1,"1",true],"two"]'
|
---|
| 31 | assert stringify(o) == '[{"o":"0","one":1,"two":"1","three":true},"two"]'
|
---|
| 32 |
|
---|
| 33 | a.append(o)
|
---|
| 34 | o['a'] = a
|
---|
| 35 |
|
---|
| 36 | assert stringify(a) == '[["0",1,"1",true,"2"],"two",{"o":"2","one":1,"two":"1","three":true,"a":"0"}]'
|
---|
| 37 | assert stringify(o) == '[{"o":"0","one":1,"two":"1","three":true,"a":"2"},"two",["2",1,"1",true,"0"]]'
|
---|
| 38 |
|
---|
| 39 | a.append({'test': 'OK'})
|
---|
| 40 | a.append([1, 2, 3])
|
---|
| 41 |
|
---|
| 42 | o['test'] = {'test': 'OK'}
|
---|
| 43 | o['array'] = [1, 2, 3]
|
---|
| 44 |
|
---|
| 45 | assert stringify(a) == '[["0",1,"1",true,"2","3","4"],"two",{"o":"2","one":1,"two":"1","three":true,"a":"0","test":"3","array":"4"},{"test":"5"},[1,2,3],"OK"]'
|
---|
| 46 | assert stringify(o) == '[{"o":"0","one":1,"two":"1","three":true,"a":"2","test":"3","array":"4"},"two",["2",1,"1",true,"0","3","4"],{"test":"5"},[1,2,3],"OK"]'
|
---|
| 47 |
|
---|
| 48 | a2 = parse(stringify(a));
|
---|
| 49 | o2 = parse(stringify(o));
|
---|
| 50 |
|
---|
| 51 | assert a2[0] == a2
|
---|
| 52 | assert o2['o'] == o2
|
---|
| 53 |
|
---|
| 54 | assert a2[1] == 1 and a2[2] == 'two' and a2[3] == True and isinstance(a2[4], dict)
|
---|
| 55 | assert a2[4] == a2[4]['o'] and a2 == a2[4]['o']['a']
|
---|
| 56 |
|
---|
| 57 | str = parse('[{"prop":"1","a":"2","b":"3"},{"value":123},["4","5"],{"e":"6","t":"7","p":4},{},{"b":"8"},"f",{"a":"9"},["10"],"sup",{"a":1,"d":2,"c":"7","z":"11","h":1},{"g":2,"a":"7","b":"12","f":6},{"r":4,"u":"7","c":5}]')
|
---|
| 58 | assert str['b']['t']['a'] == 'sup' and str['a'][1]['b'][0]['c'] == str['b']['t']
|
---|
| 59 |
|
---|
| 60 | oo = parse('[{"a":"1","b":"0","c":"2"},{"aa":"3"},{"ca":"4","cb":"5","cc":"6","cd":"7","ce":"8","cf":"9"},{"aaa":"10"},{"caa":"4"},{"cba":"5"},{"cca":"2"},{"cda":"4"},"value2","value3","value1"]');
|
---|
| 61 | assert oo['a']['aa']['aaa'] == 'value1' and oo == oo['b'] and oo['c']['ca']['caa'] == oo['c']['ca']
|
---|
| 62 |
|
---|
| 63 | print('OK')
|
---|