One document matched: draft-lhotka-yang-json-00.xml


<?xml version="1.0"?>

<?rfc toc="yes"?>
<?rfc symrefs="yes"?>
<?rfc sortrefs="yes"?>
<rfc ipr="trust200902" category="std"
     docName="draft-lhotka-yang-json-00">

<front>
  <title abbrev="Modeling JSON with YANG">Modeling JSON Text with
  YANG</title>

  <author initials="L." surname="Lhotka" fullname="Ladislav Lhotka">
    <organization>CZ.NIC</organization>
    <address>
      <email>lhotka@nic.cz</email>
    </address>
  </author>
  <date day="02" month="April" year="2012"/>
  <area>Operations and Management</area>
   <workgroup>NETMOD</workgroup>
  <abstract>
    <t>This document defines rules for mapping data models expressed
    in YANG to JSON text. It does so by specifying a procedure for
    translating the subset of YANG-compatible XML documents to JSON
    text, and vice versa.</t>
  </abstract>
</front>

<middle>

  <section anchor="sec.introduction" title="Introduction">
    <t>The aim of this document is define rules for mapping data
    models expressed in the YANG data modeling language <xref
    target="RFC6020"/> to JavaScript Object Notation (JSON) text <xref
    target="RFC4627"/>. The result can be potentially applied in two
    different ways:
    <list style="numbers">
      <t>JSON can be used in the context of the NETCONF protocol <xref
      target="RFC6241"/> and with existing data models expressed in
      YANG.</t>
      <t>Other documents that choose JSON to represent structured data
      can use YANG for defining the data model, i.e., both syntactic
      and semantic constraints that the data have to satisfy.</t>
    </list>
    The former use case is not currently possible without further work
    because all NETCONF and YANG standards so far assume that
    XML <xref target="XML"/> is used for encoding both protocol
    messages and configuration data.</t>
    <t>JSON mapping rules could be specified in a similar way as the
    XML mapping rules in <xref target="RFC6020"/>. This would however
    require solving several problems. To begin with, YANG uses XPath
    <xref target="XPath"/> quite heavily but XPath is not defined
    for JSON and such a definition would be far from straightforward.</t>
    <t>In order to avoid these technical difficulties, this document
    employs an alternative approach: it defines a relatively simple
    procedure which allows to translate the subset of XML that can be
    modeled using YANG to JSON, and vice versa. Consequently,
    validation of a JSON text against a data model can done by
    translating the JSON text to XML, which is then validated
    according to the rules stated in <xref target="RFC6020"/>.</t>
    <t>The translation procedure is adapted to YANG specifics and
    requirements, namely:
    <list style="numbers">
      <t anchor="it.ns">Translation of YANG namespaces is
      supported.</t>
      <t anchor="it.typ">The information about datatypes of leaf nodes
      is used for translating the leaf values to the most appropriate
      JSON datatype.</t>
      <t>Translation of XML attributes, mixed content, comments and
      processing instructions is not supported.</t>
    </list></t>
    <t>Properties <xref format="counter" target="it.ns"/> and <xref
    format="counter" target="it.typ"/> mean that the translation is
    driven by a YANG data model which must therefore be known in
    advance.</t>
  </section>

  <section anchor="sec.term-not" title="Terminology and Notation">
    
    <t>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL
    NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL"
    in this document are to be interpreted as described in <xref
    target="RFC2119"/>.</t>

    <t>The following terms are defined in <xref target="RFC6020"/>:
    <list style="symbols">
      <t>anyxml</t>
      <t>augment</t>
      <t>container</t>
      <t>data model</t>
      <t>data node</t>
      <t>data tree</t>
      <t>datatype</t>
      <t>identity</t>
      <t>instance identifier</t>
      <t>leaf</t>
      <t>leaf-list</t>
      <t>list</t>
      <t>module</t>
      <t>submodule</t>
    </list></t>

    <t>The following terms are defined in <xref target="XMLNS"/>:
    <list style="symbols">
      <t>local name</t>
      <t>prefixed name</t>
      <t>qualified name</t>
    </list></t>

  </section>

  <section anchor="sec.specification"
           title="Specification of the Translation Procedure">
    <t>The translation procedure defines a 1-1 correspondence
    between the subset of YANG-compatible XML documents and JSON
    text. This means that the translation can be applied in both
    directions and is always invertible.</t>
    <t>The following subsections contain mainly rules for translating XML
    documents to JSON text. Rules for the inverse translation are
    stated only where needed, otherwise they can be easily
    inferred.</t>
    <t>XML documents may be translated to JSON text only if they are
    valid instances of a YANG data model, which must therefore be
    known beforehand. There is one extra restriction beyond the
    standard YANG rules: XML documents with mixed content - which is
    marginal in YANG and allowed only in anyxml nodes - cannot be
    translated by this procedure.</t>
    <t>The semantics of several YANG statements, such as "rpc",
    "notification", "config", "if-feature" or "default", are more or
    less specific to NETCONF and may or may not be meaningful in a
    non-NETCONF context. If such a statement appears in a data model
    that is used for the translation procedure, then all YANG rules
    concerning this statement MUST be observed.</t>
    <t>For example, an application that uses YANG for validating JSON
    text may decide to use the "default" statement for defining
    default values of JSON's primitive datatypes (string, number or
    boolean). If it does so, the default values SHALL also be taken
    into account for the validation of semantic constraints such as
    those defined by "must" statements.</t>

    <section anchor="sec.names-ns" title="Names and Namespaces">
      <t>The local part of a JSON name is always identical to the
      local name of the corresponding XML element.</t>
      <t>Each JSON name lives in a namespace which is uniquely
      identified by the name of the YANG module where the
      corresponding data node is defined. If the data node is defined
      in a submodule, then the namespace identifier is the name of the
      main module to which the submodule belongs.</t>
      <t>Most of the time, the namespace of JSON names is
      implicit. The namespace MUST be explicitly specified whenever
      the namespace of a name differs from that of its parent object,
      or whenever no parent object exists. The namespace MUST NOT be
      explicitly specified elsewhere.</t>
      <t>In YANG terms, the namespace SHALL be specified only in the
      names of (i) top-level objects and (ii) objects that augment the
      data tree of another module (see Section 7.15 in <xref
      target="RFC6020"/>).</t>
      <t>Where the namespace has to be specified in JSON text, it
      SHALL be done in the following form:</t>

      <figure anchor="fig.ns-enc"
              title="Encoding a namespace identifier with a local name.">
        <artwork><![CDATA[
        <module name>:<local name>]]></artwork>
      </figure>

      <t>The translation procedure MUST correctly map YANG namespace
      URIs to YANG module names and vice versa.</t>
      <t>When mapping namespaces from JSON text to XML, the resulting
      XML document may use default namespace declarations (via the
      "xmlns" attribute), prefix-based namespace declarations (via
      attributes beginning with "xmlns:"), or any combination thereof
      following the rules stated in <xref target="XMLNS"/>. If
      prefixed names are used, their prefix SHOULD be the one defined
      by the "prefix" statement in the YANG module where each data node
      is defined.</t>

    </section>

    <section anchor="sec.elements"
             title="Mapping XML Elements to JSON Objects">
      <t>XML elements are translated to JSON objects in a straightforward way:
      <list style="symbols">
        <t>XML elements that have no siblings of the same name
        correspond either to a name/value pair or to a JSON object:
        <list style="symbols">
          <t>An XML element which is modeled as a leaf in YANG is
          translated to a name/value pair and the JSON datatype of the
          value is derived from the YANG datatype of the leaf (see
          <xref target="sec.datatypes"/> for the datatype mapping
          rules).</t>
          <t>An XML element which is modeled as a container in YANG is
          translated to an object.</t>
        </list></t>
        <t>A sequence of sibling XML elements with the same name
        (modeled as a list or leaf-list in YANG) corresponds to a JSON
        array. If the sequence is modeled as a leaf-list in YANG, then
        the array elements are primitive values (strings, numbers or
        booleans) whose type depends on the datatype of the leaf-list
        (see <xref target="sec.datatypes"/>). If the sequence is
        modeled as a list in YANG, then the array elements are JSON
        objects.</t>
      </list></t>
    </section>

    <section anchor="sec.datatypes"
             title="Mapping YANG Datatypes to JSON Values">

      <section anchor="sec.numtypes" title="Numeric Types">
        <t>YANG numeric types ("int8", "int16", "int32", "int64",
        "uint8", "uint16", "uint32", "uint64" and "decimal64") are
        mapped to JSON numbers whose decimal representation is the
        YANG canonical form of the number. Hexadecimal values MUST be
        converted to decimal.</t>
      </section>

      <section anchor="sec.string" title='The "string" Type'>
        <t>A "string" value is mapped to an identical JSON string, subject
        to JSON encoding rules.</t>
      </section>

      <section anchor="sec.boolean" title='The "boolean" Type'>
        <t>A "boolean" value is mapped to the corresponding JSON value
        'true' or 'false'.</t>
      </section>

      <section anchor="sec.enum" title='The "enumeration" Type'>
        <t>An "enumeration" value is mapped in the same way as a string
        except that the permitted values are defined by "enum"
        statements in YANG.</t>
      </section>

      <section anchor="sec.bits" title='The "bits" Type'>
        <t>A "bits" value is mapped to a string identical to the lexical
        representation of this value in XML, i.e., a space-separated
        list of bit values.</t>
      </section>

      <section anchor="sec.binary" title='The "binary" Type'>
        <t>A "binary" value is mapped to a JSON string identical to the
        lexical representation of this value in XML, i.e.,
        base64-encoded binary data.</t> 
      </section>

      <section anchor="sec.leafref" title='The "leafref" Type'>
        <t>A "leafref" value is mapped according to the same rules as
        the type of the leaf being referred to, subject to the same
        constraints as the XML value.</t>
      </section>

      <section anchor="sec.idref" title='The "identityref" Type'>
        <t>An "identityref" value is mapped to a string representing the
        qualified name of the identity. Its namespace MAY be expressed
        as shown in <xref target="fig.ns-enc"/>. If the namespace part
        is not present, the namespace of the name of the JSON object
        containing the value is assumed.</t>
      </section>

      <section anchor="sec.empty" title='The "empty" Type'>
        <t>An "empty" value is mapped to '[null]', i.e., an array with
        the 'null' value being its only element.</t>
        <t>This representation was chosen instead of using simply 'null' in
        order to facilitate the use of "empty" leafs in common
        programming languages. When used in a boolean context, the
        '[null]' value, unlike 'null', evaluates to 'true'.</t>
      </section>

      <section anchor="sec.union" title='The "union" Type'>
        <t>YANG "union" type represents a choice among multiple
        alternative types. The actual type of the XML value MUST be
        determined using the procedure specified in Sec. 9.12 of <xref
        target="RFC6020"/> and the mapping rules for that type are
        used.</t>
      </section>

      <section anchor="sec.i-i" title='The "instance-identifier" Type'>
        <t>An "instance-identifier" value is a string representing a
        simplified XPath specification. It is mapped to an analogical
        JSON string in which all occurrences of XML namespace prefixes are
        either removed or replaced with the corresponding module name
        according to the rules of <xref target="sec.names-ns"/>.</t>
        <t>When translating such a value from JSON to XML, all
        components of the instance-identifier MUST be given
        appropriate XML namespace prefixes. It is RECOMMENDED that
        these prefixes be those defined via the "prefix" statement in
        the corresponding YANG modules.</t>
      </section>

    </section>

    <section anchor="sec.ex" title="Example">
      <t>Consider a simple data model defined by the following YANG
      module:</t>

      <figure anchor="fig.exy"
              title="Example YANG module.">
        <artwork><![CDATA[
module ex-json {
  namespace "http://example.com/ex-json";
  prefix ej;
  import ietf-inet-types {
    prefix inet;
  }
  container top {
    list address {
      key "seqno";
      leaf seqno {
        type uint8;
      }
      leaf ip {
        type inet:ip-address;
        mandatory true;
      }
    }
    container phases {
      typedef angle {
        type decimal64 {
          fraction-digits 2;
        }
        units "radians";
      }
      leaf max-phase {
        default "6.28";
        type angle;
      }
      leaf-list phase {
        type angle;
        must ". <= ../max-phase";
        min-elements 1;
      }
    }
  }
}]]></artwork>
      </figure>

      <t>By using the translation procedure defined in this document,
      we can conclude that the following JSON text is valid according
      to the data model:</t>

      <figure anchor="fig.exj"
              title="Example JSON text.">
        <artwork><![CDATA[
{
  "ex-json:top": {
    "address": [
      {
        "seqno": 1,
        "ip": "192.0.2.1"
      },
      {
        "seqno": 2,
        "ip": "2001:db8:0:1::1"
      }
    ],
    "phases": {
      "phase": [0.79, 1.04, 3.14]
    }
  }
}]]></artwork>
      </figure>

      <t>Note that the semantic constraint specified by the "must"
      statement in <xref target="fig.exy"/> is satisfied by all
      elements of the "phase" array because the default value of 6.28 is
      used for the absent "max-phase" leaf.</t>
    </section>

    <section anchor="sec.iana" title="IANA Considerations">
      <t>TBD.</t>
    </section>

    <section anchor="sec.cons" title="Security Considerations">
      <t>TBD.</t>
    </section>

    <section anchor="sec.ack" title="Acknowledgments">
    <t>The author wishes to thank Martin Bjorklund and Phil Shafer for
    their helpful comments and suggestions.</t>
    </section>

  </section>
