<?xml version="1.0" encoding="US-ASCII"?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd" >

<?xml-stylesheet type='text/xsl' href='rfc2629.xslt' ?>

<?rfc strict="yes" ?>
<?rfc toc="yes"?>
<?rfc tocdepth="4"?>
<?rfc symrefs="yes"?>
<?rfc sortrefs="yes" ?>
<?rfc compact="yes" ?>
<?rfc subcompact="no" ?>

<rfc category="info" docName="draft-schiltknecht-lgr-json-00" ipr="trust200902">
    <front>
        <title abbrev="JSON for LGR">A JSON format for LGR files</title>

        <author fullname="Audric Schiltknecht" initials="A." role="editor"
                surname="Schiltknecht">
        <organization>Viagenie</organization>
        <address>
            <postal>
                <street>246 Aberdeen</street>
                <city>Quebec</city>
                <region>QC</region>
                <code>G1R 2E1</code>
                <country>Canada</country>
            </postal>
            <email>audric.schiltknecht@viagenie.ca</email>
            <uri>http://viagenie.ca</uri>
        </address>
        </author>
        <date month="August" year="2015" />

        <area>General</area>
        <keyword>lgr</keyword>

        <abstract>
            <t>This document defines a JSON format for LGRs (Label Generation Rules). LGRs are used to represent rules for validating identifier labels and their alternate representations. These LGRs are expressed in XML as defined in <xref target="I-D.ietf-lager-specification"/>.</t>
        </abstract>
    </front>

    <middle>
        <section title="Introduction">
            <t>This document describes a JSON format for representing LGRs as described in <xref target="I-D.ietf-lager-specification"/>.</t>

            <t>The key design considerations taken into account in this document are
                <list style="symbols">
                    <t>Round-tripping (converting an XML LGR to JSON and back) will yield the same semantic result as the starting point. All XML elements, attributes and values are guaranted to be preserved.</t>
                    <t>The ordering of elements MUST be preserved, as it is of importance in the original LGR XML specification.</t>
                </list>
            </t>

            <t>The terms "JSON object", "JSON array", "JSON member", and "JSON value" are to be interpreted as described in <xref target="RFC7159"/>.</t>

            <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">RFC 2119</xref>.</t>
        </section>

        <section anchor="XML_to_JSON" title="Converting from XML to JSON">
            <t>This section explains how to convert an XML LGR to JSON, by defining a simple mapping between XML nodes and JSON objects.</t>

            <section title="Basic structure">
                <t>As a valid JSON object, the basic layout of an LGR in JSON is as follows:</t>
                <figure>
                    <artwork><![CDATA[
{
    "meta": [
        ...
    ],
    "data": [
        ...
    ],
    "rules": [
        ...
    ]
}
        ]]></artwork>
                </figure>

                <t>As expressed in <xref target="I-D.ietf-lager-specification"/>, only the "data" object is mandatory.</t>

                <t>The conversion scheme follows these general conventions:
                    <list style="symbols">
                        <t>Each XML element is a JSON array with 3 members: the name of the element (called "name"), a (potentially empty) JSON object representing the attributes of the element (called "attributes"), and the value of the element (called "value").</t>
                        <t>The type of the "value" element (last element of the JSON array) depends on the context, and will be defined in each of the following sections.</t>
                    </list>
                </t>
				<t>The use of JSON arrays which are ordered sequences allows to keep the order of the declarations from the XML.</t>

                <t>For example, the XML extract:</t>
                <figure><artwork>&lt;char cp="0063"/&gt;</artwork></figure>
                <t>will be converted to </t>
                <figure><artwork>["char", {"cp": "0063"}, []]</artwork></figure>
            </section>

            <section title="Metadata">

                <t>The type of the "value" is a string, except for the "references" element where an array of the JSON-converted "reference" child XML elements is used.</t>
                <t>Given the following XML "meta" element:</t>
                <figure><artwork>
