Module: jsl.Data instance of jsl.Module

The jsl.Data module allows to handle the encoding and the serialization of the data. The information contents used by an application can be stored or represented with differents encoding schemes and in differents ways. For example, a simple UNICODE text can be represented using UTF-8 or UTF-16 schemas and it can be stored as string in the JavaScript runtime or as sequence of bytes into a file. This module is responsible to transcoding data in differents forms.

Examples:

var Data = jsl.Data //Parses a JSON string and returns an object: var obj = Data.fromJson(json) //Generates a dataset from a CSV string: var ds = Data.fromCsv(csv, { separator: ",", wrapper: "'" }) //Converts an XNode tree to a prettified XML string: var xml = Data.toXml(xnode, { prettify: true }) //Serializes a custom object into a string: var obj = new Data.Blob([2, 45, 66, ..]) var s = Data.serialize(obj) //Calculates the CRC32 of a block of binary data: var crc32 = Data.crc32(blob) //Encodes a string to a Base64 BLOB: var blob = Data.toBase64("A!N@UIBHYB#Y") //Encodes a string into a UTF-8 BLOB: var utf8 = Data.toUtf8(s) //Compresses a BLOB using the Lz77 algorithm: var lz77 = Data.toLz77(blob) //Sets the default behavior of the XML decoder: Data.setOption("xml.decoder", { validate: true, comment: "collapse" })

API reference
Base

json toJson(obj, options*)
encodes a JavaScript object into a JSON string.
obj fromJson(json, options*)
parses a JSON string and returns a JavaScript object.

xml toXml(xnode, options*)
encodes an XNode tree into an XML string.
xnode fromXml(xml, options*)
parses an XML string and returns an XNode tree.

csv toCsv(ds, options*)
encodes a DataSet instance into a CSV string.
ds fromCsv(csv, options*)
parses a CSV string and returns a DataSet instance.

s serialize(v, options*)
serializes a JavaScript value into a string.
v deserialize(data)
deserializes a JavaScript value from a string.
Binary

crc32 crc32(data)
calculates the CRC32 of a block of data.
blob toBase16(data)
encodes a block of data into a BASE-16 BLOB.
blob fromBase16(b16)
decodes a block of BASE-16 data.

blob toBase32(data)
encodes a block of data into a BASE-32 BLOB.
blob fromBase32(b32)
decodes a block of BASE-32 data.

blob toBase64(data)
encodes a block of data into a BASE-64 BLOB.
blob fromBase64(b64)
decodes a block of BASE-64 data.
Text

blob toUtf8(s)
encodes a string into an UTF-8 BLOB.
s fromUtf8(utf8)
decodes an UTF-8 block of data into a JavaScript string.

blob toUtf16(s, le*)
encodes a string into an UTF-16 BLOB.
s fromUtf16(utf16)
decodes an UTF-16 block of data into a JavaScript string.

blob toUtf16BE(s)
encodes a string into an UTF-16 big endian BLOB.
s fromUtf16BE(utf16be)
decodes an UTF-16 big endian block of data into a JavaScript string.

blob toUtf16LE(s)
encodes a string into an UTF-16 little endian BLOB.
s fromUtf16LE(utf16le)
decodes an UTF-16 little endian block of data into a JavaScript string.
Compression

blob toLz77(data, options*)
compresses a block of binary data into a BLOB using the Lz77 algorithm.
blob fromLz77(lz77)
decompresses a block of Lz77 data.

blob toLzSS(data, options*)
compresses a block of binary data into a BLOB using the LzSS algorithm.
blob fromLzSS(lzss)
decompresses a block of LzSS data.
Options

b json.extended
enables the extended JSON.
s+ json.extension
defines the marker used for stringify extended data into JSON strings.

f json.decoder.filter
defines a default output filter for the JSON parser.
f json.decoder.transform
defines a default output transformation for the JSON parser.

b json.encoder.prettify
enables the default prettification of the JSON encoder.
f json.encoder.filter
defines a default output filter for the JSON encoder.
f json.encoder.transform
defines a default output transformation for the JSON encoder.

