From 78fd4ae08c3092b0b4d39255d6301fb10dc2897c Mon Sep 17 00:00:00 2001 From: Robert Abram Date: Sat, 17 Jan 2026 15:22:05 -0600 Subject: [PATCH] Support casting to Dict --- LICENSE | 2 +- README.rst | 5 +++ pyproject.toml | 49 +++++++++++++++++++- setup.cfg | 2 - setup.py | 70 ----------------------------- src/python_easy_json/json_object.py | 7 +++ tests/test_simple_json.py | 9 ++++ 7 files changed, 69 insertions(+), 75 deletions(-) delete mode 100644 setup.cfg delete mode 100644 setup.py diff --git a/LICENSE b/LICENSE index 185f596..39d8aae 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 Robert Abram +Copyright (c) 2022-2026 Robert Abram Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.rst b/README.rst index 0885efe..f28401b 100644 --- a/README.rst +++ b/README.rst @@ -70,6 +70,11 @@ Data from a JSON String {"test_key": "test_value"} + data = dict(obj) + print(data) + + {"test_key": "test_value"} + Data from a python dictionary :: diff --git a/pyproject.toml b/pyproject.toml index 90f7b74..1a3acb2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,2 +1,47 @@ -line-length = 120 -target-version = ['py37', 'py38', 'py39', 'py310', 'py311', 'py312', 'py313', 'py314'] \ No newline at end of file +[build-system] +requires = ["setuptools >= 77.0.3"] +build-backend = "setuptools.build_meta" + +[project] +name="python-easy-json" +version="1.2.4" +python_requires=">=3.7" +license="MIT" +description="A simple, yet powerful, JSON/python dictionary to object deserialization" +authors = [ + { name = "Robert Abram", email = "rabram991@gmail.com" }, +] +dependencies = [ "dateutils", ] +requires-python = ">= 3.9" +readme="README.rst" +classifiers=[ + "Development Status :: 5 - Production/Stable", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", +] +keywords=[ + "serialization", + "rest", + "json", + "api", + "marshal", + "marshalling", + "deserialization", + "schema", + "model", + "models", + "data" +] + +[project.urls] +Homepage = "https://github.com/robabram/python-easy-json" +Repository = "https://github.com/robabram/python-easy-json" +Changelog = "https://github.com/robabram/python-easy-json" +Issues = "https://github.com/robabram/python-easy-json/issues" diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 8183238..0000000 --- a/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[metadata] -license_files = LICENSE diff --git a/setup.py b/setup.py deleted file mode 100644 index 9768705..0000000 --- a/setup.py +++ /dev/null @@ -1,70 +0,0 @@ -# -# This file is subject to the terms and conditions defined in the -# file 'LICENSE', which is part of this source code package. -# -import os - -from setuptools import setup, find_packages - -__VERSION__ = "1.2.2" - -base_dir = os.path.abspath(os.path.dirname(__file__)) - -with open(os.path.join(base_dir, "README.rst")) as readme: - readme_contents = readme.read() - -with open("requirements.txt") as requirements: - requirements_list = [] - for r in requirements.readlines(): - cleaned = r.split('#')[0].strip() - if cleaned: - requirements_list.append(cleaned) - -setup( - # This is what people 'pip install'. - name="python-easy-json", - version=__VERSION__, - description=( - "A simple, yet powerful, JSON/python dictionary to object deserialization" - ), - author="Robert Abram", - author_email="rabram991@gmail.com", - long_description=readme_contents, - url="https://github.com/robabram/python-easy-json", - packages=find_packages("src", exclude=("tests*", "examples")), - package_dir={"": "src"}, - license="MIT", - install_requires=requirements_list, - zip_safe=False, - keywords=[ - "serialization", - "rest", - "json", - "api", - "marshal", - "marshalling", - "deserialization", - "schema", - "model", - "models", - "data" - ], - python_requires=">=3.7", - classifiers=[ - "Development Status :: 5 - Production/Stable", - "Intended Audience :: Developers", - "License :: OSI Approved :: MIT License", - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", - "Programming Language :: Python :: 3.12", - "Programming Language :: Python :: 3.13", - "Programming Language :: Python :: 3.14", - ], - test_suite="tests", - project_urls={ - "Changelog": "https://pypi.python.org/pypi/python-easy-json", - "Issues": "https://github.com/robabram/python-easy-json/issues" - }, -) \ No newline at end of file diff --git a/src/python_easy_json/json_object.py b/src/python_easy_json/json_object.py index 63e83a1..8193029 100644 --- a/src/python_easy_json/json_object.py +++ b/src/python_easy_json/json_object.py @@ -307,3 +307,10 @@ def update(self, *args, **kwargs) -> "JSONObject": setattr(self, k, v) return self + + def __iter__(self): + """ Allow casting to Dict, IE: dict({JSONObject instance}) """ + data = self.to_dict(dates_to_str=True) + if data: + for k, v in data.items(): + yield k, v diff --git a/tests/test_simple_json.py b/tests/test_simple_json.py index 6fdb6bb..c889f1f 100644 --- a/tests/test_simple_json.py +++ b/tests/test_simple_json.py @@ -210,3 +210,12 @@ def test_add_invalid_operand(self): pass self.assertFalse(hasattr(obj, 'other_prop')) + + def test_cast_to_dict(self): + """ Test cast a JSONObject to a dict """ + obj = JSONObject({'test_prop': 123}) + self.assertIsInstance(obj, JSONObject) + + dict1 = dict(obj) + dict2 = obj.to_dict(dates_to_str=True) + self.assertEqual(dict1, dict2)