Skip to content

JSON parser documentation

gibello edited this page Dec 14, 2011 · 2 revisions

The framework provides a (quite basic) JSON parser, that works in "push" mode (like SAX for XML). Package: cades.icar.rest.json.

The developer must implement the cades.icar.rest.json.EventtHandler interface, to receive parsing events (eg. the startObject() method will be invoked at the beginning of a JSON object, the key(String key) and simpleValue(String val) methods upon each occurrence of a "name/simple value" pair, etc...)

To make it simpler, one may extend the cades.icar.rest.json.Default Handler class: it implements all parsing methods, but does nothing. You then just have to override methods that correspond to parsing events you wish to intercept.

Example :

The following code accepts JSON strings that contain name/address information, of the following form: { name: "My name", address: { city: "My city", country: "My country" } }

package cades.icar.rest.json.sample;

import java.io.ByteArrayInputStream;
import cades.icar.rest.json.JsonParser;

/**
 * This example parses JSON-encoded name/address information, with the following format:
 * { name: "My name", address: { city: "My city", country: "My country" } }
 */
public class NameAddressHandler extends cades.icar.rest.json.DefaultHandler {
    
    String name_;
    String city_;
    String country_;
    
    String lastKey_;
    int level_;
    
    @Override
    public void startObject() {
        level_ ++;
    }

    @Override
    public void endObject() {
        level_ --;
    }

    @Override
    public void key(String key) {
        lastKey_ = key;
    }

    @Override
    public void simpleValue(String val) throws Exception {
        if("name".equals(lastKey_)) {
            if(level_ > 1) throw new Exception("Syntax error: name should be at 1st level");
            name_ = val;
        }
        else if("city".equals(lastKey_)) city_ = val;
        else if("country".equals(lastKey_)) country_ = val;
    }

    // Specific method (not from the EventHandler interface)
    public NameAddress getResult() {
        return new NameAddress(name_, city_, country_);
    }

    // Test main program.
    public static void main(String args[]) throws Exception {
        JsonParser parser = new JsonParser();
        NameAddressHandler handler = new NameAddressHandler();
        String json = (args.length > 0
          ? args[0]
          : "{ name: \"Oscar Wilde\", address: { city: \"London\", country: \"UK\" }}" );
        parser.parse(new ByteArrayInputStream(json.getBytes()), handler);
        System.out.println(handler.getResult());
    }
}

// Class that represents a name/address information (name, city, country) and formats it.
class NameAddress {

    String name_;
    String city_;
    String country_;

    public NameAddress(String name, String city, String country) {
        this.name_ = name;
        this.city_ = city;
        this.country_ = country;
    }

    public String toString() {
        return name_ + " lives in " + city_ + ", " + country_;
    }
}
Clone this wiki locally