diff --git a/.gitignore b/.gitignore index 2bed549..d20408c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ .settings/ dist/ MANIFEST +build/ +jsog3.egg-info/ diff --git a/README.md b/README.md index cbfd19e..10a112f 100644 --- a/README.md +++ b/README.md @@ -1,36 +1,37 @@ # JavaScript Object Graphs with Python -This Python module serializes and deserializes cyclic object graphs in the [JSOG format](https://github.com/jsog/jsog). +This Python module serializes and deserializes cyclic object graphs in the [JSOG format](https://github.com/simoneggler/jsog-python). ## Source code -The official repository is (https://github.com/jsog/jsog-python). +The official repository is (https://github.com/simoneggler/jsog-python) which is a fork of (https://github.com/jsog/jsog-python). ## Download Jsog is available in PyPI: - $ pip install jsog + $ pip install jsog3 ## Usage This code mimics the standard *json* python package: - import jsog + from jsog3 import jsog string = jsog.dumps(cyclicGraph); cyclicGraph = jsog.loads(string); It can be used to convert between object graphs directly: - import jsog + from jsog3 import jsog jsogStructure = jsog.encode(cyclicGraph); // has { '@ref': 'ID' } links instead of cycles cyclicGraph = jsog.decode(jsogStructure); -## Author +## Authors * Jeff Schnitzer (jeff@infohazard.org) +* Simon Eggler (simon.eggler@gmx.net) ## License diff --git a/jsog3/__init__.py b/jsog3/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/jsog.py b/jsog3/jsog.py similarity index 92% rename from jsog.py rename to jsog3/jsog.py index ead2a66..7ef0f71 100644 --- a/jsog.py +++ b/jsog3/jsog.py @@ -39,7 +39,7 @@ def encodeObject(original): result = sofar[originalId] = { '@id': originalId } - for key, value in original.iteritems(): + for key, value in original.items(): result[key] = doEncode(value) return result @@ -78,7 +78,7 @@ def firstPassDecodeObject(encoded): if '@id' in encoded: found[encoded['@id']] = result - for key, value in encoded.iteritems(): + for key, value in encoded.items(): if key != '@id': result[key] = firstPassDecode(value) @@ -96,14 +96,17 @@ def firstPassDecodeArray(encoded): def deref(withRefs): if isinstance(withRefs, dict): - for key, value in withRefs.iteritems(): + for key, value in withRefs.items(): if isinstance(value, dict) and '@ref' in value: withRefs[key] = found[value['@ref']] else: deref(value) elif isinstance(withRefs, list): for value in withRefs: - deref(value) + if isinstance(value, dict) and '@ref' in value: + withRefs[withRefs.index(value)] = found[value['@ref']] + else: + deref(value) firstPass = firstPassDecode(encoded) deref(firstPass) diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..224a779 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[metadata] +description-file = README.md \ No newline at end of file diff --git a/setup.py b/setup.py index d450370..87ec9f1 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,13 @@ -from distutils.core import setup + + +from setuptools import setup, find_packages setup( - name='jsog', - version='1.0.1', - author='Jeff Schnitzer', - author_email='jeff@infohazard.org', - url='https://github.com/jsog/jsog-python', + name='jsog3', + version='2.0.4', + packages=['jsog3'], + author='Simon Eggler', + author_email='simon.eggler@gmx.net', + url='https://github.com/simoneggler/jsog-python', description='JSOG serializer and deserializer', keywords='jsog json', license='MIT License', @@ -13,7 +16,21 @@ 'Intended Audience :: Developers', 'License :: OSI Approved :: MIT License', 'Operating System :: OS Independent', - 'Programming Language :: Python :: 2' + 'Programming Language :: Python :: 3' ], - py_modules=['jsog'] + + package_data={ + # If any package contains *.txt or *.rst files, include them: + '': ['*.txt', '*.md'], + }, + + # metadata for upload to PyPI + project_urls={ + "Bug Tracker": "https://github.com/simoneggler/jsog-python/issuespython", + "Documentation": "https://github.com/simoneggler/jsog-python/wiki", + "Source Code": "https://github.com/simoneggler/jsog-python", + } + + # could also include long_description, download_url, classifiers, etc. + ) diff --git a/test_jsog.py b/test_jsog.py index a3ada1e..126085c 100644 --- a/test_jsog.py +++ b/test_jsog.py @@ -1,4 +1,4 @@ -import jsog +from jsog3 import jsog import unittest class TestJSOG(unittest.TestCase): @@ -60,5 +60,15 @@ def test_decode_plain_json(self): decoded = jsog.decode(json) self.assertEqual(json, decoded) + def test_decode_list_reference(self): + JSOGIFIED = '{"@id":"1","foo":"foo","inner1":{"@id":"2","bar":"bar"},"inner2":[{"@ref":"2"}]}' + parsed = jsog.loads(JSOGIFIED) + + inner1 = parsed['inner1'] + inner2 = parsed['inner2'][0] + self.assertTrue(inner1 is inner2) + + + if __name__ == '__main__': unittest.main() \ No newline at end of file