Skip to content

Commit

Permalink
Merge pull request #2 from den4uk/dev
Browse files Browse the repository at this point in the history
deployments pipeline and iso8601 format added
  • Loading branch information
den4uk committed Jun 16, 2019
2 parents 0d06aaf + 49beebf commit e77652a
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ htmlcov
.coverage
.tox
push.sh
coverage.xml
19 changes: 14 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ python:
- "3.7"
install:
- pip install pip -U
- pip install pytest
- pip install coverage
- pip install pytest-cov
- pip install coveralls
- pip install -r requirements.txt
- pip install -r requirements-dev.txt
script:
- flake8 --ignore=E501 --statistics jsonextra/
- pytest --cov=jsonextra tests/
after_success:
- coveralls
- codecov
deploy:
provider: pypi
user: denco
password:
secure: hNOvn3E5OOlXu4qeGi5Ge9gPzOVzVdtPvSXLWtS6nG1X2cFAxpijuyaAvJyEa7ebpGmd1Rf1ri6QU3oQCGmKH+xogkRce3roPHxd/DzEx6S8GbQEfhFp1xTV5Qd5R08J1/oOuRlQ8gzKTmvtYn2G6kM8oXPIMfcVhuDvG9JUdxjT2YimhJ/p3slys4ezn/EK5KqFFcI8p3vf/180/SKRUKlUO6WSHf2VHNFjKYwGgvgojUQT4hc4Xk76t4cXsSdciBsAiu+9u7cd6vNyRh72B8YgMwHKO7v46+5ViCZrxSOiLTe5UCYq9csCMDiTbXDq8Pg1nOpHjJEhv08TL8r2WXnqxQLbaBpWX1V+6bva438r4KwcYG8hQBsAKqub6+Qzn6gBYqj036MlF/7IP0QsqXRumOgv+Y34JCrBR8RSA2xqJxxQ42LHD3T3OrF0hmeQ+B5W0iipOJ0VdLRlCndHCHSAa2WOtH5MuCV7NWfXX4O+uxKu7HUjtnI546v6i1oXf3iySDpoeixJm6H4n82g5f3R/TGA0icPGfsVXmdynZDiMMwatrKc+vu/DJ4WPu8mo5U+OMYah07WdURfq9zkyC8ZWSgGb+L2wiPh+gbNcCINZIGmV1eDXjrfMZ5jmJDQkMxCyX8PANqRlxmYJuQ5gyHrz+uQt85JHqgTil3G/rg=
skip_existing: true
distributions: "bdist_wheel"
on:
branch: master
tags: true
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
JSON Extra
jsonextra
=====
[![Build Status](https://travis-ci.org/den4uk/jsonextra.svg?branch=master)](https://travis-ci.org/den4uk/jsonextra)
[![Coverage Status](https://coveralls.io/repos/github/den4uk/jsonextra/badge.svg?branch=master)](https://coveralls.io/github/den4uk/jsonextra?branch=master)
[![PyPI version](https://badge.fury.io/py/jsonextra.svg)](https://badge.fury.io/py/jsonextra)
[![Codecov](https://codecov.io/gh/den4uk/jsonextra/branch/master/graph/badge.svg)](https://codecov.io/gh/den4uk/jsonextra)
[![PyPI Version](http://img.shields.io/pypi/v/jsonextra.svg)](https://pypi.python.org/pypi/jsonextra)
[![License](http://img.shields.io/pypi/l/jsonextra.svg)](https://pypi.python.org/pypi/jsonextra)

_is same as `json` library but with extra support for `uuid` and `datetime` data classes_

## Installation

Expand All @@ -13,26 +16,37 @@ $ pip install jsonextra

## Usage

Use just like `json` as normal once imported
Use just like `json` as normal once imported, but with addition of extra data classes.

```python
import uuid, datetime # for creation of `my_data` object
import jsonextra as json
import jsonextra

my_data = {'id': uuid.uuid4(), 'created': datetime.date.today()}
# my_data --> {'id': uuid.UUID('5f7660c5-88ea-46b6-93e2-860d5b7a0271'), 'created': datetime.date(2019, 6, 16)}

# Serializes the key values to stringified versions
my_json = json.dumps(my_data)
my_json = jsonextra.dumps(my_data)
# my_json --> '{"id": "5f7660c5-88ea-46b6-93e2-860d5b7a0271", "created": "2019-06-16"}'

# Deserializes the object and confirms the output matches `my_data`
assert json.loads(my_json) == my_data # True
assert jsonextra.loads(my_json) == my_data # True
```


## Extra supported data types
##### `.dump(obj, fp, **kwargs)` & `.dumps(obj, **kwargs)`
Will seriaze extra data classes into their string representations (`__str__`).
Note: for _datetime_ objects, the `__str__` is dumped to an ISO8601-like format: `yyyy-mm-dd HH:MM:SS`, and it is the same format that will be expected by `.loads` method.


##### `.load(fp, **kwargs)` & `.loads(s, **kwargs)`
Will deseriaze any stings, which match patterns of extra supported data classes. For example, if something looks like a _uuid_ - it will be converted to `uuid.UUID`.
If this behaviour is undesired, please use built-in `json.loads` method instead of `jsonextra.loads`.



## Supported extra data classes

- datetime.date
- datetime.datetime
- uuid.UUID
- `datetime.date`
- `datetime.datetime`
- `uuid.UUID`
6 changes: 4 additions & 2 deletions jsonextra/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .json_extra import *

__license__ = 'MIT'
__project__ = 'jsonextra'
__version__ = '0.2.0'

from .json_extra import * # noqa
2 changes: 1 addition & 1 deletion jsonextra/json_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import dateutil.parser

uuid_rex = re.compile(r'^[0-9a-f]{8}\-?[0-9a-f]{4}\-?4[0-9a-f]{3}\-?[89ab][0-9a-f]{3}\-?[0-9a-f]{12}$', re.I)
datetime_rex = re.compile(r'^\d{4}\-[01]\d\-[0-3]\d [0-2]\d\:[0-5]\d\:[0-5]\d')
datetime_rex = re.compile(r'^\d{4}\-[01]\d\-[0-3]\d[\sT][0-2]\d\:[0-5]\d\:[0-5]\d')
date_rex = re.compile(r'^\d{4}\-[01]\d\-[0-3]\d$')


Expand Down
8 changes: 8 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-r requirements.txt
pytest
coverage
pytest-cov
coveralls
codecov
flake8
bumpversion
10 changes: 10 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[bumpversion]
commit = True
current_version = 0.2.0
files = jsonextra/__init__.py
tag = True
tag_name = {new_version}


[wheel]
universal = 1
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
author_email='den@saz.lt',
packages=['jsonextra'],
license='MIT License',
keywords="json uuid datetime date".split(),
install_requires=install_requires,
include_package_data=True,
long_description=long_description,
Expand Down
2 changes: 2 additions & 0 deletions tests/test_jsonextra.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def test_loads_many(py_obj, json_obj):


odd_cases = [
({'x': datetime.datetime(2019, 6, 16, 13, 31, 37)}, '{"x": "2019-06-16T13:31:37"}'), # Uses ISO8601 as input
({'x': datetime.datetime(2019, 6, 16, 13, 31, 37, 6399)}, '{"x": "2019-06-16T13:31:37.006399"}'), # Uses ISO8601 as input
({'x': '2020-12-32'}, '{"x": "2020-12-32"}'), # Incorrect date
({'x': '2019-13-01 25:64:02'}, '{"x": "2019-13-01 25:64:02"}'), # Incorrect date/time
({'x': '00000000-0000-0000-0000-000000000000'}, '{"x": "00000000-0000-0000-0000-000000000000"}'), # Not correctly structured guid
Expand Down
8 changes: 3 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ envlist = py3

[testenv]
deps =
pytest
pytest-cov
coverage
-rrequirements.txt
-rrequirements-dev.txt
commands =
pytest --cov jsonextra tests/
flake8 --ignore=E501 --statistics jsonextra/
pytest --cov=jsonextra tests/
coverage html

[testenv:py3]
Expand Down

0 comments on commit e77652a

Please sign in to comment.