b xml.decoder.validate
enables the default syntax validation of the XML parser.
s+ xml.decoder.whitetext
sets the XML decoder to collapse or preserve white-text nodes found in XML documents.
s+ xml.decoder.text
sets the XML decoder to collapse or preserve nodes of type text found in XML documents.
s+ xml.decoder.comment
sets the XML decoder to collapse or preserve nodes of type comment found in XML documents.
s+ xml.decoder.cdata
sets the XML decoder to collapse or preserve nodes of type CDATA found in XML documents.
s+ xml.decoder.pi
sets the XML decoder to collapse or preserve nodes of type processing-instruction found in XML documents.
s+ xml.decoder.dtd
sets the XML decoder to skip or parse the DTD content to find local entity definitions.
s+ xml.decoder.entity
sets the XML decoder to collapse or preserve nodes of type entity-reference found in XML documents.

b xml.encoder.prettify
enables the default prettification of the output for the XML encoder.
f xml.encoder.filter
defines a default output filter for the XML encoder.
f xml.encoder.transform
defines a default output transformation for the XML encoder.

i++ lz77.sbl
configures the default search buffer length used in the lz77 encoder/decoder.
i++ lz77.msl
configures the default max sequence length used in the lz77 encoder/decoder.
i++ lz77.mnc
configures the default max number of comparations used in the lz77 encoder/decoder.

i++ lzSS.sbl
configures the default search buffer length used in the lzSS encoder/decoder.
i++ lzSS.msl
configures the default max sequence length used in the lzSS encoder/decoder.
i++ lzSS.mnc
configures the default max number of comparations used in the lzSS encoder/decoder.

toJson

json toJson(obj, options*)

Serializes a JavaScript object into a JSON string (see the JSON Tutorial for more details). The second argument must be an object containing the following parameters:

  • b prettify: enables the prettification of the JSON output string.
  • b extended: enables the extended JSON encoding.
  • f filter: function used to filter the parts of the object that must be serialized.
  • f transform: function used to alter the JSON output string.

The filter function and the transform function are executed for every property found in the object. These functions are called on the parent object, owner of the current processed property and accepts a context object as argument. The filter function must returns a value convertible to false if you want to exclude the current property from the output. The exclusion of a property automatically stops the enumeration of further sub-properties belonging to it. The transform function instead, must returns the new piece of JSON text relative to the current processed property or null if you want to keep the default text. The context object is always the same object for every call of the filter/transform functions and allows to append also custom properties on it. On the context object you can found the following parameters:

  • s id: the name of the current property.
  • v value: the value of the current property.
  • i+ depth: the depth of the current property.
  • s txt: the stringify result of the current property (only for the transform function).
  • s tokens: an array of the form [ ",", "id|{|[|}|]", value ] (only for the transform function).
.

Returns:

  • s+json
    The JSON string.

Parameters:

  • oobj
    A not null JavaScript object.
  • ooptions*
    An object containing the parameters used to control the serialization process .

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


var Data = jsl.Data var obj = { a: 1, b: /abc/g, c: "one", d: [ 1, 2, { x: 78 }]} //Simple JSON serialization: var json = Data.toJson(obj) //JSON serialization with standard prettification: var json = Data.toJson(obj, { prettify: true }) //JSON serialization with active filter: //excludes the array from the output string: var f = function(cx) { return !(cx.value instanceof Array) } var json = Data.toJson(obj, { prettify: true, filter: f }) //JSON serialization with output transformation: //every property value of type Date is converted into a custom string format: var t = function(cx) { var comma = cx.tokens[0] var id = cx.tokens[1] var v = cx.tokens[2] var colon = jsl.isArray(this) ? "" : ":" return v instanceof Date ? comma + id + colon + v.getFullYear() + "/" + (v.getMonth() + 1) : null } var json = Data.toJson(obj, { transform: t }) //You can also change the default encoder behavior setting //the JSON encoder options directly on the module: Data.setOption("json.encoder", { prettify: true, filter: f, transform: t })

fromJson

obj fromJson(json, options*)

Deserializes a JSON string into a JavaScript object (see the JSON Tutorial for more details). The second argument must be an object containing the following parameters:

  • b extended: enables the extended JSON decoding.
  • f filter: function used to filter the parts of the output object.
  • f transform: function used to alter the content of the output object.

The filter function and the transform function are executed for every property found in the output object. These functions are called on the parent object, owner of the current processed property and accepts a context object as argument. The filter function must returns a value convertible to false if you want to exclude the current property from the output. The exclusion of a property automatically stops the enumeration of further sub-properties belonging to it. The transform function instead, must returns a JavaScript value relative to the current processed property. The context object is always the same object for every call of the filter/transform functions and allows to append also custom properties on it. On the context object you can found the following parameters:

  • s id: the name of the current property.
  • v value: the value of the current property.
  • b last: if the current property is the last property enumerated for the parent object.
