Add simple JSONTextDict
data type for SQLAlchemy columns
for basic JSON awareness/conversion
This commit is contained in:
parent
20beaf0fc8
commit
95d55f49bf
|
@ -1,8 +1,8 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8; -*-
|
||||||
################################################################################
|
################################################################################
|
||||||
#
|
#
|
||||||
# Rattail -- Retail Software Framework
|
# Rattail -- Retail Software Framework
|
||||||
# Copyright © 2010-2017 Lance Edgar
|
# Copyright © 2010-2019 Lance Edgar
|
||||||
#
|
#
|
||||||
# This file is part of Rattail.
|
# This file is part of Rattail.
|
||||||
#
|
#
|
||||||
|
@ -24,7 +24,9 @@
|
||||||
Data Types
|
Data Types
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals, absolute_import
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
from sqlalchemy import types
|
from sqlalchemy import types
|
||||||
|
|
||||||
|
@ -47,3 +49,23 @@ class GPCType(types.TypeDecorator):
|
||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
return GPC(value)
|
return GPC(value)
|
||||||
|
|
||||||
|
|
||||||
|
class JSONTextDict(types.TypeDecorator):
|
||||||
|
"""
|
||||||
|
SQLAlchemy type engine for JSON data. Interprets "raw" text as JSON when
|
||||||
|
reading from DB, and writes JSON text back to DB.
|
||||||
|
|
||||||
|
Note that the Python value for a field with this type will appear as a dict.
|
||||||
|
"""
|
||||||
|
impl = types.Text
|
||||||
|
|
||||||
|
def process_bind_param(self, value, dialect):
|
||||||
|
if value is not None:
|
||||||
|
value = json.dumps(value)
|
||||||
|
return value
|
||||||
|
|
||||||
|
def process_result_value(self, value, dialect):
|
||||||
|
if value is not None:
|
||||||
|
value = json.loads(value)
|
||||||
|
return value
|
||||||
|
|
Loading…
Reference in a new issue