Maybe we can even move this in some helper class as we use something similar in HttpObjectEncoder.