diff --git a/gluon/dal.py b/gluon/dal.py index 2c68a8d2..73783a45 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -6781,7 +6781,7 @@ def sqlhtml_validators(field): if field_type in (('string', 'text', 'password')): requires.append(validators.IS_LENGTH(field_length)) elif field_type == 'json': - requires.append(validators.IS_EMPTY_OR(validators.IS_JSON())) + requires.append(validators.IS_EMPTY_OR(validators.IS_JSON(native_json=field.db._adapter.native_json))) elif field_type == 'double' or field_type == 'float': requires.append(validators.IS_FLOAT_IN_RANGE(-1e100, 1e100)) elif field_type in ('integer','bigint'): diff --git a/gluon/validators.py b/gluon/validators.py index 898e197b..b7122633 100644 --- a/gluon/validators.py +++ b/gluon/validators.py @@ -346,13 +346,17 @@ class IS_JSON(Validator): ('spam1234', 'invalid json') """ - def __init__(self, error_message='invalid json'): + def __init__(self, error_message='invalid json', native_json=False): + self.native_json = native_json self.error_message = error_message def __call__(self, value): if value is None: return None try: + if self.native_json: + simplejson.dumps(value) # raises error in case of malformed json + return (value, None) # the serialized value is not passed return (simplejson.loads(value), None) except JSONErrors: return (value, translate(self.error_message))