def load(self, source): getter = None def get_item_value(c, keys): for k in keys: v = source.get(k) if v is not None: return v return None if isinstance(source, list): def faa(c,k): if c >= len(source): print(source) return None return source[c] getter = faa #getter = lambda c,k: source[c] elif isinstance(source, dict): getter = get_item_value else: asdict_op = getattr(source, "asdict", None) if callable(asdict_op): source = source.asdict() getter = get_item_value else: raise ValueError('Input not recognized. It must be list,dict or have asdict method') for c, key in enumerate(self.fields.keys()): alt_keys = self.fields[key].get('source_names', []) value = getter(c, [key] + alt_keys) if value is None: self[key] = None elif (isinstance(value, str) or isinstance(value, text)) and value == '' and self.fields[key].get('psql_type') in ('text', 'bytea'): self[key] = None elif isinstance(value, str): self[key] = value elif isinstance(value, text): self[key] = value.encode('utf-8', 'ignore') elif isinstance(value, list): self[key] = _json.dumps(value, cls=TupleJSONEncoder) elif isinstance(value, dict): self[key] = _json.dumps(value, cls=TupleJSONEncoder) else: self[key] = value
def dumps(obj, **kwargs): _dump_arg_defaults(kwargs) encoding = kwargs.pop('encoding', None) rv = _json.dumps(obj, **kwargs) if encoding is not None and isinstance(rv, unicode): rv = rv.encode(encoding) return rv
def dumps(obj, **kwargs): """Serialize ``obj`` to a JSON formatted ``str`` by using the application's configured encoder (:attr:`~flask.Flask.json_encoder`) if there is an application on the stack. """ if current_app: kwargs.setdefault('cls', current_app.json_encoder) return _json.dumps(obj, **kwargs)
def dumps(obj, **kwargs): """Serialize ``obj`` to a JSON formatted ``str`` by using the application's configured encoder (:attr:`~flask.Flask.json_encoder`) if there is an application on the stack. This function can return ``unicode`` strings or ascii-only bytestrings by default which coerce into unicode strings automatically. That behavior by default is controlled by the ``JSON_AS_ASCII`` configuration variable and can be overriden by the simplejson ``ensure_ascii`` parameter. """ _dump_arg_defaults(kwargs) return _json.dumps(obj, **kwargs)
def dumps(obj, **kwargs): """Serialize ``obj`` to a JSON formatted ``str`` by using the application's configured encoder (:attr:`~theflasktest.Flask.json_encoder`) if there is an application on the stack. This function can return ``unicode`` strings or ascii-only bytestrings by default which coerce into unicode strings automatically. That behavior by default is controlled by the ``JSON_AS_ASCII`` configuration variable and can be overriden by the simplejson ``ensure_ascii`` parameter. """ _dump_arg_defaults(kwargs) encoding = kwargs.pop('encoding', None) rv = _json.dumps(obj, **kwargs) if encoding is not None and isinstance(rv, text_type): rv = rv.encode(encoding) return rv
def dumps(obj, **kwargs): """Serialize ``obj`` to a JSON formatted ``str`` by using the application's configured encoder (:attr:`~env.Flask.json_encoder`) if there is an application on the stack. This function can return ``unicode`` strings or ascii-only bytestrings by default which coerce into unicode strings automatically. That behavior by default is controlled by the ``JSON_AS_ASCII`` configuration variable and can be overriden by the simplejson ``ensure_ascii`` parameter. """ _dump_arg_defaults(kwargs) encoding = kwargs.pop('encoding', None) rv = _json.dumps(obj, **kwargs) if encoding is not None and isinstance(rv, text_type): rv = rv.encode(encoding) return rv
from ._compat import text_type, PY2 from werkzeug.http import http_date from jinja2 import Markup # Use the same json implementation as itsdangerous on which we # depend anyways. try: from itsdangerous import simplejson as _json except ImportError: from itsdangerous import json as _json # Figure out if simplejson escapes slashes. This behavior was changed # from one version to another without reason. _slash_escape = '\\/' not in _json.dumps('/') __all__ = ['dump', 'dumps', 'load', 'loads', 'htmlsafe_dump', 'htmlsafe_dumps', 'JSONDecoder', 'JSONEncoder', 'jsonify'] def _wrap_reader_for_text(fp, encoding): if isinstance(fp.read(0), bytes): fp = io.TextIOWrapper(io.BufferedReader(fp), encoding) return fp def _wrap_writer_for_text(fp, encoding): try:
from .globals import current_app, request from ._compat import text_type, PY2 from werkzeug.http import http_date # Use the same json implementation as itsdangerous on which we # depend anyways. try: from itsdangerous import simplejson as _json except ImportError: from itsdangerous import json as _json # figure out if simplejson escapes slashes. This behavior was changed # from one version to another without reason. _slash_escape = "\\/" not in _json.dumps("/") __all__ = ["dump", "dumps", "load", "loads", "htmlsafe_dump", "htmlsafe_dumps", "JSONDecoder", "JSONEncoder", "jsonify"] def _wrap_reader_for_text(fp, encoding): if isinstance(fp.read(0), bytes): fp = io.TextIOWrapper(io.BufferedReader(fp), encoding) return fp def _wrap_writer_for_text(fp, encoding): try: fp.write("") except TypeError:
def dumps(obj, **kwargs): _dump_arg_defaults(kwargs) return _json.dumps(obj, **kwargs)
def asjson(self): return _json.dumps(self.asdict(), cls=TupleJSONEncoder)