Skip to content

Commit 7f8da5e

Browse files
committed
Added a RawlJSONEncoder class for easy json conversion. Issue #6
1 parent 11795a5 commit 7f8da5e

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ they were `dict`s, `object`s, or `list`s. For instance:
9393
`RawResult` should be suitable for serialization(pickling), but should you need
9494
to convert to a python type, use `RawResult.to_dict()` or `RawResult.to_list()`.
9595

96+
### JSON Encoding
97+
98+
In case you want to convert your query results directly to a JSON string for
99+
output, you can use RawlJSONEncoder.
100+
101+
json.dumps(StateModel().all(), cls=RawlJSONEncoder)
102+
96103
## Testing
97104

98105
Install the dependencies.

rawl/__init__.py

+17
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def get_name(self, pk):
5858
import warnings
5959
from enum import IntEnum
6060
from abc import ABC
61+
from json import JSONEncoder
62+
from datetime import datetime
6163
from psycopg2 import sql
6264
from psycopg2.pool import ThreadedConnectionPool
6365
from psycopg2.extensions import (
@@ -460,3 +462,18 @@ def all(self):
460462

461463
return self.select("SELECT {0} FROM " + self.table + ";",
462464
self.columns)
465+
466+
class RawlJSONEncoder(JSONEncoder):
467+
"""
468+
A JSON encoder that can be used with json.dumps
469+
470+
Usage
471+
-----
472+
json.dumps(cls=RawlJSONEncoder)
473+
"""
474+
def default(self, o):
475+
if type(o) == datetime:
476+
return o.isoformat()
477+
elif type(o) == RawlResult:
478+
return o.to_dict()
479+
return JSONEncoder.default(self, o)

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name = 'rawl',
8-
version = '0.2.2b1',
8+
version = '0.2.3b1',
99
description = 'An ugly raw SQL postgresql db layer',
1010
url = 'https://github.com/mikeshultz/rawl',
1111
author = 'Mike Shultz',

tests/test_rawl.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
import pytest
33
import logging
44
import pickle
5+
import json
56
from enum import IntEnum
67
from psycopg2 import connect
78
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
8-
from rawl import RawlBase, RawlConnection, RawlException, RawlResult
9+
from rawl import (
10+
RawlBase,
11+
RawlConnection,
12+
RawlException,
13+
RawlResult,
14+
RawlJSONEncoder
15+
)
916

1017
log = logging.getLogger(__name__)
1118

@@ -273,6 +280,27 @@ def test_serialization(self, pgdb):
273280
# Test that it can be unpickled
274281
assert type(pickle.loads(pickled_result)) == RawlResult
275282

283+
@pytest.mark.dependency(depends=['test_all', 'test_get_single_rawl'])
284+
def test_json_serialization(self, pgdb):
285+
"""
286+
Test that a RawlResult object can be serialized properly.
287+
"""
288+
289+
RAWL_ID = 1
290+
291+
mod = TheModel(RAWL_DSN)
292+
293+
result = mod.all()
294+
295+
try:
296+
json.dumps(result, cls=RawlJSONEncoder)
297+
assert True
298+
except TypeError as e:
299+
if 'serializable' in str(e):
300+
assert False
301+
else:
302+
raise e
303+
276304
@pytest.mark.dependency(depends=['test_all', 'test_get_single_rawl'])
277305
def test_select_with_columns(self, pgdb):
278306
"""

0 commit comments

Comments
 (0)