&lt;meta&gt;
    &lt;version comment="initial version"&gt;1&lt;/version&gt;
    &lt;date&gt;2010-01-01&lt;/date&gt;
    &lt;language&gt;sv&lt;/language&gt;
    &lt;scope type="domain"&gt;example&lt;/scope&gt;
    &lt;validity-start&gt;2010-01-01&lt;/validity-start&gt;
    &lt;validity-end&gt;2013-12-31&lt;/validity-end&gt;
    &lt;description type="text/html"&gt;
        &lt;![CDATA[
        This language table was developed with the
        &lt;a href="http://swedish.example/"&gt;Swedish
        examples institute&lt;/a&gt;.
        ]]&gt;
    &lt;/description&gt;
    &lt;description&gt;
    &lt;unicode-version&gt;6.3.0&lt;/unicode-version&gt;
    &lt;references&gt;
      &lt;reference id="0" comment="the most recent" &gt;The
            Unicode Standard 6.2&lt;/reference&gt;
      &lt;reference id="1" &gt;RFC 5892&lt;/reference&gt;
      &lt;reference id="2" &gt;Big-5: Computer Chinese Glyph
         and Character Code Mapping Table, Technical Report
         C-26, 1984&lt;/reference&gt;
    &lt;/references&gt;
 &lt;/meta&gt;
                </artwork></figure>

                <t>the converted JSON "meta" array is:</t>
                <figure><artwork><![CDATA[
"meta":
[
    ["version", {"comment": "initial version"}, "1"],
    ["date", {}, "2010-01-01"],
    ["language", {}, "sv"],
    ["scope", {"type": "domain"}, "example"],
    ["validity-start", {}, "2010-01-01"],
    ["validity-end", {}, "2013-12-31"],
    ["description", {"type": "text/html"}, "This language table was developed with the <a href=\"http://swedish.example/\">Swedish examples institute</a>."],
    ["unicode-version", {}, "6.3.0"],
    ["references", {}, [
        ["reference", {"id": "0", "comment": "the most recent"}, "The Unicode Standard 6.2"],
        ["reference", {"id": "1"}, "RFC 5892"],
        ["reference", {"id": "2"}, "Big-5: Computer Chinese Glyph and Character Code Mapping Table, Technical Report C-26, 1984"]
    ]]
]]]></artwork></figure>
            </section>
            <section title="Code Points and variants">
                <t>All code point data is contained in the "data" section of an LGR. There are two types of data:
                    <list style="symbols">
                        <t>Code points ("char" elements), which can have variants.</t>
                        <t>Range of code points ("range" elements), defined by their first and last code point, and cannot have variants.</t>
                    </list>
                As a consequence, the type of the "value" is an array, containing the variants of a "char" elements. For variants, it is an empty array.
                </t>

                <t>Typical conversions are described in the following examples:</t>
                <figure>
                    <artwork><![CDATA[
<data>
    <char cp="002D"/>
    <range first-cp="0030" last-cp="0039"/>
    <char cp="006C 00B7 006C" comment="Catalan middle dot"/>
</data>
]]></artwork>
                </figure>

                <figure>
                    <artwork><![CDATA[
"data":
[
    ["char", {"cp": "002D"}, []],
    ["range", {"first-cp": "0030", "last-cp": "0039"}, []],
    ["char", {"cp": "006C 00B7 006C", "comment": "Catalan middle dot"}, []]
]
        ]]></artwork>
                </figure>

                <t>For variants:</t>
                <figure>
                    <artwork><![CDATA[
<char cp="00F6">
    <var cp="006F 0065" type="block"/>
</char>]]></artwork>
                </figure>

                <figure>
                    <artwork><![CDATA[
["char", {"cp": "00F6"}, [
    ["var", {"cp": "006F 0065", "type": "block"}, []]
]]
]]></artwork>
                </figure>
            </section>
            <section title="Whole Label Rules and actions">
                <t>Rules, classes and actions are defined in the "rules" section of an LGR, as an array of JSON objets.</t>

                <t>The "value" element will have the following types:
                    <list style="symbols">
                        <t>For rules, an array of the rule's match operators.</t>
                        <t>For classes, an array of the class' codepoints, or a string if the shorthand notation is used.</t>
                        <t>For actions, an empty array since actions do not have any value or child.</t>
                    </list>
                </t>

                <figure>
                    <artwork>
  &lt;rules&gt;
    &lt;rule name="catalan-middle-dot" ref="0"&gt;
        &lt;look-behind&gt;
            &lt;char cp="006C" /&gt;
        &lt;/look-behind&gt;
        &lt;anchor /&gt;
        &lt;look-ahead&gt;
            &lt;char cp="006C" /&gt;
        &lt;/look-ahead&gt;
    &lt;/rule&gt;

    &lt;class name="virama" property="ccc:9" /&gt;
    &lt;rule name="joiner"  ref="1" &gt;
        &lt;look-behind&gt;
            &lt;class by-ref="virama" /&gt;
        &lt;/look-behind&gt;
        &lt;anchor /&gt;
    &lt;/rule&gt;

    &lt;difference name="consonants"&gt;
         &lt;class comment="all letters"&gt;0061-007A&lt;/class&gt;
         &lt;class comment="all vowels"&gt;
                 0061 0065 0069 006F 0075
         &lt;/class&gt;
     &lt;/difference&gt;

     &lt;rule name="three-or-more-consonants"&gt;
         &lt;start /&gt;
         &lt;class by-ref="consonants" count="3+" /&gt;
         &lt;end /&gt;
    &lt;/rule&gt;

    &lt;rule name="non-preferred"
          comment="matches any non-preferred code point"&gt;
        &lt;complement comment="non-preferred" &gt;
            &lt;class from-tag="preferred" /&gt;
        &lt;/complement&gt;
    &lt;/rule&gt;

    &lt;action disp="consonants" 
            match="three-or-more-consonants" /&gt;
    &lt;action disp="block" any-variant="block" /&gt;
    &lt;action disp="activate" all-variants="allocate"
            not-match="non-preferred" /&gt;
  &lt;/rules&gt;</artwork>
                </figure>
                <figure>
                    <artwork><![CDATA[
"rules":
[
    [
        "rule", {
            "name": "catalan-middle-dot",
            "ref": "0"
        },
        [
            [
                "look-behind", {},
                [
                    ["char", {"cp": "006C"}, []]
                ]
            ],
            [
                "anchor", {}, []
            ],
            [
                "look-ahead", {},
                [
                    ["char", {"cp": "006C"}, []]
                ]
            ]
        ]
    ],

    [
        "class",
        {
            "name": "virama",
            "property": "ccc:9"
        },
        []
    ],
    [
        "rule",
        {
            "name": "joiner",
            "ref": "1"
        },
        [
            [
                "look-behind",
                {},
                [
                    [
                        "class",
                        {"by-ref": "virama"},
                        []
                    ]
                ]
            ],
            ["anchor", {}, []]
        ]
    ],

    [
        "difference",
        {"name": "consonants"},
        [
            [
                "class",
                {"comment": "all letters"},
                "0061-007A"
            ],
            [
                "class",
                {"comment": "all vowels"},
                "0061 0065 0069 006F 0075"
            ]
        ]
    ],

    [
        "rule",
        {"name": "three-or-more-consonants"},
        [
            [
                "start", {}, []
            ],
            [
                "class",
                {
                    "by-ref": "consonants",
                    "count": "3+"
                },
                []
            ],
            [
                "end", {}, []
            ]
        ]
    ],

    [
        "rule",
        {
            "name": "non-preferred",
            "comment": "matches any non-prefered code point"
        },
        [
            [
                "complement",
                {"comment": "non-preferred"},
                [
                    [
                        "class",
                        {"from-tag": "preferred"},
                        []
                    ]
                ]
            ]
        ]
    ],

    [
        "action",
        {
            "disp": "consonants",
            "match": "three-or-more-consonants"
        },
        []
    ],
    [
        "action",
        {
            "disp": "block",
            "any-variant": "block"
        },
        []
    ],
    [
        "action",
        {
            "disp": "activate",
            "all-variants": "allocate",
            "not-match": "non-preferred"
        },
        []
    ]
]
]]></artwork>
                </figure>
            </section>
        </section>

        <section anchor="JSON_to_XML" title="Converting from JSON to XML">
            <t>When converting a JSON LGR to XML format, proper escaping of text content MUST be done.</t>

            <t>An empty "value" (empty list, empty object or empty string) is an XML element without value nor child.</t>
        </section>
        <section anchor="Acknowledgements" title="Acknowledgements">
            <t>TODO</t>
        </section>

        <!-- Possibly a 'Contributors' section ... -->

        <section anchor="IANA" title="IANA Considerations">
            <t>This memo includes no request to IANA.</t>
        </section>

        <section anchor="Security" title="Security Considerations">
			<t>Since JSON is used as a format, the security risks discussed in <xref target="RFC7159"/> are to be considered.</t>
        </section>
    </middle>

    <back>
        <references title="Normative References">
            <?rfc include="reference.RFC.2119.xml"?>
            <?rfc include="reference.RFC.7159.xml"?>
            <?rfc include="reference.I-D.ietf-lager-specification.xml"?>
        </references>
        <section title="ABNF syntax">
            <t>TODO</t>
            <figure>
				<artwork><![CDATA[

				]]></artwork>
			</figure>
        </section>
    </back>
</rfc>
