expatjson 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- expatjson-0.1.0/CHANGELOG.md +11 -0
- expatjson-0.1.0/LICENSE +21 -0
- expatjson-0.1.0/MANIFEST.in +6 -0
- expatjson-0.1.0/PKG-INFO +430 -0
- expatjson-0.1.0/README.en.md +391 -0
- expatjson-0.1.0/README.md +408 -0
- expatjson-0.1.0/SECURITY.md +14 -0
- expatjson-0.1.0/bench/bench.py +47 -0
- expatjson-0.1.0/pyproject.toml +42 -0
- expatjson-0.1.0/setup.cfg +4 -0
- expatjson-0.1.0/setup.py +69 -0
- expatjson-0.1.0/src/expatjson/__init__.py +224 -0
- expatjson-0.1.0/src/expatjson/__main__.py +45 -0
- expatjson-0.1.0/src/expatjson/_pure.py +78 -0
- expatjson-0.1.0/src/expatjson/errors.py +198 -0
- expatjson-0.1.0/src/expatjson.egg-info/PKG-INFO +430 -0
- expatjson-0.1.0/src/expatjson.egg-info/SOURCES.txt +60 -0
- expatjson-0.1.0/src/expatjson.egg-info/dependency_links.txt +1 -0
- expatjson-0.1.0/src/expatjson.egg-info/entry_points.txt +2 -0
- expatjson-0.1.0/src/expatjson.egg-info/top_level.txt +1 -0
- expatjson-0.1.0/src/native/expat_config.h +32 -0
- expatjson-0.1.0/src/native/speedups.cpp +191 -0
- expatjson-0.1.0/tests/test_expatjson.py +227 -0
- expatjson-0.1.0/third_party/expat/COPYING +21 -0
- expatjson-0.1.0/third_party/expat/VERSION +3 -0
- expatjson-0.1.0/third_party/expat/lib/ascii.h +126 -0
- expatjson-0.1.0/third_party/expat/lib/asciitab.h +68 -0
- expatjson-0.1.0/third_party/expat/lib/entities.h +99 -0
- expatjson-0.1.0/third_party/expat/lib/expat.h +1164 -0
- expatjson-0.1.0/third_party/expat/lib/expat_external.h +183 -0
- expatjson-0.1.0/third_party/expat/lib/fallthrough.h +51 -0
- expatjson-0.1.0/third_party/expat/lib/hash_table.h +78 -0
- expatjson-0.1.0/third_party/expat/lib/iasciitab.h +69 -0
- expatjson-0.1.0/third_party/expat/lib/internal.h +160 -0
- expatjson-0.1.0/third_party/expat/lib/latin1tab.h +68 -0
- expatjson-0.1.0/third_party/expat/lib/memory_sanitizer.h +53 -0
- expatjson-0.1.0/third_party/expat/lib/nametab.h +102 -0
- expatjson-0.1.0/third_party/expat/lib/random_arc4random.c +58 -0
- expatjson-0.1.0/third_party/expat/lib/random_arc4random.h +41 -0
- expatjson-0.1.0/third_party/expat/lib/random_arc4random_buf.c +49 -0
- expatjson-0.1.0/third_party/expat/lib/random_arc4random_buf.h +41 -0
- expatjson-0.1.0/third_party/expat/lib/random_dev_urandom.c +74 -0
- expatjson-0.1.0/third_party/expat/lib/random_dev_urandom.h +42 -0
- expatjson-0.1.0/third_party/expat/lib/random_getentropy.c +62 -0
- expatjson-0.1.0/third_party/expat/lib/random_getentropy.h +42 -0
- expatjson-0.1.0/third_party/expat/lib/random_getrandom.c +97 -0
- expatjson-0.1.0/third_party/expat/lib/random_getrandom.h +42 -0
- expatjson-0.1.0/third_party/expat/lib/random_rand_s.c +90 -0
- expatjson-0.1.0/third_party/expat/lib/random_rand_s.h +43 -0
- expatjson-0.1.0/third_party/expat/lib/siphash.h +395 -0
- expatjson-0.1.0/third_party/expat/lib/utf8tab.h +68 -0
- expatjson-0.1.0/third_party/expat/lib/winconfig.h +50 -0
- expatjson-0.1.0/third_party/expat/lib/xcs.c +111 -0
- expatjson-0.1.0/third_party/expat/lib/xcs.h +47 -0
- expatjson-0.1.0/third_party/expat/lib/xmlparse.c +10438 -0
- expatjson-0.1.0/third_party/expat/lib/xmlrole.c +1256 -0
- expatjson-0.1.0/third_party/expat/lib/xmlrole.h +130 -0
- expatjson-0.1.0/third_party/expat/lib/xmltok.c +1653 -0
- expatjson-0.1.0/third_party/expat/lib/xmltok.h +321 -0
- expatjson-0.1.0/third_party/expat/lib/xmltok_impl.c +1819 -0
- expatjson-0.1.0/third_party/expat/lib/xmltok_impl.h +76 -0
- expatjson-0.1.0/third_party/expat/lib/xmltok_ns.c +135 -0
expatjson-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 孤独のコーヒー
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
include LICENSE README.md README.en.md CHANGELOG.md SECURITY.md
|
|
2
|
+
recursive-include src/native *.cpp *.h
|
|
3
|
+
recursive-include third_party/expat *.c *.h COPYING VERSION
|
|
4
|
+
recursive-include tests *.py
|
|
5
|
+
recursive-include bench *.py
|
|
6
|
+
global-exclude __pycache__ *.pyc *.pyd *.so
|
expatjson-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: expatjson
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: XML を dict / JSON に変換するライブラリ / XML to dict and JSON converter based on Expat
|
|
5
|
+
Author: 孤独のコーヒー
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/solitary-coffee/expatjson
|
|
8
|
+
Project-URL: Issues, https://github.com/solitary-coffee/expatjson/issues
|
|
9
|
+
Keywords: xml,json,expat,parser,converter
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Programming Language :: C++
|
|
16
|
+
Classifier: Topic :: Text Processing :: Markup :: XML
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
License-File: third_party/expat/COPYING
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# expatjson
|
|
24
|
+
|
|
25
|
+
[](https://pypi.org/project/expatjson/)
|
|
26
|
+
[](https://pypi.org/project/expatjson/#files)
|
|
27
|
+
[](https://pypi.org/project/expatjson/#files)
|
|
28
|
+
[](https://pypi.org/project/expatjson/#files)
|
|
29
|
+
[](https://pypi.org/project/expatjson/#files)
|
|
30
|
+
[](https://github.com/solitary-coffee/expatjson/actions/workflows/ci.yml)
|
|
31
|
+
[](https://github.com/solitary-coffee/expatjson/blob/main/LICENSE)
|
|
32
|
+
|
|
33
|
+
XML を Python の `dict` や JSON に変換するライブラリです。パーサーには Expat を使っています。
|
|
34
|
+
追加の依存パッケージはありません。ビルド済みの wheel には C++ 拡張が含まれるため、コンパイラも不要です。
|
|
35
|
+
|
|
36
|
+
日本語 | [English](https://github.com/solitary-coffee/expatjson/blob/main/README.en.md)
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install expatjson
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## 目次
|
|
43
|
+
|
|
44
|
+
- [特長](#特長)
|
|
45
|
+
- [速度](#速度)
|
|
46
|
+
- [インストール](#インストール)
|
|
47
|
+
- [クイックスタート](#クイックスタート)
|
|
48
|
+
- [使い方](#使い方)
|
|
49
|
+
- [パーサーの指定](#パーサーの指定)
|
|
50
|
+
- [変換の規則](#変換の規則)
|
|
51
|
+
- [値になる場合とリストになる場合](#値になる場合とリストになる場合)
|
|
52
|
+
- [エラー処理](#エラー処理)
|
|
53
|
+
- [API リファレンス](#api-リファレンス)
|
|
54
|
+
- [開発](#開発)
|
|
55
|
+
- [お問い合わせ](#お問い合わせ)
|
|
56
|
+
- [ライセンス](#ライセンス)
|
|
57
|
+
|
|
58
|
+
## 特長
|
|
59
|
+
|
|
60
|
+
- C++ 拡張があれば自動で使います。ない環境では Python 実装が動きます。
|
|
61
|
+
- 標準ライブラリだけで動作します。
|
|
62
|
+
- DTD と実体宣言は受け付けません。入力サイズと階層にも上限があります。
|
|
63
|
+
- エラーには行番号、列番号、エラーコードが付きます。メッセージは日本語(既定)と英語に対応しています。
|
|
64
|
+
- 出力の形式は、属性が `@名前`、テキストが `#text`、繰り返しの要素がリストです。
|
|
65
|
+
|
|
66
|
+
## 速度
|
|
67
|
+
|
|
68
|
+
Windows 10、Python 3.11 で、生成した XML を変換した時間です(7 回の中央値)。値は文書の内容や環境によって
|
|
69
|
+
変わります。手元では `python bench/bench.py` で測れます。
|
|
70
|
+
|
|
71
|
+
| パーサー | 1.5 KiB | 149 KiB | 3 MiB |
|
|
72
|
+
|---|---:|---:|---:|
|
|
73
|
+
| **expatjson (native)** | **0.18 ms** | **11.2 ms** | **264 ms** |
|
|
74
|
+
| expatjson (Python) | 0.30 ms | 27.3 ms | 480 ms |
|
|
75
|
+
| xmltodict 1.0.4 | 0.57 ms | 53.2 ms | 1064 ms |
|
|
76
|
+
|
|
77
|
+
## インストール
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
pip install expatjson
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Python 3.9 以降が必要です。次の環境には、C++ 拡張を含むビルド済みの wheel があります。
|
|
84
|
+
|
|
85
|
+
| OS | CPU | Python |
|
|
86
|
+
|---|---|---|
|
|
87
|
+
| Windows | x86-64、ARM64(3.11 以降) | 3.9〜3.14 |
|
|
88
|
+
| Linux(glibc・musl) | x86-64、ARM64 | 3.9〜3.14 |
|
|
89
|
+
| macOS | Intel、Apple シリコン | 3.9〜3.14 |
|
|
90
|
+
|
|
91
|
+
どちらのパーサーが使われるかは、次のコマンドで確認できます。
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
python -c "import expatjson; print(expatjson.backend())" # native または python
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
<details>
|
|
98
|
+
<summary><code>python</code> と表示される場合(C++ 拡張をビルドする)</summary>
|
|
99
|
+
|
|
100
|
+
上の表にない環境では Python 実装が使われます。C++ 拡張を使うには、コンパイラを用意してから再インストールします。
|
|
101
|
+
|
|
102
|
+
| OS | コンパイラ |
|
|
103
|
+
|---|---|
|
|
104
|
+
| Windows | [Build Tools for Visual Studio](https://visualstudio.microsoft.com/ja/visual-cpp-build-tools/) の「C++ によるデスクトップ開発」 |
|
|
105
|
+
| Debian / Ubuntu | `sudo apt install build-essential python3-dev` |
|
|
106
|
+
| Fedora / RHEL | `sudo dnf install gcc-c++ python3-devel` |
|
|
107
|
+
| macOS | `xcode-select --install` |
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
EXPATJSON_REQUIRE_NATIVE=1 pip install --no-cache-dir --force-reinstall --no-binary expatjson expatjson
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
PowerShell の場合は、先に `$env:EXPATJSON_REQUIRE_NATIVE="1"` を実行してください。この環境変数を指定すると、
|
|
114
|
+
ビルドに失敗したときに Python 実装で代用せず、エラーで終了します。
|
|
115
|
+
|
|
116
|
+
</details>
|
|
117
|
+
|
|
118
|
+
<details>
|
|
119
|
+
<summary>PyPI を使わないインストール</summary>
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
# GitHub の最新版(コンパイラがない場合は Python 実装のみ)
|
|
123
|
+
pip install git+https://github.com/solitary-coffee/expatjson.git
|
|
124
|
+
|
|
125
|
+
# ダウンロードした wheel
|
|
126
|
+
pip install ./expatjson-<版>-<環境>.whl
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
</details>
|
|
130
|
+
|
|
131
|
+
## クイックスタート
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
import expatjson
|
|
135
|
+
|
|
136
|
+
doc = expatjson.parse('<book id="1"><title>珈琲</title><tag>a</tag><tag>b</tag></book>')
|
|
137
|
+
print(doc)
|
|
138
|
+
# {'book': {'@id': '1', 'title': '珈琲', 'tag': ['a', 'b']}}
|
|
139
|
+
|
|
140
|
+
print(expatjson.to_json('<a><b>1</b></a>'))
|
|
141
|
+
# {"a":{"b":"1"}}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## 使い方
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
# ファイル、gzip、Base64 でエンコードした gzip
|
|
148
|
+
doc = expatjson.parse_file("data.xml")
|
|
149
|
+
doc = expatjson.parse_gzip(payload)
|
|
150
|
+
doc = expatjson.parse_gzip_base64(text)
|
|
151
|
+
|
|
152
|
+
# 上限の変更(信頼できる入力に限る)
|
|
153
|
+
doc = expatjson.parse(xml, max_size=50 * 1024 * 1024, max_depth=500)
|
|
154
|
+
|
|
155
|
+
# 件数を気にせずループする
|
|
156
|
+
for tag in expatjson.as_list(expatjson.get_path(doc, "book", "tag")):
|
|
157
|
+
print(tag)
|
|
158
|
+
|
|
159
|
+
# 整形した JSON を出力
|
|
160
|
+
print(expatjson.to_json(xml, indent=2))
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
コマンドラインからも実行できます。
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
expatjson data.xml --indent 2 -o data.json
|
|
167
|
+
cat data.xml | python -m expatjson -
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## パーサーの指定
|
|
171
|
+
|
|
172
|
+
`backend` 引数で、使うパーサーを選べます。`to_json`、`parse_file`、`parse_gzip` でも指定できます。
|
|
173
|
+
|
|
174
|
+
| 値 | 動作 |
|
|
175
|
+
|---|---|
|
|
176
|
+
| `"auto"`(既定) | C++ 拡張があれば使い、なければ Python 実装を使う |
|
|
177
|
+
| `"native"` | 必ず C++ 拡張を使う。読み込めない場合は `BackendUnavailableError` |
|
|
178
|
+
| `"python"` | 必ず Python 実装を使う |
|
|
179
|
+
|
|
180
|
+
意図せず Python 実装で動くのを避けたいときは `"native"`、挙動を比べたいときや問題を切り分けたいときは
|
|
181
|
+
`"python"` を指定してください。
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
doc = expatjson.parse("<a>1</a>", backend="python")
|
|
185
|
+
|
|
186
|
+
try:
|
|
187
|
+
doc = expatjson.parse("<a>1</a>", backend="native")
|
|
188
|
+
except expatjson.BackendUnavailableError as error:
|
|
189
|
+
print(error, error.hint())
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
expatjson data.xml --backend native # C++ 拡張を使う
|
|
194
|
+
expatjson data.xml --backend python # Python 実装を使う
|
|
195
|
+
EXPATJSON_BACKEND=python python your_app.py # アプリ全体で C++ 拡張を読み込まない
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## 変換の規則
|
|
199
|
+
|
|
200
|
+
| XML | 結果 |
|
|
201
|
+
|---|---|
|
|
202
|
+
| `<a/>` | `{"a": None}` |
|
|
203
|
+
| `<a>text</a>` | `{"a": "text"}` |
|
|
204
|
+
| `<a x="1"/>` | `{"a": {"@x": "1"}}` |
|
|
205
|
+
| `<a x="1">text</a>` | `{"a": {"@x": "1", "#text": "text"}}` |
|
|
206
|
+
| `<a><b>1</b></a>` | `{"a": {"b": "1"}}` |
|
|
207
|
+
| `<a><b>1</b><b>2</b></a>` | `{"a": {"b": ["1", "2"]}}` |
|
|
208
|
+
|
|
209
|
+
- 値はすべて文字列です。数値や真偽値には変換しません。
|
|
210
|
+
- テキストの前後の空白は削除します。コメントと処理命令は無視します。
|
|
211
|
+
- 名前空間の接頭辞は、そのままキーに含まれます。
|
|
212
|
+
- テキストと子要素の順序は保持されません。
|
|
213
|
+
|
|
214
|
+
## 値になる場合とリストになる場合
|
|
215
|
+
|
|
216
|
+
同じ親の中で、ある名前の要素が **1 つなら値、2 つ以上ならリスト**になります。同じ形式の XML でも、件数によって
|
|
217
|
+
結果の型が変わるので注意してください。
|
|
218
|
+
|
|
219
|
+
```python
|
|
220
|
+
one = expatjson.parse("<list><item>A</item></list>")
|
|
221
|
+
print(one)
|
|
222
|
+
# {'list': {'item': 'A'}} 1 つの場合は文字列
|
|
223
|
+
|
|
224
|
+
two = expatjson.parse("<list><item>A</item><item>B</item></list>")
|
|
225
|
+
print(two)
|
|
226
|
+
# {'list': {'item': ['A', 'B']}} 2 つ以上はリスト
|
|
227
|
+
|
|
228
|
+
none = expatjson.parse("<list/>")
|
|
229
|
+
print(none)
|
|
230
|
+
# {'list': None} 0 件の場合は item のキーがない
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
属性や子要素を持つ場合も同じです。
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
print(expatjson.parse('<list><item id="1">A</item></list>'))
|
|
237
|
+
# {'list': {'item': {'@id': '1', '#text': 'A'}}}
|
|
238
|
+
|
|
239
|
+
print(expatjson.parse('<list><item id="1">A</item><item id="2">B</item></list>'))
|
|
240
|
+
# {'list': {'item': [{'@id': '1', '#text': 'A'}, {'@id': '2', '#text': 'B'}]}}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
間に別の要素があっても、同じ名前の要素は出現順に 1 つのリストにまとまります。
|
|
244
|
+
|
|
245
|
+
```python
|
|
246
|
+
print(expatjson.parse("<r><a>1</a><b>x</b><a>2</a></r>"))
|
|
247
|
+
# {'r': {'a': ['1', '2'], 'b': 'x'}}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### 件数によらず同じコードで扱う
|
|
251
|
+
|
|
252
|
+
`as_list` を通すと、0 件でも 1 件でも常にリストになります。`get_path` は途中のキーがなくても `KeyError` を
|
|
253
|
+
送出しません。
|
|
254
|
+
|
|
255
|
+
```python
|
|
256
|
+
for xml in ("<list/>", "<list><item>A</item></list>", "<list><item>A</item><item>B</item></list>"):
|
|
257
|
+
doc = expatjson.parse(xml)
|
|
258
|
+
items = expatjson.as_list(expatjson.get_path(doc, "list", "item"))
|
|
259
|
+
print(items)
|
|
260
|
+
# []
|
|
261
|
+
# ['A']
|
|
262
|
+
# ['A', 'B']
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## エラー処理
|
|
266
|
+
|
|
267
|
+
解析に失敗すると、`ExpatJsonError`(`ValueError` のサブクラス)またはその派生クラスが送出されます。
|
|
268
|
+
|
|
269
|
+
### 例外の階層
|
|
270
|
+
|
|
271
|
+
```text
|
|
272
|
+
ValueError
|
|
273
|
+
└── ExpatJsonError
|
|
274
|
+
├── XMLSyntaxError
|
|
275
|
+
│ └── EncodingError
|
|
276
|
+
├── SizeLimitError
|
|
277
|
+
├── DepthLimitError
|
|
278
|
+
├── ForbiddenXMLError
|
|
279
|
+
├── InputTypeError (TypeError でもある)
|
|
280
|
+
├── CompressionError
|
|
281
|
+
├── FileReadError (OSError でもある)
|
|
282
|
+
└── BackendUnavailableError (ImportError でもある)
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
引数の値が不正な場合(`backend="x"`、`max_depth=0`、`set_language("x")`)は、通常の `ValueError` です。
|
|
286
|
+
|
|
287
|
+
### 原因ごとの処理
|
|
288
|
+
|
|
289
|
+
`except` は、派生クラスから先に書いてください。
|
|
290
|
+
|
|
291
|
+
```python
|
|
292
|
+
def load(data):
|
|
293
|
+
try:
|
|
294
|
+
return expatjson.parse(data)
|
|
295
|
+
except expatjson.EncodingError as error:
|
|
296
|
+
print("文字コードのエラー:", error)
|
|
297
|
+
except expatjson.XMLSyntaxError as error:
|
|
298
|
+
print("構文エラー:", error.code, error.line, error.column)
|
|
299
|
+
except (expatjson.SizeLimitError, expatjson.DepthLimitError) as error:
|
|
300
|
+
print("上限超過:", error)
|
|
301
|
+
except expatjson.ForbiddenXMLError as error:
|
|
302
|
+
print("禁止されている宣言:", error)
|
|
303
|
+
except expatjson.ExpatJsonError as error:
|
|
304
|
+
print("その他のエラー:", error, error.hint())
|
|
305
|
+
return None
|
|
306
|
+
|
|
307
|
+
load("<a><b></a>")
|
|
308
|
+
load("<!DOCTYPE a><a/>")
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### エラーオブジェクトの内容
|
|
312
|
+
|
|
313
|
+
```python
|
|
314
|
+
try:
|
|
315
|
+
expatjson.parse("<a>\n <b></c>\n</a>")
|
|
316
|
+
except expatjson.ExpatJsonError as error:
|
|
317
|
+
print(error) # 開始タグと終了タグが対応していません(2 行目 8 文字目)
|
|
318
|
+
print(error.hint()) # タグは、開いた順と逆の順に閉じてください。
|
|
319
|
+
print(error.code) # mismatched_tag
|
|
320
|
+
print(error.line) # 2
|
|
321
|
+
print(error.column) # 8
|
|
322
|
+
print(error.byte_offset) # 11
|
|
323
|
+
print(error.snippet) # エラー位置の前後の抜粋
|
|
324
|
+
print(error.message("en"))
|
|
325
|
+
print(error.to_dict())
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
| 属性・メソッド | 内容 |
|
|
329
|
+
|---|---|
|
|
330
|
+
| `code` | エラーの種類を表す文字列。条件分岐にはメッセージではなくこの値を使ってください |
|
|
331
|
+
| `line` / `column` | 行番号と列番号(1 始まり)。位置がない場合は `None` |
|
|
332
|
+
| `byte_offset` | 先頭からのバイト位置(0 始まり)。位置がない場合は `None` |
|
|
333
|
+
| `snippet` | エラー位置の前後(最大 60 バイト)。位置がない場合は `None` |
|
|
334
|
+
| `detail` | 補足情報(上限値、文字コード名、型名など) |
|
|
335
|
+
| `message(lang)` | 位置を含むエラーメッセージ(`"ja"` または `"en"`) |
|
|
336
|
+
| `hint(lang)` | 対処方法 |
|
|
337
|
+
| `to_dict(lang, include_snippet=False)` | ログや API レスポンス向けの `dict` |
|
|
338
|
+
|
|
339
|
+
### エラーコード一覧
|
|
340
|
+
|
|
341
|
+
| `code` | 例外 | 発生する例 | 対処 |
|
|
342
|
+
|---|---|---|---|
|
|
343
|
+
| `empty` | `XMLSyntaxError` | 空の文字列 | 入力が空でないか確認する |
|
|
344
|
+
| `unclosed` | `XMLSyntaxError` | `<a>`(閉じる前に終わる) | 最後まで読み込めているか確認する |
|
|
345
|
+
| `mismatched_tag` | `XMLSyntaxError` | `<a><b></a></b>` | 開いた順と逆の順に閉じる |
|
|
346
|
+
| `duplicate_attribute` | `XMLSyntaxError` | `<a x="1" x="2"/>` | 重複した属性を削除する |
|
|
347
|
+
| `undefined_entity` | `XMLSyntaxError` | `<a> </a>` | 定義済みの実体参照か数値文字参照を使う |
|
|
348
|
+
| `junk_after_document` | `XMLSyntaxError` | `<a/><b/>` | ルート要素を 1 つにする |
|
|
349
|
+
| `invalid_character` | `XMLSyntaxError` | 制御文字や NUL を含む | 該当の文字を取り除く |
|
|
350
|
+
| `misplaced_declaration` | `XMLSyntaxError` | `<?xml` の前に空白がある | 先頭の空白を削除する |
|
|
351
|
+
| `syntax` | `XMLSyntaxError` | 上記以外の構文の誤り | `<`、`&`、引用符を確認する |
|
|
352
|
+
| `encoding` | `EncodingError` | 存在しない文字コード名を宣言している | UTF-8 にするか、`str` に変換して渡す |
|
|
353
|
+
| `size` | `SizeLimitError` | `max_size` を超えている | 信頼できる入力なら上限を上げる |
|
|
354
|
+
| `depth` | `DepthLimitError` | 階層が `max_depth` を超えている | 信頼できる入力なら上限を上げる |
|
|
355
|
+
| `forbidden` | `ForbiddenXMLError` | `<!DOCTYPE …>` や実体宣言がある | DTD を削除する(許可する設定はありません) |
|
|
356
|
+
| `input_type` | `InputTypeError` | `parse(None)` | `str` かバイト列を渡す |
|
|
357
|
+
| `compression` | `CompressionError` | 壊れた gzip や Base64 | データを最後まで受信できているか確認する |
|
|
358
|
+
| `file` | `FileReadError` | 存在しないファイル | パスと権限を確認する |
|
|
359
|
+
| `backend` | `BackendUnavailableError` | C++ 拡張がない環境で `backend="native"` | 再インストールするか `"auto"` にする |
|
|
360
|
+
|
|
361
|
+
### ログへの出力
|
|
362
|
+
|
|
363
|
+
`str(error)` と `to_dict()` に XML の内容は含まれません。`snippet` には内容の一部が含まれるため、個人情報などを
|
|
364
|
+
扱う場合はログに出さないでください。
|
|
365
|
+
|
|
366
|
+
```python
|
|
367
|
+
import json
|
|
368
|
+
import logging
|
|
369
|
+
|
|
370
|
+
try:
|
|
371
|
+
expatjson.parse("<a>")
|
|
372
|
+
except expatjson.ExpatJsonError as error:
|
|
373
|
+
logging.warning(json.dumps(error.to_dict(), ensure_ascii=False))
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
### メッセージの言語
|
|
377
|
+
|
|
378
|
+
既定は日本語です。英語にするには、`expatjson.set_language("en")`、環境変数 `EXPATJSON_LANG=en`、
|
|
379
|
+
コマンドラインの `--lang en` のいずれかを指定します。
|
|
380
|
+
|
|
381
|
+
C++ 拡張と Python 実装では、使う Expat のバージョンが異なる場合があります。そのため、一部の不正な入力では
|
|
382
|
+
`code` や列番号が一致しないことがあります。
|
|
383
|
+
|
|
384
|
+
## API リファレンス
|
|
385
|
+
|
|
386
|
+
| 関数 | 説明 |
|
|
387
|
+
|---|---|
|
|
388
|
+
| `parse(xml, *, max_size, max_depth, backend="auto")` | `str` やバイト列を `dict` に変換 |
|
|
389
|
+
| `to_json(xml, *, indent=None, ensure_ascii=False, ...)` | JSON 文字列に変換 |
|
|
390
|
+
| `parse_file(path, ...)` | ファイルを読み込んで変換 |
|
|
391
|
+
| `parse_gzip(data, ...)` | gzip を展開して変換(展開後のサイズも `max_size` で制限) |
|
|
392
|
+
| `parse_gzip_base64(data, ...)` | Base64 をデコードして `parse_gzip` を実行 |
|
|
393
|
+
| `backend()` | 既定で使われるパーサー(`"native"` または `"python"`) |
|
|
394
|
+
| `as_list(value)` | リストに変換(`None` は空リスト) |
|
|
395
|
+
| `get_path(doc, *keys, default=None)` | キーをたどって値を取得 |
|
|
396
|
+
| `set_language("ja" / "en")` | エラーメッセージの言語を設定 |
|
|
397
|
+
|
|
398
|
+
`max_size`(既定 20 MiB)、`max_depth`(既定 200)、`backend` は、すべての変換関数で指定できます。
|
|
399
|
+
|
|
400
|
+
| 環境変数 | 効果 |
|
|
401
|
+
|---|---|
|
|
402
|
+
| `EXPATJSON_BACKEND=python` | C++ 拡張を読み込まない |
|
|
403
|
+
| `EXPATJSON_LANG=en` | エラーメッセージを英語にする |
|
|
404
|
+
| `EXPATJSON_PURE=1` | C++ 拡張をビルドしない |
|
|
405
|
+
| `EXPATJSON_REQUIRE_NATIVE=1` | C++ 拡張のビルドに失敗したらエラーにする |
|
|
406
|
+
|
|
407
|
+
## 開発
|
|
408
|
+
|
|
409
|
+
不具合の報告や要望は [Issues](https://github.com/solitary-coffee/expatjson/issues) にお願いします。
|
|
410
|
+
|
|
411
|
+
```bash
|
|
412
|
+
git clone https://github.com/solitary-coffee/expatjson.git
|
|
413
|
+
cd expatjson
|
|
414
|
+
pip install -e .
|
|
415
|
+
python -m unittest discover -s tests -v
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
変更内容は [CHANGELOG](https://github.com/solitary-coffee/expatjson/blob/main/CHANGELOG.md) を参照してください。
|
|
419
|
+
|
|
420
|
+
## お問い合わせ
|
|
421
|
+
|
|
422
|
+
[Issues](https://github.com/solitary-coffee/expatjson/issues) か <kikukazu.kk0226@gmail.com> までご連絡ください。
|
|
423
|
+
脆弱性の報告は [SECURITY](https://github.com/solitary-coffee/expatjson/blob/main/SECURITY.md) の方法でお願いします。
|
|
424
|
+
|
|
425
|
+
## ライセンス
|
|
426
|
+
|
|
427
|
+
[MIT License](https://github.com/solitary-coffee/expatjson/blob/main/LICENSE) © 孤独のコーヒー
|
|
428
|
+
|
|
429
|
+
[Expat](https://libexpat.github.io/)(MIT License、`third_party/expat/COPYING`)を同梱しています。
|
|
430
|
+
C++ 拡張には [pybind11](https://github.com/pybind/pybind11)(BSD-3-Clause)を使用しています。
|