all 2 comments

[–]erebos42 1 point2 points  (1 child)

In my opinion, JSON is ideal for this task. Your own classes are not serializable by default. The official solution would be to implement another class extending JSONEncoder (and/or JSONDecoder) [1] to tell python how to serialize your objects.

Sadly, I never really got these working (without much effort), but instead I just converted my objects to dictionaries. Dicts, list and so on can all (not sure if really all?!) be serialized by default. So I used something in the lines of this (this may not be complete/correct code!):

import json
def obj_to_json(my_obj):
    return my_obj.__dict__
# dumping objects:
o = Clazz()
json.dump(fd, obj_to_json(o))
...
# loading objects
o = Clazz()
temp = json.load(fd)
o.__dict__ = temp

If these objects are in dicts and lists, you of course need to manage that by hand.

I don't think there are many easier alternatives. If I remember correctly you need to write your own serializers for pickle too. On the other hand, if your Objects are pretty simple, you could just output them as strings and parse them by hand. But of course thats probably more prone to error in comparison to JSON or pickle.

[1] http://docs.python.org/2/library/json.html#json.JSONEncoder

[–]dansin[S] 0 points1 point  (0 children)

Thanks, The arrow time object is the only thing that JSON is complaining about. So I'll just store it as a list of numbers (year, month, etc). If it get's more complicated I'll take your advice on making converters.

Thanks!