Honor toJSON/toXML() for XML support

/to @ritch @bajtos 

The PR allows customization of JSON to XML conversion using toJSON()/toXML(). It also adds some test cases for XML media types.