Refactor print and dump into one function

Since both print and dump need to serialize the output of the database
and the only difference between these two functions is the format of the
output, it only makes sense to refactor the serialization into one function
with the output as variable.