Documentation / @einvoice-ts/serializer / serialize
Function: serialize() ​
serialize(
document,options?):string
Defined in: serialize.ts:366
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 ​
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).
Throws ​
When a text or an attribute value holds a character XML 1.0 does not allow (a control character other than tab, line feed and carriage return, U+FFFE, U+FFFF, or half of a surrogate pair), which no escape can write. The message names the XPath and the code point; validate() reports the same value as EINV-XSD-LEXICAL.
Example ​
import { serialize } from '@einvoice-ts/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