.

Returns:

  • oobj
    The output object.

Parameters:

  • s+json
    The JSON string to deserialize.
  • ooptions*
    An object containing the parameters used to control the deserialization process .

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


var Data = jsl.Data //Simple JSON deserialization: var obj = Data.fromJson(json) //JSON deserialization with active filter: //excludes the array for the output object: var f = function(cx) { return !(cx.value instanceof Array) } var obj = Data.fromJson(json, { filter: f }) //JSON deserialization with output transformation: //every primitive empty string is transformed into a null value: var t = function(cx) { return typeof cx.value === "string" && !cx.value ? null : cx.value } var obj = Data.fromJson(json, { transform: t }) //You can also change the default decoder behavior setting //the JSON decoder options directly on the module: Data.setOption("json.decoder", { filter: f, transform: t })

toXml

xml toXml(xnode, options*)

Serializes an XNode tree into an XML string (see the XML Tutorial for more details). The second argument must be an object containing the following parameters:

  • b prettify: enables the prettification of the XML output string.
  • f filter: function used to filter the XNodes that must be serialized.
  • f transform: function used to alter the XML output string.

The filter function and the transform function are executed for every node found in the input tree. The filter function must returns a value convertible to false if you want to exclude the current node from the output. The exclusion of a node automatically stops the scan of his children. The transform function instead, must returns the new piece of XML text relative to the current node or null if you want to keep the default text. The context object is always the same object for every call of the filter/transform functions and allows to append also custom properties on it. On the context object you can found the following parameters:

  • b end: if the parse has reached the end of a node content.
  • s txt: the stringify result of the current node (only present for the transform function).
.

Returns:

  • s+xml
    The XML string.

Parameters:

  • XNodexnode
    The XNode tree.
  • ooptions*
    An object containing the parameters used to control the serialization process .

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


var Data = jsl.Data var XNode = Data.XNode var xdoc = XNode.createDocument() var root = XNode.createElement("root", { mode: "1", scale: "on" }) var text = XNode.createText("This is an XML text node") var cmmt = XNode.createComment("This is an XML comment node") xdoc.appendChild(root) root.appendChildren(text, cmmt) //Serializes an XTree into an XML string: var xml = Data.toXml(xdoc) //Serialization with prettification: var xml = Data.toXml(xdoc, { prettify: true }) //XML serialization with active filter: //excludes all the nodes of type comment: var xml = Data.toXml(xdoc, { filter: function(cx) { return this.getType() !== 8 } }) //Serialization with output transformation: //All the element names are placed in the uppercase form: var re = /(<\/?)([^\s\n\r>]+)/ var toU = function(txt, g1, g2) { return g1 + g2.toUpperCase() } var t = function(cx) { return this.getType() === 1 ? cx.txt.replace(re, toU) : cx.txt } var xml = Data.toXml(xdoc, { transform: t }) //You can also change the default encoder behavior setting //the XML encoder options directly on the module: Data.setOption("xml.encoder", { filter: f, transform: t })

fromXml

xnode fromXml(xml, options*)

Deserializes an XML string into an XNode tree (see the XML Tutorial for more details). The second argument must be an object containing the following parameters:

  • b validate: enables the syntax validation of the XML document.
  • s+ whitetext: collapses or preserves the white-text nodes.
  • s+ text: collapses or preserves the nodes of type text.
  • s+ comment: collapses or preserves the nodes of type comment.
  • s+ cdata: collapses or preserves the nodes of type CDATA.
  • s+ pi: collapses or preserves the nodes of type processing-instruction.
  • s+ dtd: skips or parses the DTD content to find local entity definitions.
  • s+ entity: collapses or preserves nodes of type entity-reference.
.

Returns:

  • XNodexnode
    The XNode tree.

Parameters:

  • s+xml
    The XML string to parse.
  • ooptions*
    An object containing the parameters used to control the deserialization process .

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


