Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enabling python3 #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.settings/
dist/
MANIFEST
build/
jsog3.egg-info/
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Empty file added jsog3/__init__.py
Empty file.
11 changes: 7 additions & 4 deletions jsog.py → jsog3/jsog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
33 changes: 25 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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.

)
12 changes: 11 additions & 1 deletion test_jsog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import jsog
from jsog3 import jsog
import unittest

class TestJSOG(unittest.TestCase):
Expand Down Expand Up @@ -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()