added json encoder and partially functioning json decoder
This commit is contained in:
parent
b9982c3a21
commit
7f9e5dbf65
3 changed files with 50 additions and 0 deletions
|
@ -88,6 +88,33 @@ def dumps(records):
|
||||||
fp.seek(0)
|
fp.seek(0)
|
||||||
return fp.read()
|
return fp.read()
|
||||||
|
|
||||||
|
def json_dumps(records):
|
||||||
|
import json
|
||||||
|
import model
|
||||||
|
import decimal
|
||||||
|
|
||||||
|
class JSONEncoder(json.JSONEncoder):
|
||||||
|
def default(self, o):
|
||||||
|
if hasattr(o, 'toJSON') and callable(getattr(o, 'toJSON')):
|
||||||
|
return o.toJSON()
|
||||||
|
|
||||||
|
elif isinstance(o, decimal.Decimal):
|
||||||
|
return str(o.quantize(decimal.Decimal('0.01')))
|
||||||
|
|
||||||
|
return super(JSONEncoder, self).default(o)
|
||||||
|
|
||||||
|
return json.dumps(records, cls=JSONEncoder, indent=2)
|
||||||
|
|
||||||
|
|
||||||
|
def json_loads(s, record_classes):
|
||||||
|
import json
|
||||||
|
|
||||||
|
def object_hook(o):
|
||||||
|
return {'object_hook':str(o)}
|
||||||
|
#def default(self, o):
|
||||||
|
# return super(JSONDecoder, self).default(o)
|
||||||
|
|
||||||
|
return json.loads(s, object_hook=object_hook)
|
||||||
|
|
||||||
# THIS WAS IN CONTROLLER, BUT UNLESS WE
|
# THIS WAS IN CONTROLLER, BUT UNLESS WE
|
||||||
# REALLY NEED A CONTROLLER CLASS, IT'S SIMPLER
|
# REALLY NEED A CONTROLLER CLASS, IT'S SIMPLER
|
||||||
|
@ -104,6 +131,7 @@ def validate_required_records(records):
|
||||||
while req_types:
|
while req_types:
|
||||||
req = req_types[0]
|
req = req_types[0]
|
||||||
if req not in types:
|
if req not in types:
|
||||||
|
from fields import ValidationError
|
||||||
raise ValidationError("Record set missing required record: %s" % req)
|
raise ValidationError("Record set missing required record: %s" % req)
|
||||||
else:
|
else:
|
||||||
req_types.remove(req)
|
req_types.remove(req)
|
||||||
|
|
|
@ -49,6 +49,19 @@ class Field(object):
|
||||||
def parse(self, s):
|
def parse(self, s):
|
||||||
self.value = s.strip()
|
self.value = s.strip()
|
||||||
|
|
||||||
|
def toJSON(self):
|
||||||
|
data = self.get_data()
|
||||||
|
|
||||||
|
return {
|
||||||
|
'__class__': self.__class__.__name__,
|
||||||
|
'name': self.name,
|
||||||
|
'maxLength': self.max_length,
|
||||||
|
'required': self.required,
|
||||||
|
'data': data,
|
||||||
|
'value': self._value,
|
||||||
|
'dataLength': len(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class TextField(Field):
|
class TextField(Field):
|
||||||
def validate(self):
|
def validate(self):
|
||||||
|
@ -146,6 +159,9 @@ class BooleanField(Field):
|
||||||
|
|
||||||
|
|
||||||
class MoneyField(Field):
|
class MoneyField(Field):
|
||||||
|
def __init__(self, name=None, max_length=0, required=False):
|
||||||
|
super(MoneyField, self).__init__(name=name, uppercase=False, max_length=max_length, required=required)
|
||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
if self.value == None and self.required:
|
if self.value == None and self.required:
|
||||||
raise ValidationError("value required", field=self)
|
raise ValidationError("value required", field=self)
|
||||||
|
|
|
@ -67,3 +67,9 @@ class Model(object):
|
||||||
field.read(fp)
|
field.read(fp)
|
||||||
|
|
||||||
|
|
||||||
|
def toJSON(self):
|
||||||
|
return {
|
||||||
|
'__class__': self.__class__.__name__,
|
||||||
|
'fields': self.get_sorted_fields()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue