https://github.com/fdawgs/fastify-json-to-xml
Fastify plugin to serialise JSON responses into XML
https://github.com/fdawgs/fastify-json-to-xml
fastify json nodejs plugin serialise serialize xml
Last synced: about 1 year ago
JSON representation
Fastify plugin to serialise JSON responses into XML
- Host: GitHub
- URL: https://github.com/fdawgs/fastify-json-to-xml
- Owner: Fdawgs
- License: mit
- Created: 2023-02-03T12:51:31.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-05-11T19:25:18.000Z (about 1 year ago)
- Last Synced: 2025-05-11T20:25:31.186Z (about 1 year ago)
- Topics: fastify, json, nodejs, plugin, serialise, serialize, xml
- Language: JavaScript
- Homepage: https://npmjs.com/package/fastify-json-to-xml
- Size: 240 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# fastify-json-to-xml
[](https://github.com/Fdawgs/fastify-json-to-xml/releases/latest/)
[](https://npmjs.com/package/fastify-json-to-xml)
[](https://github.com/Fdawgs/fastify-json-to-xml/actions/workflows/ci.yml)
[](https://coveralls.io/github/Fdawgs/fastify-json-to-xml?branch=main)
[](https://github.com/prettier/prettier)
> Fastify plugin to serialise JSON responses into XML
## Overview
The `fastify-json-to-xml` plugin adds an `onSend` hook that supports serialising 'application/json' responses into XML if:
- The `Accept` HTTP request header only includes 'application/xml'
- The `Accept` HTTP request header explicitly includes the 'application/xml' media type before 'application/json'
## Installation
Install using `npm`:
```bash
npm i fastify-json-to-xml
```
For Fastify v4.x support, use `fastify-json-to-xml@1.1.12`.
## Example usage
```js
const Fastify = require("fastify");
const jsonToXml = require("fastify-json-to-xml");
const server = Fastify();
server.register(jsonToXml);
server.get("/", (_req, res) => {
res.send({ example: "I'm an example value!" });
});
server.listen(3000, (err) => {
if (err) throw err;
console.log("Server listening on 3000");
});
```
Make an HTTP GET request to the route above, with application/xml in the `accept` HTTP request header, and the results will look like so:
```xml
I'm an example value!
```
## Replacing invalid XML characters in output
By default, this plugin will throw an error if the JSON response it is transforming has characters that XML considers invalid, such as "$".
Set `replaceInvalidChars: true` in the plugin options and they will be replaced with the Unicode replacement character (U+FFFD) instead, and the plugin will not throw an error.
For example:
```json
{ "$test-key": "test-value" }
```
Will become:
```xml
<�test-key>test-value�test-key>'
```
## Contributing
Contributions are welcome, and any help is greatly appreciated!
See [the contributing guide](https://github.com/Fdawgs/.github/blob/main/CONTRIBUTING.md) for details on how to get started.
Please adhere to this project's [Code of Conduct](https://github.com/Fdawgs/.github/blob/main/CODE_OF_CONDUCT.md) when contributing.
## Acknowledgements
- [**Aras Abbasi**](https://github.com/uzlopak) - TypeScript support
## License
`fastify-json-to-xml` is licensed under the [MIT](./LICENSE) license.