Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
Refactored json exporting implementations #23
Browse files Browse the repository at this point in the history
Signed-off-by: RaenonX <[email protected]>
  • Loading branch information
RaenonX committed Sep 20, 2021
1 parent 9151fec commit 471ecea
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
5 changes: 2 additions & 3 deletions dlasset/env/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import TYPE_CHECKING

from dlasset.enums import Locale
from dlasset.utils import export_json

if TYPE_CHECKING:
from dlasset.manifest import ManifestEntryBase
Expand Down Expand Up @@ -73,6 +74,4 @@ def update_index_files(self) -> None:
for locale, data in self._data.items():
file_path = self.get_index_file_path(locale)

with open(file_path, "w+", encoding="utf-8") as f:
# `separators` argument for minify
json.dump(data, f, separators=(",", ":"))
export_json(file_path, data, separators=(",", ":"))
7 changes: 3 additions & 4 deletions dlasset/export/functions/game_object.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Implementations to export ``GameObject`` and its component into a single script."""
import json
import os
from typing import TYPE_CHECKING

from dlasset.export.types import MonoBehaviourTree
from dlasset.log import log
from dlasset.utils import export_json

if TYPE_CHECKING:
from dlasset.export import ExportInfo
Expand Down Expand Up @@ -52,10 +52,9 @@ def export_game_object(export_info: "ExportInfo") -> None:

export_path: str = os.path.join(export_info.get_export_dir_of_obj(game_obj_info), f"{tree_name}.prefab.json")

with open(export_path, "w+", encoding="utf-8") as f:
f.write(json.dumps(tree_export, ensure_ascii=False, indent=2))
export_json(export_path, tree_export)

if idx % 200 == 0:
if idx % 50 == 0:
log(
"INFO",
f"{idx} / {len(game_obj_info_list)} ({idx / len(game_obj_info_list):.2%}) objects exported "
Expand Down
6 changes: 3 additions & 3 deletions dlasset/export/functions/monobehaviour.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Implementations to export ``MonoBehaviour``."""
import json
import os
from typing import TYPE_CHECKING

from dlasset.export.types import MonoBehaviourTree
from dlasset.log import log
from dlasset.utils import export_json

if TYPE_CHECKING:
from dlasset.export import ExportInfo
Expand Down Expand Up @@ -32,8 +32,8 @@ def export_mono_behaviour(export_info: "ExportInfo") -> list[MonoBehaviourTree]:
continue

tree = obj.read_typetree()
with open(export_path, "w+", encoding="utf-8") as f:
f.write(json.dumps(tree, ensure_ascii=False, indent=2))

export_json(export_path, tree)

trees.append(tree)

Expand Down
1 change: 1 addition & 0 deletions dlasset/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Various utility functions."""
from .execution import concurrent_run, concurrent_run_no_return, time_exec
from .export import export_json
from .image import crop_image, merge_y_cb_cr_a
52 changes: 52 additions & 0 deletions dlasset/utils/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Utility function for exporting."""
import json
from typing import Any, Optional

__all__ = ("export_json",)


def round_floats(obj: Any) -> Any:
"""Round the ``float`` in ``obj``."""
if isinstance(obj, float):
return float(format(obj, ".9g"))

if isinstance(obj, dict):
return {k: round_floats(v) for k, v in obj.items()}

if isinstance(obj, (list, tuple)):
return [round_floats(x) for x in obj]

return obj


def export_json(export_path: str, obj: Any, /, separators: Optional[tuple[str, str]] = None) -> None:
"""
Export the object ``obj`` to ``export_path``.
It was tested that the solutions below are slower:
>>> with open(export_path, "w+", encoding="utf-8") as f:
>>> f.write(json.dumps(
>>> json.loads(
>>> json.dumps(obj),
>>> parse_float=lambda x: f"{float(x):.9g}"
>>> )
>>> ))
The solution above was about on par.
>>> with open(export_path, "w+", encoding="utf-8") as f:
>>> json.dump(round_floats(obj), f)
The solution above is about 3x slower.
>>> with open(export_path, "w+", encoding="utf-8") as f:
>>> json.dump(
>>> json.loads(json.dumps(obj), parse_float=lambda x: f"{float(x):.9g}"),
>>> f,
>>> )
The solution above is about 3x slower.
"""
with open(export_path, "w+", encoding="utf-8") as f:
f.write(json.dumps(round_floats(obj), ensure_ascii=False, indent=2, separators=separators))

0 comments on commit 471ecea

Please sign in to comment.