Question:
I’d like to convert the output of pycaching.cache.Cache.load_logbook() into a list of pycaching.log.Log objects. How would I do this? API reference — pycaching 4.2.3 documentation
@Firepup650 I know you previously helped me with someone similar within Pycaching, if you could help a bit more, that’d be great!
@Firepup650 Tried that already, it looks like it should work, but it raises the error raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable") TypeError: Object of type Log is not JSON serializable
Assuming you are using flask, this would probably mean that you are trying to return the list of Logs, which flask tries to convert into a response object using jsonify so it can be sent. pycaching.log.Log is not JSON serializable.
So you’ll have to convert the logs into, for example, a dict.
The problem is that the components of Log need to be serialized too.
If you are not concerned about deserializing, then converting it all to strings is enough.
def to_dict(log):
return {
name: str(getattr(log, name, 'null'))
for name in 'author text type uuid visited'.split()
}
logs = [to_dict(log) for log in pycaching.cache.Cache.load_logbook()]