-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update setup.py * Update __init__.py * add `datetime` example * add keywords * test not serializable
- Loading branch information
Showing
5 changed files
with
259 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"# Write a custom Converter\n", | ||
"\n", | ||
"In this Example we will write a custom converter for `datetime` objects." | ||
], | ||
"metadata": { | ||
"collapsed": false, | ||
"pycharm": { | ||
"name": "#%% md\n" | ||
} | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"outputs": [], | ||
"source": [ | ||
"import json\n", | ||
"\n", | ||
"from znjson import ConverterBase\n", | ||
"from datetime import datetime\n", | ||
"import znjson" | ||
], | ||
"metadata": { | ||
"collapsed": false, | ||
"pycharm": { | ||
"name": "#%%\n" | ||
} | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"2022-03-11 10:02:18.048121\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"dt = datetime.now()\n", | ||
"print(dt)" | ||
], | ||
"metadata": { | ||
"collapsed": false, | ||
"pycharm": { | ||
"name": "#%%\n" | ||
} | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"outputs": [], | ||
"source": [ | ||
"class DatetimeConverter(ConverterBase):\n", | ||
" \"\"\"Encode/Decode datetime objects\n", | ||
"\n", | ||
" Attributes\n", | ||
" ----------\n", | ||
" level: int\n", | ||
" Priority of this converter over others.\n", | ||
" A higher level will be used first, if there\n", | ||
" are multiple converters available\n", | ||
" representation: str\n", | ||
" An unique identifier for this converter.\n", | ||
" instance:\n", | ||
" Used to select the correct converter.\n", | ||
" This should fulfill isinstance(other, self.instance)\n", | ||
" or __eq__ should be overwritten.\n", | ||
" \"\"\"\n", | ||
" level = 100\n", | ||
" representation = \"datetime\"\n", | ||
" instance = datetime\n", | ||
"\n", | ||
" def _encode(self, obj: datetime) -> str:\n", | ||
" \"\"\"Convert the datetime object to str / isoformat\"\"\"\n", | ||
" return obj.isoformat()\n", | ||
" def _decode(self, value: str) -> datetime:\n", | ||
" \"\"\"Create datetime object from str / isoformat\"\"\"\n", | ||
" return datetime.fromisoformat(value)" | ||
], | ||
"metadata": { | ||
"collapsed": false, | ||
"pycharm": { | ||
"name": "#%%\n" | ||
} | ||
} | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"To use the new converter we have to add it to `znjson.config.ACTIVE_CONVERTERS` via `znjson.register()`" | ||
], | ||
"metadata": { | ||
"collapsed": false, | ||
"pycharm": { | ||
"name": "#%% md\n" | ||
} | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"outputs": [], | ||
"source": [ | ||
"znjson.register(DatetimeConverter)" | ||
], | ||
"metadata": { | ||
"collapsed": false, | ||
"pycharm": { | ||
"name": "#%%\n" | ||
} | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"{\n", | ||
" \"_type\": \"datetime\",\n", | ||
" \"value\": \"2022-03-11T10:02:18.048121\"\n", | ||
"}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"json_string = json.dumps(dt, cls=znjson.ZnEncoder, indent=4)\n", | ||
"print(json_string)" | ||
], | ||
"metadata": { | ||
"collapsed": false, | ||
"pycharm": { | ||
"name": "#%%\n" | ||
} | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": "datetime.datetime(2022, 3, 11, 10, 2, 18, 48121)" | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"json.loads(json_string, cls=znjson.ZnDecoder)" | ||
], | ||
"metadata": { | ||
"collapsed": false, | ||
"pycharm": { | ||
"name": "#%%\n" | ||
} | ||
} | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters