org.sapia.cocoon.serialization.json
Class JsonSerializer
java.lang.Object
org.apache.avalon.framework.logger.AbstractLogEnabled
org.apache.cocoon.xml.AbstractXMLProducer
org.apache.cocoon.xml.AbstractXMLPipe
org.apache.cocoon.serialization.AbstractSerializer
org.sapia.cocoon.serialization.json.JsonSerializer
- All Implemented Interfaces:
- org.apache.avalon.excalibur.pool.Poolable, org.apache.avalon.excalibur.pool.Recyclable, org.apache.avalon.framework.logger.LogEnabled, org.apache.cocoon.serialization.Serializer, org.apache.cocoon.sitemap.SitemapModelComponent, org.apache.cocoon.sitemap.SitemapOutputComponent, org.apache.cocoon.xml.XMLPipe, org.apache.cocoon.xml.XMLProducer, org.apache.excalibur.xml.sax.XMLConsumer, org.xml.sax.ContentHandler, org.xml.sax.ext.LexicalHandler
public class JsonSerializer
- extends org.apache.cocoon.serialization.AbstractSerializer
- implements org.apache.cocoon.sitemap.SitemapModelComponent
This serializer generates JSON data out of an XML document. The serializer has
the following features/limitations:
- The serializer creates a JSON graph that maps the structure of the XML document: a JSON
object is created for each XML element in the document.
- XML namespaces are ignored.
- A JSON array is created for child elements that have the same name, under a given parent element. If there is
only one child element of a given name under another element, then no array is created (a single JSON object is created
for that single child element).
- A "cdata" field is created to hold the content of the CDATA (text nested in a given XML element) of the
corresponding XML element.
For example, given the following XML document:
Dinner next week
Will yield the following JSON data:
{
"addressBook": {
"contact": {
"firstName": "bill",
"lastName": "gates",
"cdata": "Dinner next week"
}
}
}
As the following XML:
Will yield:
{
"addressBook": {
"contact": [
{ "firstName": "bill", "lastName": "gates" },
{ "firstName": "linus", "lastName": "torvalds" }
]
}
}
Note that this serializer takes the following parameters (as part of the sitemap configuration):
content-type: by default, set to application/json.
global-var-name: the name of the Javascript global variable, if the JSON data is to be returned
as a Javascript resource (see below for explanations).
This serializer allows for the JSON data to be sent as a Javascript resource (i.e.: the corresponding JSON may then
included as a Javascript resource in a HTML page). For this to be possible, the JSON data is returned as a Javascript global
variable - in this case, the global-var-name sitemap variable must be set and correspond to the name of the Javascript
variable that will be sent to the browser. For example, given our address book data (see above), setting the global-var-name
parameter to AddressBookData would trigger the generation of the following data:
AddressBookData = {
"addressBook": {
"contact": [
{ "firstName": "bill", "lastName": "gates" },
{ "firstName": "linus", "lastName": "torvalds" }
]
}
}
Example Spring configuration:
Example Sitemap configuration:
| Fields inherited from class org.apache.cocoon.serialization.AbstractSerializer |
output |
| Fields inherited from class org.apache.cocoon.xml.AbstractXMLProducer |
contentHandler, EMPTY_CONTENT_HANDLER, lexicalHandler, xmlConsumer |
| Fields inherited from interface org.apache.cocoon.serialization.Serializer |
ROLE |
|
Method Summary |
void |
characters(char[] ch,
int start,
int length)
|
void |
endDocument()
|
void |
endElement(java.lang.String uri,
java.lang.String localName,
java.lang.String qName)
|
java.lang.String |
getMimeType()
|
void |
recycle()
|
void |
setOutputStream(java.io.OutputStream ous)
|
void |
setup(org.apache.cocoon.environment.SourceResolver resolver,
java.util.Map objectModel,
java.lang.String src,
org.apache.avalon.framework.parameters.Parameters parameters)
|
boolean |
shouldSetContentLength()
|
void |
startDocument()
|
void |
startElement(java.lang.String uri,
java.lang.String localName,
java.lang.String qname,
org.xml.sax.Attributes attrs)
|
| Methods inherited from class org.apache.cocoon.xml.AbstractXMLPipe |
comment, endCDATA, endDTD, endEntity, endPrefixMapping, ignorableWhitespace, processingInstruction, setDocumentLocator, skippedEntity, startCDATA, startDTD, startEntity, startPrefixMapping |
| Methods inherited from class org.apache.cocoon.xml.AbstractXMLProducer |
setConsumer, setContentHandler, setLexicalHandler |
| Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled |
enableLogging, getLogger, setupLogger, setupLogger, setupLogger |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Methods inherited from interface org.xml.sax.ContentHandler |
endPrefixMapping, ignorableWhitespace, processingInstruction, setDocumentLocator, skippedEntity, startPrefixMapping |
| Methods inherited from interface org.xml.sax.ext.LexicalHandler |
comment, endCDATA, endDTD, endEntity, startCDATA, startDTD, startEntity |
| Methods inherited from interface org.apache.cocoon.xml.XMLProducer |
setConsumer |
PARAM_GLOBAL_VAR_NAME
public static final java.lang.String PARAM_GLOBAL_VAR_NAME
- See Also:
- Constant Field Values
PARAM_CONTENT_TYPE
public static final java.lang.String PARAM_CONTENT_TYPE
- See Also:
- Constant Field Values
MIME_TYPE_JSON
public static final java.lang.String MIME_TYPE_JSON
- See Also:
- Constant Field Values
JsonSerializer
public JsonSerializer()
setup
public void setup(org.apache.cocoon.environment.SourceResolver resolver,
java.util.Map objectModel,
java.lang.String src,
org.apache.avalon.framework.parameters.Parameters parameters)
throws org.apache.cocoon.ProcessingException,
org.xml.sax.SAXException,
java.io.IOException
- Specified by:
setup in interface org.apache.cocoon.sitemap.SitemapModelComponent
- Throws:
org.apache.cocoon.ProcessingException
org.xml.sax.SAXException
java.io.IOException
recycle
public void recycle()
- Specified by:
recycle in interface org.apache.avalon.excalibur.pool.Recyclable- Overrides:
recycle in class org.apache.cocoon.serialization.AbstractSerializer
getMimeType
public java.lang.String getMimeType()
- Specified by:
getMimeType in interface org.apache.cocoon.sitemap.SitemapOutputComponent- Overrides:
getMimeType in class org.apache.cocoon.serialization.AbstractSerializer
setOutputStream
public void setOutputStream(java.io.OutputStream ous)
throws java.io.IOException
- Specified by:
setOutputStream in interface org.apache.cocoon.sitemap.SitemapOutputComponent- Overrides:
setOutputStream in class org.apache.cocoon.serialization.AbstractSerializer
- Throws:
java.io.IOException
startElement
public void startElement(java.lang.String uri,
java.lang.String localName,
java.lang.String qname,
org.xml.sax.Attributes attrs)
throws org.xml.sax.SAXException
- Specified by:
startElement in interface org.xml.sax.ContentHandler- Overrides:
startElement in class org.apache.cocoon.xml.AbstractXMLPipe
- Throws:
org.xml.sax.SAXException
endElement
public void endElement(java.lang.String uri,
java.lang.String localName,
java.lang.String qName)
throws org.xml.sax.SAXException
- Specified by:
endElement in interface org.xml.sax.ContentHandler- Overrides:
endElement in class org.apache.cocoon.xml.AbstractXMLPipe
- Throws:
org.xml.sax.SAXException
startDocument
public void startDocument()
throws org.xml.sax.SAXException
- Specified by:
startDocument in interface org.xml.sax.ContentHandler- Overrides:
startDocument in class org.apache.cocoon.xml.AbstractXMLPipe
- Throws:
org.xml.sax.SAXException
endDocument
public void endDocument()
throws org.xml.sax.SAXException
- Specified by:
endDocument in interface org.xml.sax.ContentHandler- Overrides:
endDocument in class org.apache.cocoon.xml.AbstractXMLPipe
- Throws:
org.xml.sax.SAXException
characters
public void characters(char[] ch,
int start,
int length)
throws org.xml.sax.SAXException
- Specified by:
characters in interface org.xml.sax.ContentHandler- Overrides:
characters in class org.apache.cocoon.xml.AbstractXMLPipe
- Throws:
org.xml.sax.SAXException
shouldSetContentLength
public boolean shouldSetContentLength()
- Specified by:
shouldSetContentLength in interface org.apache.cocoon.sitemap.SitemapOutputComponent- Overrides:
shouldSetContentLength in class org.apache.cocoon.serialization.AbstractSerializer
Copyright © 2008. All Rights Reserved.