mirror of
https://github.com/hohn/sarif-cli.git
synced 2025-12-16 01:13:03 +01:00
37 lines
860 B
Python
Executable File
37 lines
860 B
Python
Executable File
#!/usr/bin/env python
|
|
import orjson
|
|
import sys
|
|
|
|
def sizeof(x):
|
|
t = type(x)
|
|
if t is dict:
|
|
return sum(len(k) + sizeof(v) for k, v in x.items())
|
|
if t is list:
|
|
return sum(sizeof(v) for v in x)
|
|
if t is str:
|
|
return len(x)
|
|
return 8 # numbers, bools, null
|
|
|
|
def walk(x, path="root"):
|
|
if isinstance(x, dict):
|
|
yield path, sizeof(x)
|
|
for k,v in x.items():
|
|
yield from walk(v, f"{path}.{k}")
|
|
elif isinstance(x, list):
|
|
yield path, sizeof(x)
|
|
for i,v in enumerate(x):
|
|
yield from walk(v, f"{path}[{i}]")
|
|
|
|
# with open(sys.stdin, 'rb') as f:
|
|
# data_bytes = f.read()
|
|
|
|
data_bytes = sys.stdin.buffer.read()
|
|
|
|
parsed_data = orjson.loads(data_bytes)
|
|
|
|
sizes = sorted(walk(parsed_data), key=lambda kv: kv[1], reverse=True)
|
|
for p, s in sizes[:50]:
|
|
print(f"{s:10d} {p}")
|
|
|
|
|