var Data = jsl.Data //Deserializes an XML document using the default options: var xnode = Data.fromXml(xml) //Excludes some types of nodes: var xnode = Data.fromXml(xml, { cdata: "collapse", pi: "collapse" }) //Enables the parsing of the local entities: var xnode = Data.fromXml(xml, { dtd: "parse" }) //Parses the entity nodes but collapses //their content to simple text: var xnode = Data.fromXml(xml, { dtd: "parse", entity: "collapse" }) //You can also change the default decoder behavior setting //the XML decoder options directly on the module: Data.setOption("xml.decoder", { dtd: "skip", txt: "preserve" })

toCsv

csv toCsv(ds, options*)

Encodes the data contained in the dataset passed as argument into a CSV string. The second argument must be an object containing the following parameters:

  • c separator: the character to use as separator.
  • c wrapper: the character to use as wrapper to escape the data.
  • b header: if you want to include the header.
  • b extended: if you want to encode also the column types in the header.
.

Returns:

  • s+csv
    The CSV string.

Parameters:

  • DataSetds
    The DataSet instance.
  • ooptions*
    An object containing the parameters used to customize the encoding process .

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.82

Examples:


var Data = jsl.Data //encodes a dataset into a CSV string: var csv = Data.toCsv(ds, { separator: ",", wrapper: "'", header: true, extended: true }) //the extended paramater allows to encode also the column types on the string. //later you can decode the CSV string into a dataset preserving the column types: var ds2 = Data.fromCsv(csv, { separator: ",", wrapper: "'", header: true, extended: true })

fromCsv

ds fromCsv(csv, options*)

Decodes a CSV string into a DataSet instance. The second argument must be an object containing the following parameters:

  • c separator: the character to use as separator.
  • c wrapper: the character to use as wrapper to escape the data.
  • b header: if you want to include the header.
  • b extended: if you want to encode also the column types in the header.
.

Returns:

  • DataSetds
    The DataSet instance.

Parameters:

  • s+csv
    The CSV string to parse.
  • ooptions*
    An object containing the parameters used to customize the decoding process .

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.82

Examples:


var Data = jsl.Data //returns a dataset from a CSV string: var ds = Data.fromCsv(csv, { separator: ",", wrapper: "'", header: true, extended: true })

serialize

s serialize(v, options*)

Serializes a JavaScript value into a string. Every JavaScript value is allowed, from simple strings, numbers and booleans, passing throught standard objects or array and ending with complex instances composed also from circular references. The functions are always stored as executable code. Also native functions located on the global object can be serialized. Native functions not reachable from the global object instead, causes an error. If you want to retain the type of a custom object created by a JavaScript constructor when the object is deserialized, you must implements the jsl.Data.Serializable interface. A class instance instead creates always serializable objects (see the Object Serialization Tutorial for more details).

The second argument is an object containig the following options:

  • b compress - Enable the compression of the serialization output.

The compression can shrink the output of about half. The compression ratio depends from the codes of the characters contained in the serialization output. If we use ISO-LATIN-1 characters (1 byte wide) to write object property names and string values, we will get a good compression. More 2-byte chaaracters will be used to create objects and/or strings, less will be the compression ratio.

Returns:

  • s+s
    The output string.

Parameters:

  • vv
    A JavaScript value.
  • ooptions*
    An object containing the serialization options .

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


var Data = jsl.Data //serializes a simple object: var obj = { a: 1, b: "xyz", c: [ true, null, undefined ], d: function() {}, e: [], f: /z/ } obj.z = obj var s = Data.serialize(obj) //serializes a class instance: var obj = new jsl.EventMap var s = Data.serialize(obj) //serializes a custom object: //the object constructor must implements the //jsl.Data.Serializable interface: var MyType = function() { .. } MyType.prototype.getSerialId = function() { return "MyType" } var s = Data.serialize(new MyType)

deserialize

v deserialize(data)

Deserializes a JavaScript value stored into a string (see the Object Serialization Tutorial for more details).

Returns:

  • vv
    The output JavaScript value.

Parameters:

  • s+data
    The string containing the serialized data.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//Deserializes a JavaScript object: var obj = jsl.Data.deserialize(s)

crc32

crc32 crc32(data)

Calculates and returns the CRC32 of a block of binary data.

Returns:

  • i+crc32
    The CRC32 value.

Parameters:

  • al|s|Blobdata
    The block of data to analyze.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//Returns the CRC32 of a string of bytes: var crc32 = jsl.Data.crc32("ABCFDFDFFCSFCSFCS")

