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

Type configs #97

Merged
merged 13 commits into from
Sep 9, 2014
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: python
python:
- "2.6"
- "2.7"
install: "pip install . --use-mirrors"
script: "python setup.py test"
43 changes: 40 additions & 3 deletions jsonobject/api.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
from __future__ import absolute_import
from .base import JsonObjectBase, _LimitedDictInterfaceMixin
from .convert import STRING_CONVERSIONS

import decimal
import datetime

class JsonObject(JsonObjectBase, _LimitedDictInterfaceMixin):
from . import properties
import re


re_date = re.compile(r'^(\d{4})\D?(0[1-9]|1[0-2])\D?([12]\d|0[1-9]|3[01])$')
re_time = re.compile(
r'^([01]\d|2[0-3])\D?([0-5]\d)\D?([0-5]\d)?\D?(\d{3,6})?$')
re_datetime = re.compile(
r'^(\d{4})\D?(0[1-9]|1[0-2])\D?([12]\d|0[1-9]|3[01])'
r'(\D?([01]\d|2[0-3])\D?([0-5]\d)\D?([0-5]\d)?\D?(\d{3,6})?'
r'([zZ]|([\+-])([01]\d|2[0-3])\D?([0-5]\d)?)?)?$'
)
re_decimal = re.compile('^(\d+)\.(\d+)$')

_string_conversions = STRING_CONVERSIONS

class JsonObject(JsonObjectBase, _LimitedDictInterfaceMixin):
def __getstate__(self):
return self.to_json()

def __setstate__(self, dct):
self.__init__(dct)

class Meta(object):
properties = {
decimal.Decimal: properties.DecimalProperty,
datetime.datetime: properties.DateTimeProperty,
datetime.date: properties.DateProperty,
datetime.time: properties.TimeProperty,
str: properties.StringProperty,
unicode: properties.StringProperty,
bool: properties.BooleanProperty,
int: properties.IntegerProperty,
long: properties.IntegerProperty,
float: properties.FloatProperty,
list: properties.ListProperty,
dict: properties.DictProperty,
set: properties.SetProperty,
}
string_conversions = (
(re_date, datetime.date),
(re_time, datetime.time),
(re_datetime, datetime.datetime),
(re_decimal, decimal.Decimal),
)
Loading