test_json_adapter.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import json
  12. import typing as t
  13. import warnings
  14. from datetime import timedelta
  15. from pathlib import Path
  16. import numpy
  17. from taipy.gui import Icon
  18. from taipy.gui._renderers.json import JsonAdapter, _TaipyJsonEncoder
  19. from taipy.gui.utils import _DoNotUpdate, _TaipyNumber
  20. def test_default_adapter():
  21. var = 123
  22. json_string = json.dumps(var, cls=_TaipyJsonEncoder)
  23. assert json_string == json.dumps(var)
  24. var = Icon("image.png", "Text")
  25. json_string = json.dumps(var, cls=_TaipyJsonEncoder)
  26. assert json_string == json.dumps({"path": "image.png", "text": "Text"})
  27. var = _TaipyNumber(123, "number")
  28. json_string = json.dumps(var, cls=_TaipyJsonEncoder)
  29. assert json_string == "123.0"
  30. var = Path("a", "b")
  31. json_string = json.dumps(var, cls=_TaipyJsonEncoder)
  32. json_string = json_string.replace("\\\\", "/")
  33. assert json_string == '"a/b"'
  34. var = timedelta(1.5)
  35. json_string = json.dumps(var, cls=_TaipyJsonEncoder)
  36. assert json_string == '"1 day, 12:00:00"'
  37. var = numpy.int32(123)
  38. json_string = json.dumps(var, cls=_TaipyJsonEncoder)
  39. assert json_string == "123"
  40. var = _DoNotUpdate()
  41. json_string = json.dumps(var, cls=_TaipyJsonEncoder)
  42. assert json_string == "null"
  43. def test_adapter_unknown(helpers):
  44. class TestClass:
  45. def __init__(self, value: str):
  46. self._value = value
  47. var = TestClass("test")
  48. with warnings.catch_warnings(record=True) as records:
  49. json_string = json.dumps(var, cls=_TaipyJsonEncoder)
  50. assert json_string == "null"
  51. warns = helpers.get_taipy_warnings(records)
  52. assert len(warns) == 1
  53. assert "TestClass is not JSON serializable" in str(warns[0].message)
  54. def test_custom_adapter():
  55. class TestClass:
  56. def __init__(self, value: str):
  57. self._value = TestClass.change_string(value)
  58. @staticmethod
  59. def change_string(s: str) -> str:
  60. return s[::-1]
  61. class TestAdapter(JsonAdapter):
  62. def parse(self, o) -> t.Optional[t.Any]:
  63. if isinstance(o, TestClass):
  64. return o._value
  65. return None
  66. TestAdapter().register()
  67. s = "abc"
  68. var = TestClass(s)
  69. json_string = json.dumps(var, cls=_TaipyJsonEncoder)
  70. assert json_string == json.dumps(TestClass.change_string(s))