json モジュールについて (3) ( json5dumper 編 )

自作 json5 用 ファイルダンプクラス ( json5dumper.py )

  • json5の形式で、データを保存する.

    • (容量の効率よりも、可読性を重視したい.)

  • numpy ndarray をサポートしていないため、データ形式を問わずにdumpするのは難しい.

    • ndarray は np.save を用いてバイナリ保存.ファイルパスのみ jsonファイル内に記載.

    • 他は、json形式として、可読性を保つ.

使い方

  • クラス宣言

import nkUtilities.json5dumper as j5d
dumper = j5d.json5dumper()
dumper.dump( outFile=outFile, Data=Data )

Data = dumper.recall( inpFile=outFile )
  • dumpコマンドを関数ライクに直接呼ぶ.

import nkUtilities.json5dumper as j5d
dumper = j5d.json5dumper().dump( outFile=outFile, Data=Data )
  • recall コマンドを関数ライクに直接呼ぶ.

import nkUtilities.json5dumper as j5d
Data = j5d.json5dumper().recall( inpFile=inpFile )

実行結果 (dump)

{float1: 0.0, int1: 10, str1: "string", list1: [0.0, 1.0], list2: ["a1", "b2", "c2", 3], arr1: "numpyArrayPath::dat/output_npy/arr1.npy", arr2: "numpyArrayPath::dat/output_npy/arr2.npy", arr3: "numpyArrayPath::dat/output_npy/arr3.npy", dict1: {a: 0.0, b: 1.0, c: "ok"}}

実行結果 (recall)

[json5dumper.py] Class Defined. USE json5dumper.dump() / json5dumper.recall()
[json5dumper.py] save in :: dat/output.json
[json5dumper.py] Class Defined. USE json5dumper.dump() / json5dumper.recall()

[Data]
{'float1': 0.0, 'int1': 10, 'str1': 'string', 'list1': [0.0, 1.0], 'list2': ['a1', 'b2', 'c2', 3], 'arr1': array([0.  , 0.25, 0.5 , 0.75, 1.  ]), 'arr2': array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],

       [[ 6,  7],
        [ 8,  9],
        [10, 11]],

       [[12, 13],
        [14, 15],
        [16, 17]],

       [[18, 19],
        [20, 21],
        [22, 23]],

       [[24, 25],
        [26, 27],
        [28, 29]]]), 'arr3': array([[   0,    1,    2, ...,  997,  998,  999],
       [1000, 1001, 1002, ..., 1997, 1998, 1999]]), 'dict1': {'a': 0.0, 'b': 1.0, 'c': 'ok'}}

[recalled]
{'float1': 0.0, 'int1': 10, 'str1': 'string', 'list1': [0.0, 1.0], 'list2': ['a1', 'b2', 'c2', 3], 'arr1': array([0.  , 0.25, 0.5 , 0.75, 1.  ]), 'arr2': array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],

       [[ 6,  7],
        [ 8,  9],
        [10, 11]],

       [[12, 13],
        [14, 15],
        [16, 17]],

       [[18, 19],
        [20, 21],
        [22, 23]],

       [[24, 25],
        [26, 27],
        [28, 29]]]), 'arr3': array([[   0,    1,    2, ...,  997,  998,  999],
       [1000, 1001, 1002, ..., 1997, 1998, 1999]]), 'dict1': {'a': 0.0, 'b': 1.0, 'c': 'ok'}}