Skip to content

Documentation / @facturometro/serializer / serialize

Function: serialize() ​

serialize(document, options?): string

Defined in: serialize.ts:337

Serializes an invoice or a credit note to UBL 2.1 XML.

The output is deterministic: elements and attributes come out in the order of the Peppol BIS Billing 3.0 syntax, never in the order of the object's keys, so two equal documents always produce byte-identical XML. The root carries exactly three namespace declarations (the document's own as the default namespace, cac and cbc); UBLVersionID, UBLExtensions and the model's kind discriminant are never written.

Nothing is defaulted, computed or reformatted here - run the document through createInvoice() or createCreditNote() first if it needs that.

Parameters ​

document ​

Document | UncheckedDocument

A canonical (Invoice, CreditNote) or unchecked document.

options? ​

SerializeOptions = {}

Declaration and indentation; see SerializeOptions.

Returns ​

string

The XML document, without a trailing newline.

Throws ​

When the argument is not an invoice or credit note document; when a leaf holds something other than a string or a leaf object (an object with a value and/or a known attribute) - a number, a Decimal, a Date and a typo such as { amount: '1.00' } are all errors rather than silently empty elements; when an aggregate holds something other than an object; or when an array is used for an element that does not repeat (or the other way round).

Example ​

ts
import { serialize } from '@facturometro/serializer';

serialize(invoice);
// <?xml version="1.0" encoding="UTF-8"?>
// <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" ...>
//   <cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#...</cbc:CustomizationID>
//   ...
// </Invoice>

serialize(invoice, { declaration: false, indent: false }); // one line, no prolog

MIT licensed. Specification artefacts belong to OpenPEPPOL, CEN and OASIS.