Skip to content

Modules and Mappings

highsource edited this page Dec 23, 2014 · 2 revisions

Introduction

Jsonix Schema Compiler compiles incoming schemas in form of mappings which are grouped into modules.

Modules

Please see the following link for an in-depth explanation of different approaches to JavaScript modules.

Modules generated by the Jsonix Schema Compiler are JavaScript files which export one or more Jsonix mapping.

Currently Jsonix Schema Compiler generates modules in UMD-like style which are compatible to AMD as well as CJS and naked browser runtime. This means that modules generated by the Jsonix Schema Compiler can be used for instance with RequireJS, Node.js as well as directly in the browser without any module loader whatsoever.

This is how a typical generated module looks like:

var PO_Module_Factory = function () {
  // The PO module is defined here
  var PO = { ... };
  return {
    PO: PO
  };
};
if (typeof define === 'function' && define.amd) {
  // Export via AMD
  define([], PO_Module_Factory);
}
else {
  var PO_Module = PO_Module_Factory();
  if (typeof module !== 'undefined' && module.exports) {
    // Export via CJS
    module.exports.PO = PO_Module.PO;
  }
  else {
    // Export as a global variable (vanilla browser)
    var PO = PO_Module.PO;
  }
}

You can configure the compiler to generate modules containing specific mappings. If a certain mapping was not included in any module, the compiler will implicitly create a module containing this mapping.

Mappings

One module contains one or more Jsonix mappings.

Mappings are derived from schemas. Each distinct logical schema (one or more schema files with the same target namespace) will create one mapping.

Each Jsonix mapping has a name. This name can be either configured explicitly or generated automatically based on the namespace URI. For instance the URI http://www.w3.org/1999/xlink will produce the mapping name org_w3__1999_xlink by default.

The name of the mapping will be used by the module to export the given mapping. For instance, in the previous example the PO module exports the PO mapping - via AMD define, CJS exports or simply as a global variable depending on the detected runtime (or absence thereof).

There may be several mappings for one logical schema/namespace - but configured with different contents and included into different modules. If there are several mappings for one namespace, all of them must have the same name.

A mapping can be configured with different contents (via [[includes|Includes Configuration]] and [[excludes|Excludes Configuration]]). This allows generating different mapping subsets for different use cases. For instance, you might need a "full" mapping containing everything you original schema defines or a "light" mapping containing only a few elements or types which you need for a certain use case.

Clone this wiki locally