Overview | Package | Class | Tree | Serialized | Deprecated | Index | Help
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES

## Package com.ibm.portal.streaming.json

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

See:
Description

Class Summary
EmptyJsonHandler | Deprecated. use `EmptyJsonContentHandler` instead

## 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()
             */

          Overview
        Package
          Class
          Tree
          Serialized
          Deprecated
          Index
          Help

     PREV PACKAGE
     NEXT PACKAGE

      FRAMES
     NO FRAMES