</middle>

<back>

  <references title="Normative References">

    <reference anchor='RFC2119'>
      <front>
        <title abbrev='RFC Key Words'>Key words for use in RFCs to
        Indicate Requirement Levels</title>
        <author initials='S.' surname='Bradner' fullname='Scott Bradner'>
          <organization/>
        </author>
        <date year='1997' month='March' />
      </front>
      <seriesInfo name='BCP' value='14' />
      <seriesInfo name='RFC' value='2119' />
      <format type='HTML'
              target='http://www.ietf.org/html/rfc2119' />
    </reference>

    <reference anchor="RFC4627">
      <front>
        <title>The application/json Media Type for JavaScript Object
        Notation (JSON)</title>
        <author initials="D." surname="Crockford" fullname="Douglas Crockford">
          <organization/>
        </author>
        <date month="July" year="2006"/>
      </front>
      <seriesInfo name='RFC' value='4627' />
      <format type='HTML'
              target='http://www.ietf.org/html/rfc4627' />
    </reference>

    <reference anchor='RFC6020'>
      <front>
        <title>YANG - A Data Modeling Language for Network
        Configuration Protocol (NETCONF)</title>
        <author role="editor" initials='M' surname='Bjorklund'
                fullname='Martin Bjorklund'>
          <organization>Tail-f Systems</organization>
        </author>
        <date month='September' year='2010' />
      </front>
      <seriesInfo name='RFC' value='6020'/>
      <format type='HTML'
              target='http://tools.ietf.org/html/rfc6020' />
    </reference>

    <reference anchor="RFC6241">
      <front>
        <title>NETCONF Configuration Protocol</title>
        <author initials="R." surname="Enns" fullname="R. Enns">
          <organization/>
        </author>
        <author initials="M." surname="Bjorklund" fullname="M. Bjorklund">
          <organization/>
        </author>
        <author initials="J." surname="Schoenwaelder" fullname="J. Schoenwaelder">
          <organization/>
        </author>
        <author initials="A." surname="Bierman" fullname="A. Bierman">
          <organization/>
        </author>
        <date year="2011" month="June"/>
      </front>
      <seriesInfo name="RFC" value="6241"/>
      <format type="HTML" target="http://www.ietf.org/html/rfc6241"/>
    </reference>

    <reference anchor='XML'
               target='http://www.w3.org/TR/2006/REC-xml-20060816'>
      <front>
        <title>Extensible Markup Language (XML) 1.0 (Fifth Edition)</title>
        <author initials='T.' surname='Bray' fullname='Tim Bray'>
          <organization />
        </author>
        <author initials='J.' surname='Paoli' fullname='Jean Paoli'>
          <organization />
        </author>
        <author initials='C.' surname='Sperberg-McQueen'
                fullname='C. M. Sperberg-McQueen'>
          <organization />
        </author>
        <author initials='E.' surname='Maler' fullname='Eve Maler'>
          <organization />
        </author>
        <author initials='F.' surname='Yergeau' fullname='Francois Yergeau'>
          <organization />
        </author>
        <date month='November' day='26' year='2008' />
      </front>
      <seriesInfo name='World Wide Web Consortium Recommendation'
                  value='REC-xml-20081126' />
      <format type='HTML'
              target='http://www.w3.org/TR/2008/REC-xml-20081126' />
    </reference>

    <reference anchor='XMLNS'
               target='http://www.w3.org/TR/2009/REC-xml-names-20091208'>
      <front>
        <title>Namespaces in XML 1.0 (Third Edition)</title>
        <author initials='T.' surname='Bray' fullname='Tim Bray'>
          <organization/>
        </author>
        <author initials='D.' surname='Hollander' fullname='Dave Hollander'>
          <organization/>
        </author>
        <author initials='A.' surname='Layman' fullname='Andrew Layman'>
          <organization/>
        </author>
        <author initials='R.' surname='Tobin' fullname='Richard Tobin'>
          <organization/>
        </author>
        <author initials='H. S.' surname='Thompson'
                fullname='Henry S. Thompson'>
          <organization/>
        </author>
        <date month='December' day='8' year='2009' />
      </front>
      <seriesInfo name='World Wide Web Consortium Recommendation'
                  value='REC-xml-names-20091208' />
      <format type='HTML'
              target='http://www.w3.org/TR/2009/REC-xml-names-20091208' />
    </reference>

  </references>

  <references title="Informative References">

    <reference anchor='XPath'
               target='http://www.w3.org/TR/1999/REC-xpath-19991116'>
      <front>
        <title>XML Path Language (XPath) Version 1.0</title>
        <author initials='J.' surname='Clark' fullname='James Clark'>
          <organization/>
        </author>
        <date month='November' day='16' year='1999' />
      </front>
      <seriesInfo name='World Wide Web Consortium Recommendation'
                  value='REC-xpath-19991116' />
      <format type='HTML'
              target='http://www.w3.org/TR/1999/REC-xpath-19991116' />
    </reference>

  </references>

</back>

</rfc>

PAFTECH AB 2003-20262026-04-23 14:33:44