import java.io.*;
import javax.json.*;
import javax.json.stream.*;


/**
 * An example of element-based parsing using JSON-P.
 *
 * @author  Prof. David Bernstein, James Madison University
 */
public class ElementBasedExample
{
    /**
     * The entry point of the application.
     *
     * @param args  The command line arguments (which are ignored)
     */
    public static void main(String[] args) throws Exception
    {
        BufferedReader in = new BufferedReader(
            new FileReader(
                new File("../../jsonexamples/basics/person.json")));

        // Create the JsonParser
        JsonParser parser = Json.createParser(in);

        // Process events
        while (parser.hasNext())
        {
            JsonParser.Event evt = parser.next();
            if (evt == JsonParser.Event.KEY_NAME)
            {
                String name = parser.getString();
                if (name.equals("firstName"))
                {
                    // Move to the next state
                    parser.next();           

                    // Get the String for the value
                    String value = parser.getString();
                    System.out.println(value);
                }
            }
        }
    }
}
