redis / redis-om-python

Object mapping, and more, for Redis and Python
MIT License
1.07k stars 108 forks source link

中文乱码问题 Chinese garbled character problem #504

Open stuacct076 opened 1 year ago

stuacct076 commented 1 year ago

有办法解决JsonModal.get()中文序列化问题吗 Is there a way to solve the JsonModal.get() Chinese serialization problem?


framework:FastAPI

requirements.txt:

anyio==3.6.2
async-timeout==4.0.2
cffi==1.15.1
click==8.1.3
colorama==0.4.6
cryptography==40.0.2
fastapi==0.95.0
h11==0.14.0
hiredis==2.2.2
httptools==0.5.0
idna==3.4
more-itertools==8.14.0
pptree==3.1
pycparser==2.21
pydantic==1.10.7
python-dotenv==1.0.0
python-ulid==1.1.0
PyYAML==6.0
redis==4.5.4
redis-om==0.1.2
sniffio==1.3.0
starlette==0.26.1
types-pyOpenSSL==23.1.0.2
types-redis==4.5.4.1
typing_extensions==4.5.0
uvicorn==0.21.1
watchfiles==0.19.0
websockets==11.0.2
import uvicorn
from fastapi import FastAPI
from redis_om import JsonModel, get_redis_connection

app = FastAPI()

class User(JsonModel):
    name: str
    age: int

    class Meta:
        database = get_redis_connection(url="redis://:123456@localhost:6379/1", decode_responses=True)

@app.get("/save")
def save_user():
    user = User(pk="test", name="张三", age=18)
    return user.save()

@app.get("/get/{pk}")
def get_user():
    """
    {
        "pk": "test",
        "name": "å¼ ä¸\u0089", # 中文乱码 (Chinese garbled characters)
        "age": 18
    }
    """
    return User.get("test")

if __name__ == '__main__':
    uvicorn.run(app, host="127.0.0.1", port=8000)

Thanks

stuacct076 commented 1 year ago

I studied the source code and tried to solve this problem by the following way, But I don't know if there's a hidden danger to this!!! 👀

image