私はsimplejsonを使用してjson文字列をPythonオブジェクトに逆直列化しています。私はjsonを自分のドメインオブジェクトにデシリアライズすることを担当するカスタムの書かれたobject_hookを持っています。Pythonオブジェクトに大きなjson文字列をデシリアライズ
私のjson文字列が巨大な場合(つまり、サーバーが約800Kのドメインオブジェクトをjson文字列の形で返している)、私のpythonデシリアライザはそれらを逆シリアル化するのに約10分かかります。
もう少し詳しく掘り下げてみましたが、それは単なるjsonsonのように見えますが、それはすべてをobject_hookに委任しています。私はobject_hookを最適化しようとしましたが、それも私のパフォーマンスを改善していません。 (私はほとんど1分の改善を得た)
私の質問は、巨大なデータセットを扱うために最適化された他の標準フレームワークを持っているのか、object_hookレベルですべてを行うのではなくフレームワークの能力を利用できる方法です。
私は、object_hookがなければ、フレームワークはドメインオブジェクトのリストではなく辞書のリストを返すことが分かります。
ここにはすべてのポインタが便利です。 FYI私はここのsimplejsonバージョン3.7.2
を使用しています
は私のサンプル_object_hookです:
def _object_hook(dct):
if '@CLASS' in dct: # server sends domain objects with this @CLASS
clsname = dct['@CLASS']
# This is like Class.forName (This imports the module and gives the class)
cls = get_class(clsname)
# As my server is in java, I convert the attributes to python as per python naming convention.
dct = dict((convert_java_name_to_python(k), dct[k]) for k in dct.keys())
if cls != None:
obj_key = None
if "@uuid"in dct
obj_key = dct["@uuid"]
del(dct["@uuid"])
else:
info("Class missing uuid: " + clsname)
dct.pop("@CLASS", None)
obj = cls(**dct) #This I found to be the most time consuming process. In my domian object, in the __init__ method I have the logic to set all attributes based on the kwargs passed
if obj_key is not None:
shared_objs[obj_key] = obj #I keep all uuids along with the objects in shared_objs dictionary. This shared_objs will be used later to replace references.
else:
warning("class not found: " + clsname)
obj = dct
return obj
else:
return dct
サンプル応答:
{"@CLASS":"sample.counter","@UUID":"86f26a0a-1a58-4429-a762- 9b1778a99c82","val1":"ABC","val2":1131,"val3":1754095,"value4": {"@CLASS":"sample.nestedClass","@UUID":"f7bb298c-fd0b-4d87-bed8- 74d5eb1d6517","id":1754095,"name":"XYZ","abbreviation":"ABC"}}
私は多くの入れ子のレベルと数を持っていますサーバーから受け取ったレコードの数は800K以上です。
興味深いですね。それを素早くチェックするためのサンプルスニペットが便利です。 –
'object_hook'関数のコードと解析したいJSONのサンプルを投稿できれば、あなたの質問に答えるのに役立ちます。 – jstlaurent