1

The following code throws

TypeError: Object of type 'datetime' is not JSON serializable

which I know how to resolve. However my real question is how to cleanly structure the code to avoid a partial file if any exception occurs in json.dump.

import datetime
import json

def save(data):
    with open('data.txt', 'w') as outfile:
        json.dump(data, outfile)

data = dict(sometime=datetime.datetime.now())
save(data)

The above code throws an exception and results in a partial file like:

{"sometime": 

Should I dumps to a string first in a try/except? If so are there any memory implications to be aware of? Or delete the file in an except block?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
User
  • 62,498
  • 72
  • 186
  • 247
  • Possible duplicate of [How to make a class JSON serializable](https://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable) – metatoaster Feb 17 '18 at 01:52

2 Answers2

2

Use a try/except block like:

Code:

def save_json(data, filename):
    try:
        with open(filename, 'w') as outfile:
            json.dump(data, outfile)
    except:
        try:
            os.unlink(filename)
        except FileNotFoundError:
            pass

and if you want to preserve the exception:

def save_json(data, filename):
    try:
        with open(filename, 'w') as outfile:
            json.dump(data, outfile)
    except:
        if os.path.exists(filename):
            os.unlink(filename)
        raise

Test Code:

import datetime
import json
import os

data = dict(sometime=datetime.datetime.now())
save_json(data, 'data.txt')
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

That depends on whether your JSON data is under your control or from unknown source. If it’s from somewhere you can’t predict, use try...except... block. Otherwise, fix your program to make it always available to serialize.

Hou Lu
  • 3,012
  • 2
  • 16
  • 23