Fixed serialization of objects to json and handle html and plain returns