package edu.grinnell.tranchri.cohnhan.deweytyl.hardtmad;

/**
 * JSONParser Class
 * 
 * @author Hannah Cohn
 * @author Ty Dewey
 * @author Madeleine Hardt
 * @author Christine Tran
 */

public class JSONParser
{
  // +--------+----------------------------------------------
  // | Fields |
  // +--------+

  String jString;
  JSONValue jValue;

  // +--------------+----------------------------------------
  // | Constructors |
  // +--------------+

  public JSONParser(String input)
  {
    jString = input;
  } // JSONParser (String)

  public JSONParser(JSONValue obj)
  {
    jValue = obj;
  } // JSONParser (JSONValue)

  // +---------------+---------------------------------------
  // | Class Methods |
  // +---------------+

  public Object JSONToJavaObject()
    throws Exception
  {
    this.jValue = JSONUtils.parseValue(jString);
    char type = jValue.type();
    switch (type)
      {
        case 'n':
          return JSONUtils.getNumber(jValue);
        case 's':
          return JSONUtils.getString(jValue);
        case 'c':
          {
            String str = JSONUtils.getString(jValue);
            // Handle symbolic constants
            if (str.equals("true"))
              return true;
            else if (str.equals("false"))
              return false;
            else
              return null;
          } // case 'c'
        case 'a':
          return JSONUtils.getArray(jValue);
        case 'o':
          return JSONUtils.getObject(jValue);
      } // switch

    throw new Exception("Unsupported String" + jString);

  } // JSONToJavaObject()

  public String JavaObjectToStringOfJSON()
    throws Exception
  {
    
    return jString;
  } // JavaObjectToStringOfJSON()

}

@@ -51,16 +51,132 @@
 dtmad;%0A%0A
+import java.lang.reflect.Array;%0Aimport java.lang.reflect.Field;%0Aimport java.math.BigDecimal;%0Aimport java.util.Map;%0A%0A
 /**%0A * J
@@ -454,16 +454,37 @@
  jValue;
+%0A  Object javaObject;
 %0A%0A  // +
@@ -691,25 +691,22 @@
 NParser(
-JSONValue
+Object
  obj)%0A  
@@ -712,21 +712,25 @@
  %7B%0A    j
-Value
+avaObject
  = obj;%0A
@@ -1720,16 +1720,49 @@
 ject()%0A%0A
+  @SuppressWarnings(%22unchecked%22)%0A
   public
@@ -1825,16 +1825,1852 @@
   %7B%0A    
+// If the Object is a number%0A    if (javaObject instanceof BigDecimal)%0A      %7B%0A        jString = javaObject.toString();%0A      %7D // if%0A%0A    // If the Object is a String%0A    else if (javaObject instanceof java.lang.String)%0A      %7B%0A        jString = %22%5C%22%22 + javaObject.toString() + %22%5C%22%22;%0A      %7D // else if%0A%0A    // If the Object is a symbolic constant%0A    else if (javaObject instanceof Boolean %7C%7C javaObject == null)%0A      %7B%0A        jString = javaObject.toString();%0A      %7D // else if%0A%0A    // If the Object is a Pair (key-value pair)%0A    else if (javaObject instanceof Map.Entry)%0A      %7B%0A        JSONParser parser = new JSONParser (((Map.Entry%3CString, Object%3E) javaObject).getValue());%0A        jString =%0A            %22%5C%22%22 + ((Map.Entry%3CString, Object%3E) javaObject).getKey().toString() + %22%5C%22 : %22%0A                + parser.JavaObjectToStringOfJSON();%0A      %7D // else if%0A%0A    // If the Object is an Array%0A    else if (javaObject instanceof Array)%0A      %7B%0A        jString = %22%22;%0A        Object instance;%0A        int i = 0;%0A        while ((instance = Array.get(javaObject, i)) != null)%0A          %7B%0A            JSONParser parser = new JSONParser (instance);%0A            jString +=  parser.JavaObjectToStringOfJSON();%0A            i++;%0A          %7D // while%0A      %7D // else if%0A%0A    // If the Object is any other type%0A    else%0A      %7B%0A        jString = %22%7B%22;%0A        int i = 0;%0A        Field%5B%5D fields = javaObject.getClass().getFields();%0A        Field field;%0A        while ((field = fields%5Bi%5D) != null)%0A          %7B%0A            JSONParser parser = new JSONParser (field.get(javaObject));%0A            if (i != 0)%0A              %7B%0A                jString += %22, %22;%0A              %7D // if%0A            jString += %22%5C%22%22 + field.getName() + %22%5C%22 : %22 + parser.JavaObjectToStringOfJSON();%0A            i++;%0A          %7D // while%0A        jString += %22%7D%22;%0A      %7D // else%0A
 %0A    ret
@@ -3718,9 +3718,29 @@
 SON()%0A%0A%7D
+ // class JSONParser
 %0A

Add method to convert java object to json string