toBase16

blob toBase16(data)

Encodes a block of binary data into a BASE-16 BLOB.

Returns:

  • Blobblob
    The encoded binary data.

Parameters:

  • al|s|Blobdata
    The block of data to encode.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//Encodes an array of bytes: var b16 = jsl.Data.toBase16([2, 78, 0, 0, 9, ..])

fromBase16

blob fromBase16(b16)

Decodes a block of BASE-16 binary data.

Returns:

  • Blobblob
    The decoded binary data.

Parameters:

  • al|s|Blobb16
    The block of encoded data.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//Decodes a string of B16 data: var data = jsl.Data.fromBase16("48454C4C4F20574F524C44")

toBase32

blob toBase32(data)

Encodes a block of binary data into a BASE-32 BLOB.

Returns:

  • Blobblob
    The encoded binary data.

Parameters:

  • al|s|Blobdata
    The block of data to encode.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//Encodes a string of bytes: var b32 = jsl.Data.toBase32("ABCFDFDFFCSFCSFCS")

fromBase32

blob fromBase32(b32)

Decodes a block of BASE-32 binary data.

Returns:

  • Blobblob
    The decoded binary data.

Parameters:

  • al|s|Blobb32
    The block of encoded data.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//Decodes a string of B32 data: var data = jsl.Data.fromBase32("JBCUYTCPEBLU6USMIQ======")

toBase64

blob toBase64(data)

Encodes a block of binary data into a BASE-64 BLOB.

Returns:

  • Blobblob
    The encoded binary data.

Parameters:

  • al|s|Blobdata
    The block of data to encode.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//Encodes an array of bytes: var b64 = jsl.Data.toBase64([200, 200, 89, 98, 77, 255, 0 ])

fromBase64

blob fromBase64(b64)

Decodes a block of BASE-64 binary data.

Returns:

  • Blobblob
    The decoded binary data.

Parameters:

  • al|s|Blobb64
    The block of encoded data.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//Decodes a string of B64 data: var b64 = jsl.Data.fromBase64("SEVMTE8gV09STEQ=")

toUtf8

blob toUtf8(s)

Returns a BLOB object containing the UTF-8 encoding of the string.

Returns:

  • Blobblob
    The UTF-8 data.

Parameters:

  • ss
    A JavaScript String.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

fromUtf8

s fromUtf8(utf8)

Decodes an UTF-8 block of binary data into a JavaScript string.

Returns:

  • ss
    The decoded JavaScript string.

Parameters:

  • al|s|Blobutf8
    The UTF-8 data.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

toUtf16

blob toUtf16(s, le*)

Returns a block of binary data containing the UTF-16 encoding (with BOM) of the string.

Returns:

  • Blobblob
    The UTF-16 data.

Parameters:

  • ss
    The JavaScript string to encode.
  • ble*
    Byte order: true enable little endian order (default=big endian) .

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//Encodes a string into an UTF-16 big endian BLOB: var utf16 = jsl.Data.toUtf16(s) //Encodes a string into an UTF-16 little endian BLOB: var utf16 = jsl.Data.toUtf16(s, true)

fromUtf16

s fromUtf16(utf16)

Decodes an UTF-16 (with BOM) block of binary data into a JavaScript string.

Returns:

  • ss
    The decoded JavaScript string.

Parameters:

  • al|s|Blobutf16
    The UTF-16 data.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

toUtf16BE

blob toUtf16BE(s)

Returns a block of binary data containing the UTF-16 big endian encoding of the string (without BOM).

Returns:

  • Blobblob
    The UTF-16 data.

Parameters:

  • ss
    The JavaScript string to encode.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

fromUtf16BE

s fromUtf16BE(data)

Decodes an UTF-16 big endian (without BOM) block of binary data into a JavaScript string.

Returns:

  • ss
    The decoded string.

Parameters:

  • al|s|Blobdata
    The UTF-16 data.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

toUtf16LE

blob toUtf16LE(s)

Returns a block of binary data containing the UTF-16 little endian encoding of the string (without BOM).

Returns:

  • Blobblob
    The UTF-16 data.

Parameters:

  • ss
    The JavaScript string to encode.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

fromUtf16LE

s fromUtf16LE(data)

Decodes an UTF-16 little endian (without BOM) block of binary data into a JavaScript string.

Returns:

  • ss
    The decoded string.

