package edu.grinnell.callaway;

import java.util.HashMap;
import java.util.Vector;

public class JSON
{
  // parse determines the class of the object returned from parsing a JSON
  // string. After determining if the object is a Hashmap representation of an
  // object or a Vector representation of an array, parse recursively calls
  // parseElements to set the contents of the object or array.
  public Object parse(String str)
    throws Exception
  {
    int length = str.length();
    char start = str.charAt(0);
    char end = str.charAt(length);
    if (start == '{' && end == '}')
      {
        return parseObject(str, 1, length);
      }
    else if (start == '[' && end == ']')
      {
        return parseArray(str, 1, length);
      }
    else if (start == '{' || start == '[' || end == '}' || end == ']')
      {
        throw new Exception(
                            "Invalid syntax: Start and end braces do not match "
                                + start + " ... " + end);
      }
    else
      {
        throw new Exception("Invalid syntax: No start and end braces");
      }
  }

  public static HashMap<String, Object> parseObject(String str, int start,
                                                    int end)
    throws Exception

  {
    // make a new hashmap to store key value pairs
    // go through each char in the object's substring
    // if we have not found a key
    // // if the first char is an open quote
    // // // find the close quote
    // // else throw exception, objects should start with key
    // if we have found a key
    // // get the value
    HashMap<String, Object> newObject = new HashMap<String, Object>();
    boolean key_found = false;
    for (int i = start; i < end; i++)
      {
        char c = str.charAt(i);
        if (!key_found)
          {
            switch (c)
              {
                // these are formatting and can be ignored
                case ' ':
                case '\n':
                case '\t':
                case '\r':
                case '\b':
                case '\f':
                  break;
                // start of a key string
                case '"':

                  break;
                // otherwise error
                default:
                  throw new Exception("Invalid syntax: object must contain only key value piars");
              }
          }
        else // key found, find value
          {
            
          }

      }
    return newObject;
  }

  public static Vector<Object> parseArray(String str, int start, int end)
  {
    return vec;
  }
}

@@ -1865,18 +1865,16 @@
         
-  
 // these
@@ -2161,16 +2161,690 @@
 se '%22':%0A
+                  // find matching close quote%0A                  // scan through each char until quote is found%0A                  int valueStart = i;%0A                  while ((c = str.charAt(++i)) != '%22')%0A                    %7B%0A                      // skip over '%5C%22' chars%0A                      if (c == '%5C%5C')%0A                        %7B%0A                          if (str.charAt(i + 1) == '%22')%0A                            %7B%0A                              ++i;%0A                            %7D%0A                        %7D%0A                    %7D%0A                  int valueEnd = i;%0A                  String key = str.substring(valueStart, valueEnd);%0A                  key_found = true;
 %0A       
@@ -2951,32 +2951,71 @@
 w new Exception(
+%0A                                      
 %22Invalid syntax:
@@ -3095,21 +3095,16 @@
         
-else 
 // key f
@@ -3122,24 +3122,35 @@
 value%0A  
+      else%0A
         
 %7B%0A      
@@ -3133,32 +3133,34 @@
    else%0A        
+  
 %7B%0A            %0A 
@@ -3157,16 +3157,536 @@
         
+switch (c)%0A              %7B%0A              // these are formatting and can be ignored%0A                case ' ':%0A                case '%5Cn':%0A                case '%5Ct':%0A                case '%5Cr':%0A                case '%5Cb':%0A                case '%5Cf':%0A                  break;%0A                // start of a value%0A                case ':':%0A                  %0A                default:%0A                  throw new Exception(%0A                                      %22Invalid syntax: key must be followed by a value%22);%0A              %7D
 %0A       

write value parser for parseObject