まず、ここで本当にハードディスクを節約できるかどうかを判断する必要があります。パーセント差で考えるのではなく、プログラムを実行する人々のディスクスペースのコストです。
次に、これを行うハックな方法を見つけました。
フォルダー /Lib/json/ を Python インストール以外のフォルダーにコピーします。フォルダに次のような名前を付けます。jsondepth
encoding.py に移動し、3 つの変更を行います (行番号は Python 2.7 のものです)。
411行目、変更
def _iterencode(o, _current_indent_level):
if isinstance(o, basestring):
yield _encoder(o)
に
def _iterencode(o, _current_indent_level):
if(_current_indent_level > SKIP_INDENT_LEVEL):
yield 'null'
return
if isinstance(o, basestring):
yield _encoder(o)
行 335、変更
def _iterencode_dict(dct, _current_indent_level):
if not dct:
yield '{}'
に
def _iterencode_dict(dct, _current_indent_level):
if(_current_indent_level > SKIP_INDENT_LEVEL):
yield 'null'
return
if not dct:
yield '{}'
行 282、変更
def _iterencode_list(lst, _current_indent_level):
if not lst:
yield '[]'
に
def _iterencode_list(lst, _current_indent_level):
if(_current_indent_level > SKIP_INDENT_LEVEL):
yield 'null'
return
if not lst:
yield '[]'
5 行目、変更
try:
from _json import encode_basestring_ascii as c_encode_basestring_ascii
except ImportError:
c_encode_basestring_ascii = None
try:
from _json import make_encoder as c_make_encoder
except ImportError:
c_make_encoder = None
に
c_encode_basestring_ascii = None
c_make_encoder = None
ファイルの先頭のどこか (たとえば、13 行目)
SKIP_INDENT_LEVEL = 4 # or whatever recursion depth you want.
インデントを有効にする必要があることに注意してください。有効にしないと、これは完全に崩壊します。また、私はこれをテストしていないことに注意してください。明示または黙示の保証はありません:)