Parameters:

  • al|s|Blobdata
    The UTF-16 data.

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

toLz77

blob toLz77(data, options*)

Compresses a block of binary data into a BLOB using the Lz77 algorithm (see the Lz Tutorial for more details). The second argument must be an object containing the following parameters:

  • i[3-16] sbl: search buffer length.
  • i[3-8] msl: max sequence length.
  • i[1-255] mnc: max number of comparations in the search buffer.
.

Returns:

  • Blobblob
    The compressed data.

Parameters:

  • al|s|Blobdata
    The block of data to compress.
  • ooptions*
    An object containing the parameters used to control the compression process .

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


var Data = jsl.Data //compresses a block of binary data: var lz77 = Data.toLz77(data) //compresses a block of binary data using custom parameters: var lz77 = Data.toLz77(data, { sbl: 12, msl: 6, mnc: 5 }) //compresses a block of binary data in //asynchronous mode to avoid long running script: var lz77 = [] var blob = new Data.Blob([ .. ]) //data source var size = 8 * 40000 //size in bits jsl.require("timer") var Timer = jsl.Timer var tid = Timer.createTimer(function(data) { var data = blob.read(size) if(data.length) lz77.push(Data.toLz77(data)) else Timer.stop(tid) }, 100)

fromLz77

blob fromLz77(lz77, options*)

Decompresses a Lz77 block of binary data (see the Lz Tutorial for more details). The second argument must be an object containing the following parameters:

  • i[3-16] sbl: search buffer length.
  • i[3-8] msl: max sequence length.
.

Returns:

  • Blobblob
    The decompressed data.

Parameters:

  • al|s+|Bloblz77
    The block of data to decompress.
  • ooptions*
    An object containing the parameters used to control the decompression process .

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


var Data = jsl.Data //decompresses a block of binary data: var data = Data.fromLz77(lz77) //decompresses a block of binary data using custom parameters: var data = Data.fromLz77(data, { sbl: 12, msl: 6 }) //decompresses a block of binary data in //asynchronous mode to avoid long running script: var data = "" var lz77 = [ .. ] //list of compressed blocks var size = 8 * 40000 //size in bits jsl.require("timer") var Timer = jsl.Timer var tid = Timer.createTimer(function(data) { var block = lz77.shift() if(block) data += Data.fromLz77(block) else Timer.stop(tid) }, 100)

toLzSS

blob toLzSS(data, options*)

Compresses a block of binary data into a BLOB using the LzSS algorithm (see the Lz Tutorial for more details). The second argument must be an object containing the following parameters:

  • i[3-16] sbl: search buffer length.
  • i[3-8] msl: max sequence length.
  • i[1-255] mnc: max number of comparations in the search buffer.
.

Returns:

  • Blobblob
    The compressed data.

Parameters:

  • al|s|Blobdata
    The block of data to compress.
  • ooptions*
    An object containing the parameters used to control the compression process .

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


var Data = jsl.Data //compresses a block of binary data: var lzss = Data.toLzSS(data) //compresses a block of binary data using custom parameters: var lzss = Data.toLzSS(data, { sbl: 12, msl: 6, mnc: 5 }) //compresses a block of binary data in //asynchronous mode to avoid long running script: var lzss = [] var blob = new Data.Blob([ .. ]) //data source var size = 8 * 40000 //size in bits jsl.require("timer") var Timer = jsl.Timer var tid = Timer.createTimer(function(data) { var data = blob.read(size) if(data.length) lzss.push(Data.toLzSS(data)) else Timer.stop(tid) }, 100)

fromLzSS

blob fromLzSS(lzss, options*)

Decompresses a LzSS block of binary data (see the Lz Tutorial for more details). The second argument must be an object containing the following parameters:

  • i[3-16] sbl: search buffer length.
  • i[3-8] msl: max sequence length.
.

Returns:

  • Blobblob
    The decompressed data.

Parameters:

  • al|s+|Bloblzss
    The block of data to decompress.
  • ooptions*
    An object containing the parameters used to control the decompression process .

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


