perhaps Python's json encoder does not maintain ordering?   I don't know 
either.   Can you set echo='debug' on your create_engine() and examine the 
parameters being passed as well as the data being returned?   also I'm not 
seeing which database backend or driver you're using which may be significant 
(particularly if its PostgreSQL which has a fully native JSON datatype).


On Wed, Jan 6, 2021, at 7:39 AM, yoch....@gmail.com wrote:
> Hi,
> 
> I have a problem with a custom JSON type, which is not preserving keys order, 
> despite that I'm using python 3.8.
> 
> Here is the type definition and usage:
> 
> ```
> import json
> from sqlalchemy import Column, Integer
> from sqlalchemy.types import TypeDecorator, TEXT
> from sqlalchemy.ext.mutable import MutableDict
> 
> class JSONType(TypeDecorator):
>     impl = TEXT
> 
>     def process_bind_param(self, value, dialect):
>         if value is not None:
>             value = json.dumps(value)
>         return value
> 
>     def process_result_value(self, value, dialect):
>         if value is not None:
>             value = json.loads(value)
>         return value
> 
> JSONDict = MutableDict.as_mutable(JSONType)
> 
> Base = automap_base()
> 
> class Picture(Base):
>     __tablename__ = 'picture'
>     id = Column(Integer, primary_key=True)
>     data = Column(JSONDict)
> ```
> 
> When I'm storing values in the data field (values are nested elements of 
> different types), store the picture in database and reload it, the keys order 
> of data dict is not preserved. I'm don't understand why, because the version 
> of python I'm using is supposed to keep the dict insertion order.
> 
> Have you some idea how to solve that ? The JSON keys order is important for 
> my application.
> 
> Thank you
> 
> 

> -- 
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
>  
> http://www.sqlalchemy.org/
>  
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> --- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/2023e849-37d4-43e5-bd1f-02829474c62bn%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/2023e849-37d4-43e5-bd1f-02829474c62bn%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/8361158f-49ca-41a5-a1c4-f2297eacb6c7%40www.fastmail.com.

Reply via email to