How to serialize SqlAlchemy result to JSON?

后端 未结 27 1479
说谎
说谎 2020-11-22 09:59

Django has some good automatic serialization of ORM models returned from DB to JSON format.

How to serialize SQLAlchemy query result to JSON format?

I tried

相关标签:
27条回答
  • 2020-11-22 10:04

    A flat implementation

    You could use something like this:

    from sqlalchemy.ext.declarative import DeclarativeMeta
    
    class AlchemyEncoder(json.JSONEncoder):
    
        def default(self, obj):
            if isinstance(obj.__class__, DeclarativeMeta):
                # an SQLAlchemy class
                fields = {}
                for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
                    data = obj.__getattribute__(field)
                    try:
                        json.dumps(data) # this will fail on non-encodable values, like other classes
                        fields[field] = data
                    except TypeError:
                        fields[field] = None
                # a json-encodable dict
                return fields
    
            return json.JSONEncoder.default(self, obj)
    

    and then convert to JSON using:

    c = YourAlchemyClass()
    print json.dumps(c, cls=AlchemyEncoder)
    

    It will ignore fields that are not encodable (set them to 'None').

    It doesn't auto-expand relations (since this could lead to self-references, and loop forever).

    A recursive, non-circular implementation

    If, however, you'd rather loop forever, you could use:

    from sqlalchemy.ext.declarative import DeclarativeMeta
    
    def new_alchemy_encoder():
        _visited_objs = []
    
        class AlchemyEncoder(json.JSONEncoder):
            def default(self, obj):
                if isinstance(obj.__class__, DeclarativeMeta):
                    # don't re-visit self
                    if obj in _visited_objs:
                        return None
                    _visited_objs.append(obj)
    
                    # an SQLAlchemy class
                    fields = {}
                    for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
                        fields[field] = obj.__getattribute__(field)
                    # a json-encodable dict
                    return fields
    
                return json.JSONEncoder.default(self, obj)
    
        return AlchemyEncoder
    

    And then encode objects using:

    print json.dumps(e, cls=new_alchemy_encoder(), check_circular=False)
    

    This would encode all children, and all their children, and all their children... Potentially encode your entire database, basically. When it reaches something its encoded before, it will encode it as 'None'.

    A recursive, possibly-circular, selective implementation

    Another alternative, probably better, is to be able to specify the fields you want to expand:

    def new_alchemy_encoder(revisit_self = False, fields_to_expand = []):
        _visited_objs = []
    
        class AlchemyEncoder(json.JSONEncoder):
            def default(self, obj):
                if isinstance(obj.__class__, DeclarativeMeta):
                    # don't re-visit self
                    if revisit_self:
                        if obj in _visited_objs:
                            return None
                        _visited_objs.append(obj)
    
                    # go through each field in this SQLalchemy class
                    fields = {}
                    for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
                        val = obj.__getattribute__(field)
    
                        # is this field another SQLalchemy object, or a list of SQLalchemy objects?
                        if isinstance(val.__class__, DeclarativeMeta) or (isinstance(val, list) and len(val) > 0 and isinstance(val[0].__class__, DeclarativeMeta)):
                            # unless we're expanding this field, stop here
                            if field not in fields_to_expand:
                                # not expanding this field: set it to None and continue
                                fields[field] = None
                                continue
    
                        fields[field] = val
                    # a json-encodable dict
                    return fields
    
                return json.JSONEncoder.default(self, obj)
    
        return AlchemyEncoder
    

    You can now call it with:

    print json.dumps(e, cls=new_alchemy_encoder(False, ['parents']), check_circular=False)
    

    To only expand SQLAlchemy fields called 'parents', for example.

    0 讨论(0)
  • 2020-11-22 10:04

    It is not so straighforward. I wrote some code to do this. I'm still working on it, and it uses the MochiKit framework. It basically translates compound objects between Python and Javascript using a proxy and registered JSON converters.

    Browser side for database objects is db.js It needs the basic Python proxy source in proxy.js.

    On the Python side there is the base proxy module. Then finally the SqlAlchemy object encoder in webserver.py. It also depends on metadata extractors found in the models.py file.

    0 讨论(0)
  • 2020-11-22 10:04

    Even though it's a old post, Maybe I didn't answer the question above, but I want to talk about my serialization, at least it works for me.

    I use FastAPI,SqlAlchemy and MySQL, but I don't use orm model;

    # from sqlalchemy import create_engine
    # from sqlalchemy.orm import sessionmaker
    # engine = create_engine(config.SQLALCHEMY_DATABASE_URL, pool_pre_ping=True)
    # SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    

    Serialization code

    
    
    import decimal
    import datetime
    
    
    def alchemy_encoder(obj):
        """JSON encoder function for SQLAlchemy special classes."""
        if isinstance(obj, datetime.date):
            return obj.strftime("%Y-%m-%d %H:%M:%S")
        elif isinstance(obj, decimal.Decimal):
            return float(obj)
    
    import json
    from sqlalchemy import text
    
    # db is SessionLocal() object 
    
    app_sql = 'SELECT * FROM app_info ORDER BY app_id LIMIT :page,:page_size'
    
    # The next two are the parameters passed in
    page = 1
    page_size = 10
    
    # execute sql and return a <class 'sqlalchemy.engine.result.ResultProxy'> object
    app_list = db.execute(text(app_sql), {'page': page, 'page_size': page_size})
    
    # serialize
    res = json.loads(json.dumps([dict(r) for r in app_list], default=alchemy_encoder))
    
    

    If it doesn't work, please ignore my answer. I refer to it here

    https://codeandlife.com/2014/12/07/sqlalchemy-results-to-json-the-easy-way/

    0 讨论(0)
  • 2020-11-22 10:06
    step1:
    class CNAME:
       ...
       def as_dict(self):
           return {item.name: getattr(self, item.name) for item in self.__table__.columns}
    
    step2:
    list = []
    for data in session.query(CNAME).all():
        list.append(data.as_dict())
    
    step3:
    return jsonify(list)
    
    0 讨论(0)
  • 2020-11-22 10:07

    A more detailed explanation. In your model, add:

    def as_dict(self):
           return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}
    

    The str() is for python 3 so if using python 2 use unicode(). It should help deserialize dates. You can remove it if not dealing with those.

    You can now query the database like this

    some_result = User.query.filter_by(id=current_user.id).first().as_dict()
    

    First() is needed to avoid weird errors. as_dict() will now deserialize the result. After deserialization, it is ready to be turned to json

    jsonify(some_result)
    
    0 讨论(0)
  • 2020-11-22 10:07

    Use the built-in serializer in SQLAlchemy:

    from sqlalchemy.ext.serializer import loads, dumps
    obj = MyAlchemyObject()
    # serialize object
    serialized_obj = dumps(obj)
    
    # deserialize object
    obj = loads(serialized_obj)
    

    If you're transferring the object between sessions, remember to detach the object from the current session using session.expunge(obj). To attach it again, just do session.add(obj).

    0 讨论(0)
提交回复
热议问题