var Data = jsl.Data //decompresses a block of binary data: var data = Data.fromLzSS(lzss) //decompresses a block of binary data using custom parameters: var data = Data.fromLzSS(lzss, { sbl: 12, msl: 6 }) //decompresses a block of binary data in //asynchronous mode to avoid long running script: var data = "" var lzss = [ .. ] //list of compressed blocks var size = 8 * 40000 //size in bits jsl.require("timer") var Timer = jsl.Timer var tid = Timer.createTimer(function(data) { var block = lzss.shift() if(block) data += Data.fromLzSS(block) else Timer.stop(tid) }, 100)

json.extended

option: json.extended

Enables the extended JSON (default=false). In the extended mode the following JavaScript types/values are serialized into a string using a prefix marker and later can be reported to the original form at deserialization time:

  • undefined value
  • NaN value
  • Infinity value
  • Date instances
  • RegExp instances
  • function instances
.

Returns:

  • bjson.extended

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


jsl.setOption("json.extended", true)

json.extension

option: json.extension

Defines the marker used for stringify extended data into JSON strings (default="{~~@#}").

Returns:

  • s+json.extension

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


jsl.setOption("json.extension", "#{!!")

json.decoder.filter

option: json.decoder.filter

Defines the default output filter used to deserialize JSON strings (default=null).

Returns:

  • fjson.decoder.filter

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//filters the null properties: var f = function(cx) { return cx.value != null } jsl.setOption("json.decoder.filter", f)

json.decoder.transform

option: json.decoder.transform

Defines the default output transformation used to deserialize JSON strings (default=null).

Returns:

  • fjson.decoder.transform

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//transforms null properties to 0: var t = function(cx) { return cx.value === null ? 0 : cx.value } jsl.setOption("json.decoder.transform", t)

json.encoder.prettify

option: json.encoder.prettify

Enables the default prettification of the JSON encoder (default=false).

Returns:

  • bjson.encoder.prettify

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


jsl.setOption("json.encoder.prettify", true)

json.encoder.filter

option: json.encoder.filter

Defines the default output filter used to serialize JavaScript objects into JSON strings (default=null).

Returns:

  • fjson.encoder.filter

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//filters the null properties: var f = function(cx) { return cx.value != null } jsl.setOption("json.encoder.filter", f)

json.encoder.transform

option: json.encoder.transform

Defines the default output transformation used to serialize JavaScript objects into JSON strings (default=null).

Returns:

  • fjson.encoder.transform

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//transforms null properties to 0: var t = function(cx) { return cx.value == null ? 0 : cx.value } jsl.setOption("json.encoder.transform", t)

xml.decoder.validate

option: xml.decoder.validate

Enables the default syntax validation of the XML parser (default=false).

Returns:

  • bxml.decoder.validate

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


jsl.setOption("xml.decoder.validate", true)

xml.decoder.whitetext

option: xml.decoder.whitetext

Sets the XML decoder to collapse or preserve white-text nodes (formed only by spaces, tabs and new line) found in XML documents (default="collapse"). Sometimes these empty nodes does not contains informations and exists only because the XML document to parse is in a pretty form. To avoid encountering these nodes in the output tree, you can decide to collapse them .

Returns:

  • s+xml.decoder.whitetext

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//collapses all the empty nodes: jsl.setOption("xml.decoder.whitetext", "collapse") //preserves all the empty nodes: jsl.setOption("xml.decoder.whitetext", "preserve")

xml.decoder.text

option: xml.decoder.text

Sets the XML decoder to collapse or preserve the nodes of type text found in XML documents (default="collapse"). When a text node is collapsed, its content is inserted as value of the parent node. If a parent node has several children and some of they is of type text, its value is composed from the concatenation of all the nodes of type text found as children in the same order. .

Returns:

  • s+xml.decoder.text

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//collapses all the text nodes: jsl.setOption("xml.decoder.text", "collapse") //preserves all the text nodes: jsl.setOption("xml.decoder.text", "preserve")

xml.decoder.comment

option: xml.decoder.comment

Sets the XML decoder to collapse or preserve the nodes of type comment found in XML documents (default="preserve"). .

Returns:

  • s+xml.decoder.comment

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//collapses all the comment nodes: jsl.setOption("xml.decoder.comment", "collapse") //preserves all the comment nodes: jsl.setOption("xml.decoder.comment", "preserve")

xml.decoder.cdata

option: xml.decoder.cdata

Sets the XML decoder to collapse or preserve the nodes of type cdata found in XML documents (default="preserve"). .

