c:map

<c:map>
  <!-- Content: sequence-constructor -->
</c:map>

Creates an expando object.

Category
instruction
Permitted parent elements
Any XCST element whose content model is sequence-constructor
Any literal result element

Building Expando Objects

c:map supports two kinds of expando objects. By default, it creates an object whose properties can hold any kind of value. Or you can create JObject objects for JSON programming.

Example: Building an object to hold HTML attributes
<c:variable name='amountAttribs'>
   <c:map>
      <c:map-entry key='"class"'>refresh</c:map-entry>
      <c:if test='readOnly'>
         <c:map-entry key='"readonly"'>readonly</c:map-entry>
      </c:if>
   </c:map>
</c:variable>
<a:editor for='amount' attributes='amountAttribs'/>
Example: Building a JObject
<c:variable name='map' as='JObject'>
   <c:map>
      <c:map-entry key='"name"'>John</c:map-entry>
   </c:map>
</c:variable>

<c:assert test='((JValue)map["name"]).Value == "John"'/>

JSON Serialization

If c:map is used when constructing complex or simple content then it serializes directly to JSON.

Example: JSON output

Use the text output method to serialize JSON.

<c:output method='text'/>

<c:template name='c:initial-template'>
   <c:map>
      <c:map-entry key='"name"'>John</c:map-entry>
   </c:map>
</c:template>

<!-- Outputs: {"name":"John"} -->
Example: Building JSON for an attribute
<div>
   <c:attribute name='data-options'>
      <c:map>
         <c:map-entry key='"name"'>John</c:map-entry>
      </c:map>
   </c:attribute>
</div>

<!-- Outputs: <div data-options="{&quot;name&quot;:&quot;John&quot;}"/> -->
Example: Building a JSON string
<c:variable name='json'>
   <c:value-of>
      <c:map>
         <c:map-entry key='"name"'>John</c:map-entry>
      </c:map>
   </c:value-of>
</c:variable>

<c:assert test='json is string'/>
<c:assert test='json == "{\"name\":\"John\"}"'/>

Error Conditions

It is a compilation error if the required item type of the containing sequence constructor is not one of, or a super class of, Object or JObject.

It is a run-time error if the result of evaluating the sequence constructor includes values other than those returned by c:map-entry.

See Also