Package com.ibm.portal.streaming.json

This package defines an API for writing and parsing JSON in a stream based manner.

See:
          Description

Interface Summary
DefaultJsonContentHandler Convenience interface to the JsonContentHandler that allows to write high-level java objects.
JsonContentHandler Content handler that defines events for JSON data structures.
JsonContentHandlerEx  
JsonContentHandlerProvider Interface on a class that manages JsonContentHandler callbacks.
JsonDocumentContentHandler Implementation of a JsonContentHandler that builds a document graph out of a sequence of JSON callbacks.
JSONHandler Deprecated. use JsonContentHandler instead
JSONHandlerFactory Factory that generates handlers for producing and parsing JSON events in a stream based manner.
JsonProducer Represents a buffered JSON stream that can be copied onto
JSONReader Deprecated. use JsonParserReader instead
JsonStream Base interface for JSON streams
 

Class Summary
EmptyJsonHandler Deprecated. use EmptyJsonContentHandler instead
JSONInputSource Deprecated.  
JsonParserOutputStream Parser that decomposes a JSON stream into callbacks to JsonContentHandler.
JsonParserReader Implementation of a pull parser to decode JSON data as defined in RFC 4627.
JsonParserReader.Entry Represents an entry in this input stream.
JsonParserWriter Parser that decomposes a JSON stream into callbacks to JsonContentHandler.
 

Enum Summary
JsonParserReader.TYPE Type of the next entry
 

Package com.ibm.portal.streaming.json Description

This package defines an API for writing and parsing JSON in a stream based manner.

Example for writing a simple JSON document:

   final JSONHandlerFactory fct = getJSONHandlerFactory();
   final Writer writer = new OutputStreamWriter(System.out, "UTF-8");
   final DefaultJsonContentHandler handler = fct.createJsonContentHandler(writer);
   handler.startDocument();
   handler.startObject();
   handler.jsonMember("id", 3);
   handler.jsonMember("unique-name", "number three");
   handler.startMember("preferences");
   handler.jsonArray(new String[]{"portal", "portlet"});
   handler.endMember();
   handler.endObject();
   handler.endDocument();
   writer.flush();
   writer.close();
This would give the following output:
{
        "id": 3,
        "unique-name": "number three",
        "preferences": ["portal", "portlet"]
}

Example for parsing the above mentioned simple JSON document:

        public void parse() throws Exception {
        final String json = "{\"id\":3,\"unique-name\":\"number three\",\"preferences\":[\"portal\",\"portlet\"]}";
        final StringReader reader = new StringReader(json);
        final JsonParserReader parser = fct.createJsonParserReader(reader);
        List<SampleJsonObject> objects = null;
        SampleJsonObject current = null;
        String s;
        for (JsonParserReader.Entry e = parser.getNextEntry(); e != null; e = parser.getNextEntry()) {
            switch (e.getType()) {
            case START_OBJECT:
                current = new SampleJsonObject();
                break;
            case START_MEMBER:
                s = e.readString();
                if ("id".equals(s)) {
                    e = parser.getNextEntry();
                    final Number number = e.readNumber();
                    current.setId(number.intValue());
                } else if ("unique-name".equals(s)) {
                    e = parser.getNextEntry();
                    final String uniqueName = e.readString();
                    current.setUniqueName(uniqueName);
                } else if ("preferences".equals(s)) {
                    final Collection<String> preferences = parsePreferences(parser);
                    current.setPreferences(preferences);
                }
                break;
            case END_OBJECT:
                objects.add(current);
                current = null;
                break;
            case START_DOCUMENT:
                objects = new ArrayList<SampleJsonObject>();
                break;
            case END_DOCUMENT:
                System.out.println("Read these objects: " + objects);
                break;
            default:
                break;
            }
        }
        parser.close();
    }

    private Collection<String> parsePreferences(final JsonParserReader parser) throws IOException {
        Collection<String> preferences = null;
        for (JsonParserReader.Entry e = parser.getNextEntry(); e != null; e = parser.getNextEntry()) {
            switch (e.getType()) {
            case START_ARRAY:
                preferences = new ArrayList<String>();
                break;
            case END_ARRAY:
                // We finished
                return preferences;
            case START_STRING:
                preferences.add(e.readString());
                break;
            }
        }
        return null;
    }

    static class SampleJsonObject {
        private int id;
        private String uniqueName;
        private Collection<String> preferences;

        /**
         * @return the id
         */
        public int getId() {
            return id;
        }

        /**
         * @param id
         *            the id to set
         */
        public void setId(int id) {
            this.id = id;
        }

        /**
         * @return the uniqueName
         */
        public String getUniqueName() {
            return uniqueName;
        }

        /**
         * @param uniqueName
         *            the uniqueName to set
         */
        public void setUniqueName(String uniqueName) {
            this.uniqueName = uniqueName;
        }

        /**
         * @return the preferences
         */
        public Collection<String> getPreferences() {
            return preferences;
        }

        /**
         * @param preferences
         *            the preferences to set
         */
        public void setPreferences(Collection<String> preferences) {
            this.preferences = preferences;
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.lang.Object#toString()
         */