# jsonix

> Jsonix (JSON interfaces for XML) is a JavaScript library which allows converting between XML and JSON structures.

Latest version **3.0.0** (published 2019-03-20) · 0 weekly downloads

## Install

```sh
npm install jsonix
pnpm add jsonix
yarn add jsonix
bun add jsonix
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2019-03-20 |
| First published | 2014-02-23 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 7.5 MB |
| Known vulnerabilities | 0 (+8 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 372 |
| Author | Alexey Valikov |
| Maintainers | highsource |
| Keywords | json, xml, unmarshal, unmarshalling, marshal, marshalling, parse, parsing, serialize, serializing, javascript objects, dom, util, utility, jaxb, jsonix |

## Links

- npm: https://www.npmjs.com/package/jsonix
- Repository: https://github.com/highsource/jsonix
- Homepage: http://github.com/highsource/jsonix
- Issues: http://github.com/highsource/jsonix/issues
- npm.io page: https://npm.io/package/jsonix

## Dependencies (3)

- [xmldom](https://npm.io/package/xmldom.md) >=0.1.21
- [amdefine](https://npm.io/package/amdefine.md) 0.x.x
- [xmlhttprequest](https://npm.io/package/xmlhttprequest.md) 1.x.x

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 3.0.0 (latest) — 2019-03-20
- 2.4.1 — 2016-01-16
- 2.4.0 — 2015-12-28
- 2.3.2 — 2015-09-12
- 2.3.1 — 2015-08-30
- 2.3.0 — 2015-06-28
- 2.2.1 — 2015-04-11
- 2.2.0 — 2015-04-05
- 2.1.4 — 2015-03-25
- 2.1.3 — 2015-02-28
- 2.1.2 — 2015-02-28
- 2.1.1 — 2014-12-28
- 2.1.0 — 2014-09-12
- 2.0.12 — 2014-08-31
- 2.0.11 — 2014-08-24
- … 2 more at https://npm.io/package/jsonix/versions

## README

# Jsonix

* Jsonix (JSON interfaces for XML) is a JavaScript library which allows you to convert between XML and JSON structures.
* With Jsonix you can parse XML into JSON (this process is called _unmarshalling_) or serialize JSON in XML form (this is called _marshalling_).
* These conversions are based on declarative XML/JSON mappings which can be written manually or generated from an XML Schema.

Jsonix advantages:

* Strongly structured
* Type-safe
* Bidirectional
* (Optionally) XML Schema-driven

See also the other [Jsonix features](#jsonix-features).

## Example

Here's a working example for the [purchase order schema](http://www.w3.org/TR/xmlschema-0/#po.xsd) (try it [online in JSFiddle](http://jsfiddle.net/lexi/LP3DC/)).

### Generate mappings

```
java -jar node_modules/jsonix/lib/jsonix-schema-compiler-full.jar
  -d mappings -p PO purchaseorder.xsd
```

Generates mappings for the `purchaseorder.xsd` schema in the `mappings\PO.js`; mappings will be placed in the variable `PO`.

### Parse XML into JS

```javascript
// Include or require PO.js so that PO variable is available
// For instance, in node.js:
var PO = require('./mappings/PO').PO;

// First we construct a Jsonix context - a factory for unmarshaller (parser)
// and marshaller (serializer)
var context = new Jsonix.Context([PO]);

// Then we create a unmarshaller
var unmarshaller = context.createUnmarshaller();

// Unmarshal an object from the XML retrieved from the URL
unmarshaller.unmarshalURL('po.xml',
    // This callback function will be provided
    // with the result of the unmarshalling
    function (unmarshalled) {
        // Alice Smith
        console.log(unmarshalled.value.shipTo.name);
        // Baby Monitor
        console.log(unmarshalled.value.items.item[1].productName);
    });
```

You can also `unmarshalString`, `unmarshalDocument` and (under node.js) `unmarshalFile`.
    
### Serialize JS as XML

```javascript
// Create a marshaller
var marshaller = context.createMarshaller();

// Marshal a JavaScript Object as XML (DOM Document)
var doc = marshaller.marshalDocument({
    name: {
        localPart: "purchaseOrder"
    },
    value: {
        orderDate: { year: 1999, month: 10, day: 20 },
        shipTo: {
            country: "US",
            name: "Alice Smith",
            street: "123 Maple Street",
            city: "Mill Valley",
            state: "CA",
            zip: 90952
        },
        billTo: { /* ... */ },
        comment: 'Hurry, my lawn is going wild!',
        items: { /* ... */ }
    }
});
```

You can also `marshalString`.

## Jsonix Features

* Runs in almost any modern browser
* Runs in [Node.js](http://nodejs.org/)
* Runs with CommonJS modules, AMD modules as well as vanilla (globals, without any module loader)
* Bidirectional (XML -> JS as well as JS -> XML)
* Implements *marshalling* (serializing the JavaScript object into XML)
  * Supports string data and DOM nodes as result
* Implements *unmarshalling* (parsing a JavaScript object from XML)	
  * Supports string data, DOM nodes, URLs or files (with Node.js) as source
* Driven by declarative XML/JS mappings which control how JavaScript object is converted into XML or vice versa
* Mappings can be automatically generated based on the XML Schema
* Strongly-structured - XML/object mappings describe structures of JavaScript objects
* Strongly-typed - Conversion between string content on XML side and values on the JavaScript side is controlled by declared property types
* Provides extensible type system
  * Supports most XML Schema simple types (inlcuding QNames)
  * Supports enumerations, list and union simple types
  * Allows adding own simple types
  * Supports complex types consisting of several properties
  * Supports deriving complex types by extension
* Provides advanced property system
  * Value, attribute, element, element reference properties for string processing of XML content
  * Any attribute, any element properties for "lax" processing for XML content

## Documentation

* [Jsonix GitHub Project](https://github.com/highsource/jsonix)
* [Jsonix Wiki](https://github.com/highsource/jsonix/wiki)

---
_Source: https://npm.io/package/jsonix · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
