Skip to content

Commit a2343a8

Browse files
authored
Merge pull request #265 from mathsman5133/orjson
Switch to orjson
2 parents 98630ae + dd2068a commit a2343a8

File tree

9 files changed

+38
-38
lines changed

9 files changed

+38
-38
lines changed

coc/abc.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
"""
24-
import ujson
24+
import orjson
2525
from pathlib import Path
2626
from typing import AsyncIterator, Any, Dict, Type, Optional, TYPE_CHECKING
2727

@@ -223,8 +223,8 @@ def _load_json_meta(cls, json_meta: dict, id, name: str, lab_to_townhall):
223223
production_building = "Pet Shop"
224224

225225
# load buildings
226-
with open(BUILDING_FILE_PATH) as fp:
227-
buildings = ujson.load(fp)
226+
with open(BUILDING_FILE_PATH, 'rb') as fp:
227+
buildings = orjson.loads(fp.read())
228228

229229
# without production_building, it is a hero
230230
if not production_building:
@@ -346,8 +346,8 @@ def __init__(self):
346346
self.loaded = False
347347

348348
def _load_json(self, english_aliases, lab_to_townhall):
349-
with open(self.FILE_PATH) as fp:
350-
data = ujson.load(fp)
349+
with open(self.FILE_PATH, 'rb') as fp:
350+
data = orjson.loads(fp.read())
351351

352352
id = 2000
353353
for c, [supercell_name, meta] in enumerate(data.items()):

coc/client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from pathlib import Path
3030
from typing import AsyncIterator, Iterable, List, Optional, Type, Union, TYPE_CHECKING
3131

32-
import ujson
32+
import orjson
3333

3434
from .clans import Clan, RankedClan
3535
from .errors import Forbidden, GatewayError, NotFound, PrivateWarLog
@@ -350,11 +350,11 @@ def _create_client(self, email, password):
350350
)
351351

352352
def _load_holders(self):
353-
with open(ENGLISH_ALIAS_PATH) as fp:
354-
english_aliases = ujson.load(fp)
353+
with open(ENGLISH_ALIAS_PATH, 'rb') as fp:
354+
english_aliases = orjson.loads(fp.read())
355355

356-
with open(BUILDING_FILE_PATH) as fp:
357-
buildings = ujson.load(fp)
356+
with open(BUILDING_FILE_PATH, 'rb') as fp:
357+
buildings = orjson.loads(fp.read())
358358

359359
english_aliases = {
360360
v["TID"]: v.get("EN", None)

coc/hero.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ujson
1+
import orjson
22

33
from typing import TYPE_CHECKING, Dict, List, Optional, Type
44
from pathlib import Path
@@ -311,8 +311,8 @@ class EquipmentHolder(DataContainerHolder):
311311

312312
def _load_json(self, english_aliases, lab_to_townhall):
313313
id = 3000
314-
with open(EQUIPMENT_FILE_PATH) as fp:
315-
equipment_data = ujson.load(fp)
314+
with open(EQUIPMENT_FILE_PATH, 'rb') as fp:
315+
equipment_data = orjson.loads(fp.read())
316316

317317
for supercell_name, equipment_meta in equipment_data.items():
318318
if not equipment_meta.get("TID"):

coc/http.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from json import loads as json_loads
3636

3737
import aiohttp
38+
import orjson
3839

3940
from .errors import (
4041
HTTPException,
@@ -52,10 +53,10 @@
5253
stats_url_matcher = re.compile(r"%23[\da-zA-Z]+|\d{8,}|global")
5354

5455

55-
async def json_or_text(response):
56+
async def json_or_text(response: aiohttp.ClientResponse):
5657
"""Parses an aiohttp response into a the string or json response."""
5758
try:
58-
ret = await response.json()
59+
ret = await response.json(loads=orjson.loads)
5960
except aiohttp.ContentTypeError:
6061
ret = await response.text(encoding="utf-8")
6162

coc/spell.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ujson
1+
import orjson
22

33
from typing import Type, Dict, List
44
from pathlib import Path
@@ -127,10 +127,10 @@ class SpellHolder(DataContainerHolder):
127127
data_object = Spell
128128

129129
def _load_json(self, english_aliases, lab_to_townhall):
130-
with open(SPELLS_FILE_PATH) as fp:
131-
spell_data = ujson.load(fp)
132-
with open(ARMY_LINK_ID_FILE_PATH) as fp:
133-
army_link_ids = ujson.load(fp)
130+
with open(SPELLS_FILE_PATH, 'rb') as fp:
131+
spell_data = orjson.loads(fp.read())
132+
with open(ARMY_LINK_ID_FILE_PATH, 'rb') as fp:
133+
army_link_ids = orjson.loads(fp.read())
134134

135135
id = 2000 # fallback ID for non-standard spells
136136
for supercell_name, spell_meta in spell_data.items():

coc/troop.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Dict, List, Optional, Tuple, Type
22
from pathlib import Path
33

4-
import ujson
4+
import orjson
55

66
from .abc import DataContainer, DataContainerHolder
77
from .enums import Resource
@@ -168,12 +168,12 @@ class TroopHolder(DataContainerHolder):
168168
item_lookup: Dict[Tuple[str, bool], Type[Troop]]
169169

170170
def _load_json(self, english_aliases: dict, lab_to_townhall):
171-
with open(TROOPS_FILE_PATH) as fp:
172-
troop_data = ujson.load(fp)
173-
with open(SUPER_TROOPS_FILE_PATH) as fp:
174-
super_troop_data = ujson.load(fp)
175-
with open(ARMY_LINK_ID_FILE_PATH) as fp:
176-
army_link_ids: dict = ujson.load(fp)
171+
with open(TROOPS_FILE_PATH, 'rb') as fp:
172+
troop_data = orjson.loads(fp.read())
173+
with open(SUPER_TROOPS_FILE_PATH, 'rb') as fp:
174+
super_troop_data = orjson.loads(fp.read())
175+
with open(ARMY_LINK_ID_FILE_PATH, 'rb') as fp:
176+
army_link_ids: dict = orjson.loads(fp.read())
177177

178178
super_data: dict = {v["Replacement"]: v for item in super_troop_data.values() for v in item.values()}
179179

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
aiohttp
2-
ujson
3-
zstandard
2+
orjson
3+
zstandard

tests/test_capitalraidseasons.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import unittest
33
from pathlib import Path
44

5-
import ujson
5+
import orjson
66

77
from coc import RaidLogEntry, RaidClan, RaidAttack, RaidMember
88
from coc.entry_logs import RaidLog
@@ -11,9 +11,8 @@
1111

1212
tracemalloc.start()
1313

14-
with open(Path(__file__).parent.joinpath(Path("mockdata/clans/capitalraidseasons/CAPITALRAIDSEASON.json")),
15-
encoding="utf-8") as fp:
16-
MOCK_CAPITALRAIDSEASON = ujson.load(fp)
14+
with open(Path(__file__).parent.joinpath(Path("mockdata/clans/capitalraidseasons/CAPITALRAIDSEASON.json")),'rb') as fp:
15+
MOCK_CAPITALRAIDSEASON = orjson.loads(fp.read())
1716

1817

1918
class TestRaids(unittest.TestCase):

tests/test_clans.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from pathlib import Path
33

4-
import ujson
4+
import orjson
55

66
from coc.clans import Clan
77
from coc.miscmodels import Location, Label, BaseLeague, ChatLanguage, CapitalDistrict
@@ -11,11 +11,11 @@
1111

1212
tracemalloc.start()
1313

14-
with open(Path(__file__).parent.joinpath(Path("mockdata/clans/clans/CLAN.json")), encoding="utf-8") as fp:
15-
MOCK_CLAN = ujson.load(fp)
14+
with open(Path(__file__).parent.joinpath(Path("mockdata/clans/clans/CLAN.json")), 'rb') as fp:
15+
MOCK_CLAN = orjson.loads(fp.read())
1616

17-
with open(Path(__file__).parent.joinpath(Path("mockdata/clans/search/CLANS_FOUND.json")), encoding="utf-8") as fp:
18-
MOCK_SEARCH_CLAN = ujson.load(fp)
17+
with open(Path(__file__).parent.joinpath(Path("mockdata/clans/search/CLANS_FOUND.json")), 'rb') as fp:
18+
MOCK_SEARCH_CLAN = orjson.loads(fp.read())
1919

2020

2121
class TestClans(unittest.TestCase):

0 commit comments

Comments
 (0)