0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

jsonをorjsonに移行するときのポイント

0
Last updated at Posted at 2025-09-30

LL言語にありがちですが標準ライブラリのjsonは言語ネイティブ実装なこともあり遅めです。
Pythonでライブラリを使うならfastapiとも親和性が高いorjsonが一つの定番です

import orjson as jsonという形で置き換え可能ですが
それでも細かな差異は出ます

dumps

•	json.dumps() → str を返す
•	orjson.dumps() → bytes を返す

orjson.dumps.decode()としましょう

datetime

互換性重視でjsonの動作に近づけるならOPT_PASSTHROUGH_DATETIMEをoptionで渡すとよいです
ただ、orjsonの標準動作が一番高速なのでできれば標準のほうに寄せましょう

numpy

これも互換性重視ならOPT_SERIALIZE_NUMPYが近い動作になります
これもorjsonの標準動作が高速なのでなるべく寄せましょう

両方optionを渡す例

orjson.dumps(data, option=orjson.OPT_PASSTHROUGH_DATETIME | orjson.OPT_SERIALIZE_NUMPY).decode()

当然ですがどのjsonライブラリを使うにせよ最初から導入するのが一番効率良いです
jsonデータを良く扱うなら標準jsonの本番利用は控えましょう

python地味ネタ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?