Returns:

  • s+xml.decoder.cdata

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//collapses all the cdata nodes: jsl.setOption("xml.decoder.cdata", "collapse") //preserves all the cdata nodes: jsl.setOption("xml.decoder.cdata", "preserve")

xml.decoder.pi

option: xml.decoder.pi

Sets the XML decoder to collapse or preserve the nodes of type processing-instruction found in XML documents (default="preserve"). .

Returns:

  • s+xml.decoder.pi

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//collapses all the cdata nodes: jsl.setOption("xml.decoder.pi", "collapse") //preserves all the cdata nodes: jsl.setOption("xml.decoder.pi", "preserve")

xml.decoder.dtd

option: xml.decoder.dtd

Sets the XML decoder to parse or skip the definitions of the local entity references found inside the DTD documents (default="skip"). .

Returns:

  • s+xml.decoder.dtd

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//skips all the local entity definitions: jsl.setOption("xml.decoder.dtd", "skip") //parses all the local entity definitions: jsl.setOption("xml.decoder.dtd", "parse")

xml.decoder.entity

option: xml.decoder.entity

Sets the XML decoder to collapse or preserve nodes of type entity-reference found in XML documents (default="collapse"). To enable the parsing of the local entities you must enable also the xml.decoder.dtd option. If the entity nodes are preserved, they are found in the output XNode tree, otherwise they are resolved and placed in the document before starting the parsing .

Returns:

  • s+xml.decoder.entity

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//collapses all the entity nodes: jsl.setOption("xml.decoder.entity", "collapse") //preserves all the entity nodes: jsl.setOption("xml.decoder.entity", "preserve")

xml.encoder.prettify

option: xml.encoder.prettify

Enables the default output prettification for the XML encoder (default=false) .

Returns:

  • s+xml.encoder.prettify

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

xml.encoder.filter

option: xml.encoder.filter

Defines the default output filter for the XML encoder (default=null) .

Returns:

  • s+xml.encoder.filter

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//filters the comment nodes: var f = function(cx) { return this.getType() !== 8 } jsl.setOption("xml.encoder.filter", f)

xml.encoder.transform

option: xml.encoder.transform

Defines a default output transformation for the XML encoder (default=null) .

Returns:

  • s+xml.encoder.transform

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.67

Examples:


//transforms the content of the comment nodes: var t = function(cx) { return this.getType() === 8 cx.txt.toUpperCase() ? : cx.txt } jsl.setOption("xml.encoder.transform", t)

lz77.sbl

option: lz77.sbl

Defines the default search-buffer-length parameter used in the lz77 encoder/decoder, expressed in number of bits (default=14) .

Returns:

  • i[2-16]lz77.sbl

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.71

Examples:


//defines a new default sbl: jsl.setOption("lz77.sbl", 10)

lz77.msl

option: lz77.msl

Defines the default max-sequence-length parameter used in the lz77 encoder/decoder, expressed in number of bits (default=5) .

Returns:

  • i[2-8]lz77.msl

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.71

Examples:


//defines a new default msl: jsl.setOption("lz77.sbl", 4)

lz77.mnc

option: lz77.mnc

Defines the default max-number-of-comparations parameter used in the lz77 encoder/decoder, expressed as number (default=1) .

Returns:

  • i[1-255]lz77.mnc

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.71

Examples:


//defines a new default mnc: jsl.setOption("lz77.mnc", 3)

lzSS.sbl

option: lzSS.sbl

Defines the default search-buffer-length parameter used in the lzSS encoder/decoder, expressed in number of bits (default=14) .

Returns:

  • i[2-16]lzSS.sbl

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.71

Examples:


//defines a new default sbl: jsl.setOption("lzSS.sbl", 10)

lzSS.msl

option: lzSS.msl

Defines the default max-sequence-length parameter used in the lzSS encoder/decoder, expressed in number of bits (default=5) .

Returns:

  • i[2-8]lzSS.msl

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.71

Examples:


//defines a new default msl: jsl.setOption("lzSS.sbl", 4)

lzSS.mnc

option: lzSS.mnc

Defines the default max-number-of-comparations parameter used in the lzSS encoder/decoder, expressed as number (default=1) .

Returns:

  • i[1-255]lzSS.mnc

Throws:

  • ParamError
    If the method is called with a wrong number of parameters or if it is called with wrong parameters values.

Since:

v.0.71

Examples:


//defines a new default mnc: jsl.setOption("lzSS.mnc", 3)