def decodeBSONEntity(di, key): """ Inspired by pymongo bson.json_util.default, but specially processing some value types: ObjectId --> hexvalue datetime --> rfc3339 Also, while json_util.default creates a new dict in the form {$name: decodedvalue} we assign the decoded value, 'flattening' the value directly in the field. Fallback to other values using json_util.default, and flattening only those decoded entities that has only one key. """ value = di[key] if isinstance(value, ObjectId): di[key] = str(value) return if isinstance(value, datetime): di[key] = rfc3339(value, utc=True, use_system_timezone=False) return try: decoded = json_util.default(di[key]) if len(decoded.keys()) == 1: di[key] = decoded[decoded.keys()[0]] else: di[key] = decoded except: pass
def get_trucks(): try: trucks = list(db.trucks.find({})) if trucks: return Response(json.dumps( { "message": "{} trucks".format(len(trucks)), "data": trucks }, default=json_util.default), status=200, mimetype="application/json") else: return Response(json.dumps({"message": "No trucks"}), status=400, mimetype='application/json') except Exception as e: return Response(json.dumps( { "message": "Couldn't retrieve trucks", "error": e }, default=json_util.default()), status=404, mimetype='application/json')
def default(cls, obj): try: default = cls.__defaults__.get(type(obj)) except AttributeError: from network import NSMacAddress, NSIpAddress, NSSensorId, NSLocalUnitId cls.__defaults__ = { NSMacAddress: lambda o: { '$mac': str(o) }, NSIpAddress: lambda o: { '$ip': str(o) }, NSSensorId: lambda o: { '$sid': str(o) }, NSLocalUnitId: lambda o: { '$luid': str(o) }, uuid.UUID: lambda o: { "$uuid": o.hex }, datetime.datetime: cls.__datetime_default__, } default = cls.__defaults__.get(type(obj)) if default: return default(obj) try: from bson import json_util return json_util.default(obj) except: raise TypeError("%r is not JSON serializable" % obj)
def our_default_encoder(obj): """Override the json encoder that comes with pymongo (bson) So that datetime objects are encoded as ISO-8601""" if isinstance(obj, datetime): return time.mktime(obj.timetuple()) return json_util.default(obj)
def convert_to_json_serializable(self, value): if isinstance(value, (np.int, np.int32, np.int64)): return int(value) if isinstance(value, (np.float, np.float32, np.float64)): return float(value) else: return json_util.default(value)
def default(obj): if isinstance(obj,matrix): obj = { "$matrix": obj.T.tolist() } else: obj = json_util.default(obj) return obj
def default_date(obj): try: return json_util.default(obj) except TypeError: if isinstance(obj, date): return {"$date_only": obj.isoformat()} raise TypeError("%r is not JSON serializable" % obj)
def default(self, obj): if isinstance(obj, date): serializable = obj.isoformat() elif isinstance(obj, Decimal): serializable = float(obj) else: serializable = json_util.default(obj) return serializable
def _default_date(obj): """`date` serializer for json.""" try: return json_util.default(obj) except TypeError: if isinstance(obj, date): return {"$date_only": obj.isoformat()} raise TypeError("%r is not JSON serializable" % obj)
def default(obj, json_options=bson.json_util.DEFAULT_JSON_OPTIONS): if isinstance(obj, datetime.datetime): return obj.strftime('%Y%m%dT%H%M%S.%f')[:-3] + 'Z' if isinstance(obj, bson.Timestamp): return obj.as_datetime().strftime('%Y%m%dT%H%M%S.%f')[:-3] + 'Z' if isinstance(obj, bson.ObjectId): return str(obj) return json_util.default(obj, json_options)
def _convert(obj): if isinstance(obj, dict): return {k: _convert(v) for k, v in obj.items()} if isinstance(obj, list): return [_convert(v) for v in obj] try: return default(obj, json_options=RELAXED_JSON_OPTIONS) except TypeError: return obj
def json_convert(obj): if isinstance(obj, ObjectId): return str(obj) elif isinstance(obj, datetime): return arrow.get(obj).for_json() #'2015-11-16T23:38:04.551214+00:00' return json_util.default(obj)
def default(self, obj): #_idはObjectId型で、JSONではSerializeできない。よって変換する if isinstance(obj, ObjectId): return json_util.default(obj) elif isinstance(obj, datetime): return obj.strftime("%Y-%m-%d %H:%M:%S") else: return obj return json.JSONEncoder.default(self, obj)
def default(self, o): if isinstance(o, db.QuerySet): return map(lambda d: d.to_json(), o) if isinstance(o, JsonSerializer): return o.to_json() try: return json_util.default(o) except: return super(JSONEncoder, self).default(o)
def to_json(obj): if isinstance(obj, ObjectId): return str(obj) elif isinstance(obj, DBRef): return {"collection": obj.collection, "id": str(obj.id)} elif isinstance(obj, datetime.datetime): return obj.isoformat() else: return default(obj)
def json_formater(obj): if isinstance(obj, ObjectId): return str(obj) if isinstance(obj, datetime): return obj.isoformat() return json_util.default(obj)
def default(self, obj): """Adds support for data types which implement ``as_dict``. """ if hasattr(obj, "as_dict"): return obj.as_dict() if mongo_json is not None: return mongo_json.default(obj) return json.JSONEncoder.default(self, obj)
def default(self, obj, **kwargs): if isinstance(obj, BaseDocument): return obj.to_mongo().to_dict() elif isinstance(obj, ObjectId): return str(obj) elif isinstance(obj, datetime): return obj.strftime("%Y-%m-%d %H:%M:%S") elif isinstance(obj, date): return obj.strftime("%Y-%m-%d") return default(obj)
def to_json(obj): if isinstance(obj, ObjectId): return str(obj) elif isinstance(obj, DBRef): return {'collection': obj.collection, 'id': str(obj.id)} elif isinstance(obj, datetime.datetime): return obj.isoformat() elif isinstance(obj, set): return list(obj) else: return default(obj)
def to_jsonable(obj): if isinstance(obj, datetime.datetime): return obj.isoformat() if isinstance(obj, ObjectId): return json_util.default(obj) props = dir(obj) if 'to_dict' in props: return obj.as_dict() if '__iter__' in props: return list(obj) raise TypeError('Cannot serialize {} of type {}'.format(obj, type(obj)))
def json_formater(obj): # if isinstance(obj, Entry): # return obj.fields() # if isinstance(obj, ObjectId): # return str(obj) if isinstance(obj, datetime): return obj.isoformat() return json_util.default(obj)
def get_streets(): global list_streets list_candidates = list() req = request.args.get("req").upper() animals = Animal.objects(ear_tag__icontains=req) for animal in animals: print(animal.ear_tag) return json_util.default(animals)
def to_json(data): """Convert a BSON value to something JSON serialiseable, using json_util.default. """ if isinstance(data, dict): return dict((key, to_json(value)) for key, value in data.iteritems()) elif isinstance(data, (list, Cursor)): return map(to_json, data) else: try: return default(data) except TypeError: return data
def to_json(obj): if isinstance(obj, ObjectId): return str(obj) elif isinstance(obj, DBRef): return {"collection": obj.collection, "id": str(obj.id)} elif isinstance(obj, datetime.datetime): return obj.isoformat() elif isinstance(obj, set): return list(obj) elif isinstance(obj, User): return obj.username else: return default(obj)
def default(self, obj): if hasattr(obj, 'iteritems') or hasattr(obj, 'items'): # PY3 support return dict(((k, self.default(v)) for k, v in obj.iteritems())) elif hasattr(obj, '__iter__') and not isinstance(obj, string_types): return list((self.default(v) for v in obj)) try: if isinstance(obj, ObjectId): return str(obj) elif isinstance(obj, datetime.datetime): return obj.isoformat() else: return json_util.default(obj) except TypeError: return obj
def bson_to_json(o): if isinstance(o, ObjectId): return str(o) if isinstance(o, datetime.datetime): r = o.isoformat() return r + 'Z' elif isinstance(o, datetime.date): return o.isoformat() elif isinstance(o, datetime.time): r = o.isoformat() if o.microsecond: r = r[:12] return r elif isinstance(o, decimal.Decimal): return str(o) return default(o)
def neo_json_convert(obj, json_options=DEFAULT_JSON_OPTIONS): """Recursive helper method that converts BSON types so they can be converted into json. """ if hasattr(obj, 'iteritems') or hasattr(obj, 'items'): # PY3 support return SON(((k, neo_json_convert(v, json_options)) for k, v in iteritems(obj))) elif hasattr(obj, '__iter__') and not isinstance(obj, (text_type, bytes)): return list((neo_json_convert(v, json_options) for v in obj)) try: obj = default(obj, json_options) return obj except TypeError: return obj
def _json_convert(self, obj): """Recursive helper method that converts BSON types so they can be converted into json. """ string_types = (bytes, str) if hasattr(obj, 'iteritems') or hasattr(obj, 'items'): # PY3 support return SON( ((k, self._json_convert(v)) for k, v in obj.iteritems())) elif isinstance(obj, BaseDocument): return self._json_convert(transform(obj)) elif hasattr(obj, '__iter__') and not isinstance(obj, string_types): return list((self._json_convert(v) for v in obj)) try: return json_util.default(obj) except TypeError: return obj
def default(self, obj): """Serialize MongoDB object types using :mod:`bson.json_util`. Falls back to Quart's default JSON serialization for all other types. This may raise ``TypeError`` for object types not recognized. .. version-added:: 2.4.0 """ if hasattr(obj, "iteritems") or hasattr(obj, "items"): return SON((k, self.default(v)) for k, v in iteritems(obj)) elif hasattr(obj, "__iter__") and not isinstance(obj, string_types): return [self.default(v) for v in obj] else: try: return json_util.default(obj, **self._default_kwargs) except TypeError: # PyMongo couldn't convert into a serializable object, and # the Flask default JSONEncoder won't; so we return the # object itself and let stdlib json handle it if possible return obj
def serialize(self, obj): if isinstance(obj, ObjectId): return str(obj) if isinstance(obj, MongoModel): ret = {} for k in obj.__columns__.keys(): ret_k = self.column_mapping.get(k, k) if getattr(obj.__class__, 'visible_columns', None): if k in obj.visible_columns: ret[ret_k] = getattr(obj, k) else: ret[ret_k] = getattr(obj, k) if getattr(obj, 'serialize', None): for k in obj.serialize: ret[k] = getattr(obj, k) return ret return json_util.default(obj)
def _default(obj): """ Custom conversion of BSON types """ if isinstance(obj, datetime.datetime): return obj.strftime("%Y-%m-%d %H:%M:%S") elif isinstance(obj, ObjectId): return str(obj) elif isinstance(obj, float): if (math.isnan(obj)): return '$float___nan' elif (math.isinf(obj)): if (obj > 0): return '$float___inf' else: return '$float___-inf' else: return obj else: return default(obj)
def default(self, obj): # use the Flask default if possible try: return super(BSONJSONEncoder, self).default(obj) except TypeError as exception: pass # use a simple string for ObjectId if isinstance(obj, ObjectId): return str(obj) # try the BSON encoder for other BSON types try: return bson_json_util.default(obj) except TypeError: pass # general iterables aren't handled by any of the other encoders if isinstance(obj, collections.Iterable): return list(obj) # finally, re-raise the original TypeError exception raise exception
def default(self, obj): if isinstance(obj, datetime.datetime) or isinstance(obj,datetime.date): return obj.isoformat() if isinstance(obj, ObjectId): return str(obj) return json_util.default(obj)
def default(self, obj): return json_util.default(obj)
def _custom_json_default(obj): if is_date_value(obj): return datetime_to_bson(obj) else: return json_util.default(obj)
def datetime_adapter(obj, request): return json_util.default(obj)
def _custom_json_hook(obj): if type(obj) in [datetime, date]: return {"$date": obj.strftime("%Y-%m-%dT%H:%M:%S.000Z")} else: return json_util.default(obj)
def dict_to_json_converter(obj, **kwargs): if isinstance(obj, ObjectId): return OID_PREFIX + str(obj) else: return json_util.default(obj, **kwargs)
def objectId_adapter(obj, request): return json_util.default(obj)
def default(self, obj): if isinstance(obj, datetime): return obj.strftime("%Y-%m-%d %H:%M:%S") if isinstance(obj, ObjectId): return str(obj) return json_util.default(obj, json_util.CANONICAL_JSON_OPTIONS)
def pymongo_adapter(obj, request): return json_util.default(obj)
def default(self, obj): return json_util.default(obj) app = Flask(__name__)