Search code examples
pythonmarshmallow

Serialize object with umlauts to json file with Marshmallow


I want to serialize a pyhthon object to json. The object has some strings with umlauts in it. I use Marshmallow for the Schema. Below a code snippet.

from marshmallow import Schema, fields, post_load, post_dump
import logging as log
import os.path
log.basicConfig(level=log.DEBUG)

class Umlaut():
    def __init__(self, name): self.name = name

class UmlautSchema(Schema):
    name = fields.Str()
    
    @post_dump
    def post_dump(self, data, many=False):
        log.debug(data) # Umlauts are fine
        return data

class Filehandling():
    def write(self, u):
        pathToFile = os.path.abspath("/tmp/")
        schema = UmlautSchema()
        res = schema.dumps(u)
        log.debug(res)  # Here we have 'Umlaut-\u00f6-\u00e4-\u00fc-\u00c4-\u00d6-\u00dc'
        file = os.path.join(pathToFile, "umlauts.json")
        with open(file, mode="w", encoding="UTF-8") as outfile:
            outfile.write(res)
            outfile.close()

def run():
    filehandling = Filehandling()
    u = Umlaut("Umlaut-ö-ä-ü-Ä-Ö-Ü")
    log.debug(u.name) # Umlauts are fine
    filehandling.write(u)

When I run my code the umlauts in the json file looks like \u00f6. What can I do to get the umlauts properly encoded like 'ä, ö, ü'?

When you look at the log statements in the post_dump method the umlauts are ok but when

Thanks for your help Stephan


Solution

  • According to the docs additional kwargs are passed to the configured JSON renderer, by default json from Python itself is used (ref).

    Using your classes with ensure_ascii=False:

    >>> schema.dumps(u, ensure_ascii=False)
    '{"name": "Umlaut-ö-ä-ü-Ä-Ö-Ü"}'
    

    For other renderers you have to check their documentation.