json-lib-2.2.2-jdk15

16:38:11.470 INFO  jd.cli.Main - Decompiling json-lib-2.2.2-jdk15.jar
package net.sf.json;

import java.util.HashSet;

final class AbstractJSON$1
  extends ThreadLocal
{
  protected synchronized Object initialValue()
  {
    return new HashSet();
  }
}

/* Location:
 * Qualified Name:     net.sf.json.AbstractJSON.1
 * Java Class Version: 1.1 (45.3)
 * JD-Core Version:    0.7.1
 */
package net.sf.json;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import net.sf.json.util.JsonEventListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

abstract class AbstractJSON
{
  private static ThreadLocal cycleSet = new ThreadLocal()
  {
    protected synchronized Object initialValue()
    {
      return new HashSet();
    }
  };
  private static final Log log = LogFactory.getLog(AbstractJSON.class);
  
  protected static boolean addInstance(Object instance)
  {
    return getCycleSet().add(instance);
  }
  
  protected static void fireArrayEndEvent(JsonConfig jsonConfig)
  {
    if (jsonConfig.isEventTriggeringEnabled())
    {
      Iterator listeners = jsonConfig.getJsonEventListeners().iterator();
      while (listeners.hasNext())
      {
        JsonEventListener listener = (JsonEventListener)listeners.next();
        try
        {
          listener.onArrayEnd();
        }
        catch (RuntimeException e)
        {
          log.warn(e);
        }
      }
    }
  }
  
  protected static void fireArrayStartEvent(JsonConfig jsonConfig)
  {
    if (jsonConfig.isEventTriggeringEnabled())
    {
      Iterator listeners = jsonConfig.getJsonEventListeners().iterator();
      while (listeners.hasNext())
      {
        JsonEventListener listener = (JsonEventListener)listeners.next();
        try
        {
          listener.onArrayStart();
        }
        catch (RuntimeException e)
        {
          log.warn(e);
        }
      }
    }
  }
  
  protected static void fireElementAddedEvent(int index, Object element, JsonConfig jsonConfig)
  {
    if (jsonConfig.isEventTriggeringEnabled())
    {
      Iterator listeners = jsonConfig.getJsonEventListeners().iterator();
      while (listeners.hasNext())
      {
        JsonEventListener listener = (JsonEventListener)listeners.next();
        try
        {
          listener.onElementAdded(index, element);
        }
        catch (RuntimeException e)
        {
          log.warn(e);
        }
      }
    }
  }
  
  protected static void fireErrorEvent(JSONException jsone, JsonConfig jsonConfig)
  {
    if (jsonConfig.isEventTriggeringEnabled())
    {
      Iterator listeners = jsonConfig.getJsonEventListeners().iterator();
      while (listeners.hasNext())
      {
        JsonEventListener listener = (JsonEventListener)listeners.next();
        try
        {
          listener.onError(jsone);
        }
        catch (RuntimeException e)
        {
          log.warn(e);
        }
      }
    }
  }
  
  protected static void fireObjectEndEvent(JsonConfig jsonConfig)
  {
    if (jsonConfig.isEventTriggeringEnabled())
    {
      Iterator listeners = jsonConfig.getJsonEventListeners().iterator();
      while (listeners.hasNext())
      {
        JsonEventListener listener = (JsonEventListener)listeners.next();
        try
        {
          listener.onObjectEnd();
        }
        catch (RuntimeException e)
        {
          log.warn(e);
        }
      }
    }
  }
  
  protected static void fireObjectStartEvent(JsonConfig jsonConfig)
  {
    if (jsonConfig.isEventTriggeringEnabled())
    {
      Iterator listeners = jsonConfig.getJsonEventListeners().iterator();
      while (listeners.hasNext())
      {
        JsonEventListener listener = (JsonEventListener)listeners.next();
        try
        {
          listener.onObjectStart();
        }
        catch (RuntimeException e)
        {
          log.warn(e);
        }
      }
    }
  }
  
  protected static void firePropertySetEvent(String key, Object value, boolean accumulated, JsonConfig jsonConfig)
  {
    if (jsonConfig.isEventTriggeringEnabled())
    {
      Iterator listeners = jsonConfig.getJsonEventListeners().iterator();
      while (listeners.hasNext())
      {
        JsonEventListener listener = (JsonEventListener)listeners.next();
        try
        {
          listener.onPropertySet(key, value, accumulated);
        }
        catch (RuntimeException e)
        {
          log.warn(e);
        }
      }
    }
  }
  
  protected static void fireWarnEvent(String warning, JsonConfig jsonConfig)
  {
    if (jsonConfig.isEventTriggeringEnabled())
    {
      Iterator listeners = jsonConfig.getJsonEventListeners().iterator();
      while (listeners.hasNext())
      {
        JsonEventListener listener = (JsonEventListener)listeners.next();
        try
        {
          listener.onWarning(warning);
        }
        catch (RuntimeException e)
        {
          log.warn(e);
        }
      }
    }
  }
  
  protected static void removeInstance(Object instance)
  {
    getCycleSet().remove(instance);
  }
  
  private static Set getCycleSet()
  {
    return (Set)cycleSet.get();
  }
}

/* Location:
 * Qualified Name:     net.sf.json.AbstractJSON
 * Java Class Version: 1.1 (45.3)
 * JD-Core Version:    0.7.1
 */
package net.sf.json;

import java.io.Writer;

public abstract interface JSON
{
  public abstract boolean isArray();
  
  public abstract boolean isEmpty();
  
  public abstract int size();
  
  public abstract String toString(int paramInt);
  
  public abstract String toString(int paramInt1, int paramInt2);
  
  public abstract Writer write(Writer paramWriter);
}

/* Location:
 * Qualified Name:     net.sf.json.JSON
 * Java Class Version: 1.1 (45.3)
 * JD-Core Version:    0.7.1
 */
package net.sf.json;

import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import net.sf.ezmorph.Morpher;
import net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.object.IdentityObjectMorpher;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.processors.JsonVerifier;
import net.sf.json.util.CycleDetectionStrategy;
import net.sf.json.util.JSONTokener;
import net.sf.json.util.JSONUtils;
import net.sf.json.util.NewBeanInstanceStrategy;
import org.apache.commons.lang.StringUtils;

public final class JSONArray
  extends AbstractJSON
  implements JSON, List, Comparable
{
  private List elements;
  private boolean expandElements;
  
  public static JSONArray fromObject(Object object)
  {
    return fromObject(object, new JsonConfig());
  }
  
  public static JSONArray fromObject(Object object, JsonConfig jsonConfig)
  {
    if ((object instanceof JSONString)) {
      return _fromJSONString((JSONString)object, jsonConfig);
    }
    if ((object instanceof JSONArray)) {
      return _fromJSONArray((JSONArray)object, jsonConfig);
    }
    if ((object instanceof Collection)) {
      return _fromCollection((Collection)object, jsonConfig);
    }
    if ((object instanceof JSONTokener)) {
      return _fromJSONTokener((JSONTokener)object, jsonConfig);
    }
    if ((object instanceof String)) {
      return _fromString((String)object, jsonConfig);
    }
    if ((object != null) && (object.getClass().isArray()))
    {
      Class type = object.getClass().getComponentType();
      if (!type.isPrimitive()) {
        return _fromArray((Object[])object, jsonConfig);
      }
      if (type == Boolean.TYPE) {
        return _fromArray((boolean[])object, jsonConfig);
      }
      if (type == Byte.TYPE) {
        return _fromArray((byte[])object, jsonConfig);
      }
      if (type == Short.TYPE) {
        return _fromArray((short[])object, jsonConfig);
      }
      if (type == Integer.TYPE) {
        return _fromArray((int[])object, jsonConfig);
      }
      if (type == Long.TYPE) {
        return _fromArray((long[])object, jsonConfig);
      }
      if (type == Float.TYPE) {
        return _fromArray((float[])object, jsonConfig);
      }
      if (type == Double.TYPE) {
        return _fromArray((double[])object, jsonConfig);
      }
      if (type == Character.TYPE) {
        return _fromArray((char[])object, jsonConfig);
      }
      throw new JSONException("Unsupported type");
    }
    if ((JSONUtils.isBoolean(object)) || (JSONUtils.isFunction(object)) || (JSONUtils.isNumber(object)) || (JSONUtils.isNull(object)) || (JSONUtils.isString(object)) || ((object instanceof JSON)))
    {
      fireArrayStartEvent(jsonConfig);
      JSONArray jsonArray = new JSONArray().element(object, jsonConfig);
      fireElementAddedEvent(0, jsonArray.get(0), jsonConfig);
      fireArrayStartEvent(jsonConfig);
      return jsonArray;
    }
    if ((object instanceof Enum)) {
      return _fromArray((Enum)object, jsonConfig);
    }
    if (((object instanceof Annotation)) || ((object != null) && (object.getClass().isAnnotation()))) {
      throw new JSONException("Unsupported type");
    }
    if (JSONUtils.isObject(object))
    {
      fireArrayStartEvent(jsonConfig);
      JSONArray jsonArray = new JSONArray().element(JSONObject.fromObject(object, jsonConfig));
      fireElementAddedEvent(0, jsonArray.get(0), jsonConfig);
      fireArrayStartEvent(jsonConfig);
      return jsonArray;
    }
    throw new JSONException("Unsupported type");
  }
  
  public static Class[] getCollectionType(PropertyDescriptor pd, boolean useGetter)
    throws JSONException
  {
    Type type;
    Type type;
    if (useGetter)
    {
      Method m = pd.getReadMethod();
      type = m.getGenericReturnType();
    }
    else
    {
      Method m = pd.getWriteMethod();
      Type[] gpts = m.getGenericParameterTypes();
      if (1 != gpts.length) {
        throw new JSONException("method " + m + " is not a standard setter");
      }
      type = gpts[0];
    }
    if (!(type instanceof ParameterizedType)) {
      return null;
    }
    ParameterizedType pType = (ParameterizedType)type;
    Type[] actualTypes = pType.getActualTypeArguments();
    
    Class[] ret = new Class[actualTypes.length];
    for (int i = 0; i < ret.length; i++) {
      ret[i] = ((Class)actualTypes[i]);
    }
    return ret;
  }
  
  public static int[] getDimensions(JSONArray jsonArray)
  {
    if ((jsonArray == null) || (jsonArray.isEmpty())) {
      return new int[] { 0 };
    }
    List dims = new ArrayList();
    processArrayDimensions(jsonArray, dims, 0);
    int[] dimensions = new int[dims.size()];
    int j = 0;
    for (Iterator i = dims.iterator(); i.hasNext();) {
      dimensions[(j++)] = ((Integer)i.next()).intValue();
    }
    return dimensions;
  }
  
  public static Object toArray(JSONArray jsonArray)
  {
    return toArray(jsonArray, new JsonConfig());
  }
  
  public static Object toArray(JSONArray jsonArray, Class objectClass)
  {
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setRootClass(objectClass);
    return toArray(jsonArray, jsonConfig);
  }
  
  public static Object toArray(JSONArray jsonArray, Class objectClass, Map classMap)
  {
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setRootClass(objectClass);
    jsonConfig.setClassMap(classMap);
    return toArray(jsonArray, jsonConfig);
  }
  
  public static Object toArray(JSONArray jsonArray, JsonConfig jsonConfig)
  {
    Class objectClass = jsonConfig.getRootClass();
    Map classMap = jsonConfig.getClassMap();
    if (jsonArray.size() == 0) {
      return Array.newInstance(objectClass == null ? Object.class : objectClass, 0);
    }
    int[] dimensions = getDimensions(jsonArray);
    Object array = Array.newInstance(objectClass == null ? Object.class : objectClass, dimensions);
    
    int size = jsonArray.size();
    for (int i = 0; i < size; i++)
    {
      Object value = jsonArray.get(i);
      if (JSONUtils.isNull(value))
      {
        Array.set(array, i, null);
      }
      else
      {
        Class type = value.getClass();
        if (JSONArray.class.isAssignableFrom(type))
        {
          Array.set(array, i, toArray((JSONArray)value, objectClass, classMap));
        }
        else if ((String.class.isAssignableFrom(type)) || (Boolean.class.isAssignableFrom(type)) || (Character.class.isAssignableFrom(type)) || (JSONFunction.class.isAssignableFrom(type)))
        {
          if ((objectClass != null) && (!objectClass.isAssignableFrom(type))) {
            value = JSONUtils.getMorpherRegistry().morph(objectClass, value);
          }
          Array.set(array, i, value);
        }
        else if (JSONUtils.isNumber(type))
        {
          if ((objectClass != null) && ((Byte.class.isAssignableFrom(objectClass)) || (Byte.TYPE.isAssignableFrom(objectClass)))) {
            Array.set(array, i, Byte.valueOf(String.valueOf(value)));
          } else if ((objectClass != null) && ((Short.class.isAssignableFrom(objectClass)) || (Short.TYPE.isAssignableFrom(objectClass)))) {
            Array.set(array, i, Short.valueOf(String.valueOf(value)));
          } else {
            Array.set(array, i, value);
          }
        }
        else if (objectClass != null)
        {
          JsonConfig jsc = jsonConfig.copy();
          jsc.setRootClass(objectClass);
          jsc.setClassMap(classMap);
          Array.set(array, i, JSONObject.toBean((JSONObject)value, jsc));
        }
        else
        {
          Array.set(array, i, JSONObject.toBean((JSONObject)value));
        }
      }
    }
    return array;
  }
  
  public static Object toArray(JSONArray jsonArray, Object root, JsonConfig jsonConfig)
  {
    Class objectClass = root.getClass();
    if (jsonArray.size() == 0) {
      return Array.newInstance(objectClass, 0);
    }
    int[] dimensions = getDimensions(jsonArray);
    Object array = Array.newInstance(objectClass == null ? Object.class : objectClass, dimensions);
    
    int size = jsonArray.size();
    for (int i = 0; i < size; i++)
    {
      Object value = jsonArray.get(i);
      if (JSONUtils.isNull(value))
      {
        Array.set(array, i, null);
      }
      else
      {
        Class type = value.getClass();
        if (JSONArray.class.isAssignableFrom(type))
        {
          Array.set(array, i, toArray((JSONArray)value, root, jsonConfig));
        }
        else if ((String.class.isAssignableFrom(type)) || (Boolean.class.isAssignableFrom(type)) || (JSONUtils.isNumber(type)) || (Character.class.isAssignableFrom(type)) || (JSONFunction.class.isAssignableFrom(type)))
        {
          if ((objectClass != null) && (!objectClass.isAssignableFrom(type))) {
            value = JSONUtils.getMorpherRegistry().morph(objectClass, value);
          }
          Array.set(array, i, value);
        }
        else
        {
          try
          {
            Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(root.getClass(), null);
            
            Array.set(array, i, JSONObject.toBean((JSONObject)value, newRoot, jsonConfig));
          }
          catch (JSONException jsone)
          {
            throw jsone;
          }
          catch (Exception e)
          {
            throw new JSONException(e);
          }
        }
      }
    }
    return array;
  }
  
  public static Collection toCollection(JSONArray jsonArray)
  {
    return toCollection(jsonArray, new JsonConfig());
  }
  
  public static Collection toCollection(JSONArray jsonArray, Class objectClass)
  {
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setRootClass(objectClass);
    return toCollection(jsonArray, jsonConfig);
  }
  
  public static Collection toCollection(JSONArray jsonArray, JsonConfig jsonConfig)
  {
    Collection collection = null;
    Class collectionType = jsonConfig.getCollectionType();
    if (collectionType.isInterface())
    {
      if (collectionType.equals(List.class)) {
        collection = new ArrayList();
      } else if (collectionType.equals(Set.class)) {
        collection = new HashSet();
      } else {
        throw new JSONException("unknown interface: " + collectionType);
      }
    }
    else {
      try
      {
        collection = (Collection)collectionType.newInstance();
      }
      catch (InstantiationException e)
      {
        throw new JSONException(e);
      }
      catch (IllegalAccessException e)
      {
        throw new JSONException(e);
      }
    }
    Class objectClass = jsonConfig.getRootClass();
    Map classMap = jsonConfig.getClassMap();
    
    int size = jsonArray.size();
    for (int i = 0; i < size; i++)
    {
      Object value = jsonArray.get(i);
      if (JSONUtils.isNull(value))
      {
        collection.add(null);
      }
      else
      {
        Class type = value.getClass();
        if (JSONArray.class.isAssignableFrom(value.getClass()))
        {
          collection.add(toCollection((JSONArray)value, jsonConfig));
        }
        else if ((String.class.isAssignableFrom(type)) || (Boolean.class.isAssignableFrom(type)) || (JSONUtils.isNumber(type)) || (Character.class.isAssignableFrom(type)) || (JSONFunction.class.isAssignableFrom(type)))
        {
          if (!value.getClass().isAssignableFrom(type)) {
            throw new JSONException("can't assign value " + value + " of type " + value.getClass() + " to Collection of type " + type);
          }
          collection.add(value);
        }
        else if (objectClass != null)
        {
          JsonConfig jsc = jsonConfig.copy();
          jsc.setRootClass(objectClass);
          jsc.setClassMap(classMap);
          collection.add(JSONObject.toBean((JSONObject)value, jsc));
        }
        else
        {
          collection.add(JSONObject.toBean((JSONObject)value));
        }
      }
    }
    return collection;
  }
  
  /**
   * @deprecated
   */
  public static List toList(JSONArray jsonArray)
  {
    return toList(jsonArray, new JsonConfig());
  }
  
  /**
   * @deprecated
   */
  public static List toList(JSONArray jsonArray, Class objectClass)
  {
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setRootClass(objectClass);
    return toList(jsonArray, jsonConfig);
  }
  
  /**
   * @deprecated
   */
  public static List toList(JSONArray jsonArray, Class objectClass, Map classMap)
  {
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setRootClass(objectClass);
    jsonConfig.setClassMap(classMap);
    return toList(jsonArray, jsonConfig);
  }
  
  /**
   * @deprecated
   */
  public static List toList(JSONArray jsonArray, JsonConfig jsonConfig)
  {
    if (jsonArray.size() == 0) {
      return new ArrayList();
    }
    Class objectClass = jsonConfig.getRootClass();
    Map classMap = jsonConfig.getClassMap();
    
    List list = new ArrayList();
    int size = jsonArray.size();
    for (int i = 0; i < size; i++)
    {
      Object value = jsonArray.get(i);
      if (JSONUtils.isNull(value))
      {
        list.add(null);
      }
      else
      {
        Class type = value.getClass();
        if (JSONArray.class.isAssignableFrom(type))
        {
          list.add(toList((JSONArray)value, objectClass, classMap));
        }
        else if ((String.class.isAssignableFrom(type)) || (Boolean.class.isAssignableFrom(type)) || (JSONUtils.isNumber(type)) || (Character.class.isAssignableFrom(type)) || (JSONFunction.class.isAssignableFrom(type)))
        {
          if ((objectClass != null) && (!objectClass.isAssignableFrom(type))) {
            value = JSONUtils.getMorpherRegistry().morph(objectClass, value);
          }
          list.add(value);
        }
        else if (objectClass != null)
        {
          JsonConfig jsc = jsonConfig.copy();
          jsc.setRootClass(objectClass);
          jsc.setClassMap(classMap);
          list.add(JSONObject.toBean((JSONObject)value, jsc));
        }
        else
        {
          list.add(JSONObject.toBean((JSONObject)value));
        }
      }
    }
    return list;
  }
  
  public static List toList(JSONArray jsonArray, Object root, JsonConfig jsonConfig)
  {
    if ((jsonArray.size() == 0) || (root == null)) {
      return new ArrayList();
    }
    List list = new ArrayList();
    int size = jsonArray.size();
    for (int i = 0; i < size; i++)
    {
      Object value = jsonArray.get(i);
      if (JSONUtils.isNull(value))
      {
        list.add(null);
      }
      else
      {
        Class type = value.getClass();
        if (JSONArray.class.isAssignableFrom(type)) {
          list.add(toList((JSONArray)value, root, jsonConfig));
        } else if ((String.class.isAssignableFrom(type)) || (Boolean.class.isAssignableFrom(type)) || (JSONUtils.isNumber(type)) || (Character.class.isAssignableFrom(type)) || (JSONFunction.class.isAssignableFrom(type))) {
          list.add(value);
        } else {
          try
          {
            Object newRoot = jsonConfig.getNewBeanInstanceStrategy().newInstance(root.getClass(), null);
            
            list.add(JSONObject.toBean((JSONObject)value, newRoot, jsonConfig));
          }
          catch (JSONException jsone)
          {
            throw jsone;
          }
          catch (Exception e)
          {
            throw new JSONException(e);
          }
        }
      }
    }
    return list;
  }
  
  private static JSONArray _fromArray(boolean[] array, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    if (!addInstance(array)) {
      try
      {
        return jsonConfig.getCycleDetectionStrategy().handleRepeatedReferenceAsArray(array);
      }
      catch (JSONException jsone)
      {
        removeInstance(array);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
      catch (RuntimeException e)
      {
        removeInstance(array);
        JSONException jsone = new JSONException(e);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
    }
    JSONArray jsonArray = new JSONArray();
    for (int i = 0; i < array.length; i++)
    {
      Boolean b = array[i] != 0 ? Boolean.TRUE : Boolean.FALSE;
      elements.add(b);
      fireElementAddedEvent(i, b, jsonConfig);
    }
    removeInstance(array);
    fireArrayEndEvent(jsonConfig);
    return jsonArray;
  }
  
  private static JSONArray _fromArray(byte[] array, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    if (!addInstance(array)) {
      try
      {
        return jsonConfig.getCycleDetectionStrategy().handleRepeatedReferenceAsArray(array);
      }
      catch (JSONException jsone)
      {
        removeInstance(array);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
      catch (RuntimeException e)
      {
        removeInstance(array);
        JSONException jsone = new JSONException(e);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
    }
    JSONArray jsonArray = new JSONArray();
    for (int i = 0; i < array.length; i++)
    {
      Number n = JSONUtils.transformNumber(new Byte(array[i]));
      elements.add(n);
      fireElementAddedEvent(i, n, jsonConfig);
    }
    removeInstance(array);
    fireArrayEndEvent(jsonConfig);
    return jsonArray;
  }
  
  private static JSONArray _fromArray(char[] array, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    if (!addInstance(array)) {
      try
      {
        return jsonConfig.getCycleDetectionStrategy().handleRepeatedReferenceAsArray(array);
      }
      catch (JSONException jsone)
      {
        removeInstance(array);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
      catch (RuntimeException e)
      {
        removeInstance(array);
        JSONException jsone = new JSONException(e);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
    }
    JSONArray jsonArray = new JSONArray();
    for (int i = 0; i < array.length; i++)
    {
      Character c = new Character(array[i]);
      elements.add(c);
      fireElementAddedEvent(i, c, jsonConfig);
    }
    removeInstance(array);
    fireArrayEndEvent(jsonConfig);
    return jsonArray;
  }
  
  private static JSONArray _fromArray(double[] array, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    if (!addInstance(array)) {
      try
      {
        return jsonConfig.getCycleDetectionStrategy().handleRepeatedReferenceAsArray(array);
      }
      catch (JSONException jsone)
      {
        removeInstance(array);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
      catch (RuntimeException e)
      {
        removeInstance(array);
        JSONException jsone = new JSONException(e);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
    }
    JSONArray jsonArray = new JSONArray();
    try
    {
      for (int i = 0; i < array.length; i++)
      {
        Double d = new Double(array[i]);
        JSONUtils.testValidity(d);
        elements.add(d);
        fireElementAddedEvent(i, d, jsonConfig);
      }
    }
    catch (JSONException jsone)
    {
      removeInstance(array);
      fireErrorEvent(jsone, jsonConfig);
      throw jsone;
    }
    removeInstance(array);
    fireArrayEndEvent(jsonConfig);
    return jsonArray;
  }
  
  private static JSONArray _fromArray(Enum e, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    if (!addInstance(e)) {
      try
      {
        return jsonConfig.getCycleDetectionStrategy().handleRepeatedReferenceAsArray(e);
      }
      catch (JSONException jsone)
      {
        removeInstance(e);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
      catch (RuntimeException re)
      {
        removeInstance(e);
        JSONException jsone = new JSONException(re);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
    }
    JSONArray jsonArray = new JSONArray();
    if (e != null)
    {
      elements.add(e.toString());
      fireElementAddedEvent(0, jsonArray.get(0), jsonConfig);
    }
    else
    {
      JSONException jsone = new JSONException("enum value is null");
      removeInstance(e);
      fireErrorEvent(jsone, jsonConfig);
      throw jsone;
    }
    removeInstance(e);
    fireArrayEndEvent(jsonConfig);
    return jsonArray;
  }
  
  private static JSONArray _fromArray(float[] array, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    if (!addInstance(array)) {
      try
      {
        return jsonConfig.getCycleDetectionStrategy().handleRepeatedReferenceAsArray(array);
      }
      catch (JSONException jsone)
      {
        removeInstance(array);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
      catch (RuntimeException e)
      {
        removeInstance(array);
        JSONException jsone = new JSONException(e);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
    }
    JSONArray jsonArray = new JSONArray();
    try
    {
      for (int i = 0; i < array.length; i++)
      {
        Float f = new Float(array[i]);
        JSONUtils.testValidity(f);
        elements.add(f);
        fireElementAddedEvent(i, f, jsonConfig);
      }
    }
    catch (JSONException jsone)
    {
      removeInstance(array);
      fireErrorEvent(jsone, jsonConfig);
      throw jsone;
    }
    removeInstance(array);
    fireArrayEndEvent(jsonConfig);
    return jsonArray;
  }
  
  private static JSONArray _fromArray(int[] array, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    if (!addInstance(array)) {
      try
      {
        return jsonConfig.getCycleDetectionStrategy().handleRepeatedReferenceAsArray(array);
      }
      catch (JSONException jsone)
      {
        removeInstance(array);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
      catch (RuntimeException e)
      {
        removeInstance(array);
        JSONException jsone = new JSONException(e);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
    }
    JSONArray jsonArray = new JSONArray();
    for (int i = 0; i < array.length; i++)
    {
      Number n = new Integer(array[i]);
      elements.add(n);
      fireElementAddedEvent(i, n, jsonConfig);
    }
    removeInstance(array);
    fireArrayEndEvent(jsonConfig);
    return jsonArray;
  }
  
  private static JSONArray _fromArray(long[] array, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    if (!addInstance(array)) {
      try
      {
        return jsonConfig.getCycleDetectionStrategy().handleRepeatedReferenceAsArray(array);
      }
      catch (JSONException jsone)
      {
        removeInstance(array);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
      catch (RuntimeException e)
      {
        removeInstance(array);
        JSONException jsone = new JSONException(e);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
    }
    JSONArray jsonArray = new JSONArray();
    for (int i = 0; i < array.length; i++)
    {
      Number n = JSONUtils.transformNumber(new Long(array[i]));
      elements.add(n);
      fireElementAddedEvent(i, n, jsonConfig);
    }
    removeInstance(array);
    fireArrayEndEvent(jsonConfig);
    return jsonArray;
  }
  
  private static JSONArray _fromArray(Object[] array, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    if (!addInstance(array)) {
      try
      {
        return jsonConfig.getCycleDetectionStrategy().handleRepeatedReferenceAsArray(array);
      }
      catch (JSONException jsone)
      {
        removeInstance(array);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
      catch (RuntimeException e)
      {
        removeInstance(array);
        JSONException jsone = new JSONException(e);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
    }
    JSONArray jsonArray = new JSONArray();
    try
    {
      for (int i = 0; i < array.length; i++)
      {
        Object element = array[i];
        jsonArray.addValue(element, jsonConfig);
        fireElementAddedEvent(i, jsonArray.get(i), jsonConfig);
      }
    }
    catch (JSONException jsone)
    {
      removeInstance(array);
      fireErrorEvent(jsone, jsonConfig);
      throw jsone;
    }
    catch (RuntimeException e)
    {
      removeInstance(array);
      JSONException jsone = new JSONException(e);
      fireErrorEvent(jsone, jsonConfig);
      throw jsone;
    }
    removeInstance(array);
    fireArrayEndEvent(jsonConfig);
    return jsonArray;
  }
  
  private static JSONArray _fromArray(short[] array, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    if (!addInstance(array)) {
      try
      {
        return jsonConfig.getCycleDetectionStrategy().handleRepeatedReferenceAsArray(array);
      }
      catch (JSONException jsone)
      {
        removeInstance(array);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
      catch (RuntimeException e)
      {
        removeInstance(array);
        JSONException jsone = new JSONException(e);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
    }
    JSONArray jsonArray = new JSONArray();
    for (int i = 0; i < array.length; i++)
    {
      Number n = JSONUtils.transformNumber(new Short(array[i]));
      elements.add(n);
      fireElementAddedEvent(i, n, jsonConfig);
    }
    removeInstance(array);
    fireArrayEndEvent(jsonConfig);
    return jsonArray;
  }
  
  private static JSONArray _fromCollection(Collection collection, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    if (!addInstance(collection)) {
      try
      {
        return jsonConfig.getCycleDetectionStrategy().handleRepeatedReferenceAsArray(collection);
      }
      catch (JSONException jsone)
      {
        removeInstance(collection);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
      catch (RuntimeException e)
      {
        removeInstance(collection);
        JSONException jsone = new JSONException(e);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
    }
    JSONArray jsonArray = new JSONArray();
    try
    {
      i = 0;
      for (elements = collection.iterator(); elements.hasNext();)
      {
        Object element = elements.next();
        jsonArray.addValue(element, jsonConfig);
        fireElementAddedEvent(i, jsonArray.get(i++), jsonConfig);
      }
    }
    catch (JSONException jsone)
    {
      int i;
      Iterator elements;
      removeInstance(collection);
      fireErrorEvent(jsone, jsonConfig);
      throw jsone;
    }
    catch (RuntimeException e)
    {
      removeInstance(collection);
      JSONException jsone = new JSONException(e);
      fireErrorEvent(jsone, jsonConfig);
      throw jsone;
    }
    removeInstance(collection);
    fireArrayEndEvent(jsonConfig);
    return jsonArray;
  }
  
  private static JSONArray _fromJSONArray(JSONArray array, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    if (!addInstance(array)) {
      try
      {
        return jsonConfig.getCycleDetectionStrategy().handleRepeatedReferenceAsArray(array);
      }
      catch (JSONException jsone)
      {
        removeInstance(array);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
      catch (RuntimeException e)
      {
        removeInstance(array);
        JSONException jsone = new JSONException(e);
        fireErrorEvent(jsone, jsonConfig);
        throw jsone;
      }
    }
    JSONArray jsonArray = new JSONArray();
    int index = 0;
    for (Iterator elements = array.iterator(); elements.hasNext();)
    {
      Object element = elements.next();
      elements.add(element);
      fireElementAddedEvent(index++, element, jsonConfig);
    }
    removeInstance(array);
    fireArrayEndEvent(jsonConfig);
    return jsonArray;
  }
  
  private static JSONArray _fromJSONString(JSONString string, JsonConfig jsonConfig)
  {
    return _fromJSONTokener(new JSONTokener(string.toJSONString()), jsonConfig);
  }
  
  private static JSONArray _fromJSONTokener(JSONTokener tokener, JsonConfig jsonConfig)
  {
    fireArrayStartEvent(jsonConfig);
    
    JSONArray jsonArray = new JSONArray();
    int index = 0;
    try
    {
      if (tokener.nextClean() != '[') {
        throw tokener.syntaxError("A JSONArray text must start with '['");
      }
      if (tokener.nextClean() == ']')
      {
        fireArrayEndEvent(jsonConfig);
        return jsonArray;
      }
      tokener.back();
      for (;;)
      {
        if (tokener.nextClean() == ',')
        {
          tokener.back();
          elements.add(JSONNull.getInstance());
          fireElementAddedEvent(index, jsonArray.get(index++), jsonConfig);
        }
        else
        {
          tokener.back();
          Object v = tokener.nextValue(jsonConfig);
          if (!JSONUtils.isFunctionHeader(v))
          {
            if (((v instanceof String)) && (JSONUtils.mayBeJSON((String)v))) {
              jsonArray.addValue("\"" + v + "\"", jsonConfig);
            } else {
              jsonArray.addValue(v, jsonConfig);
            }
            fireElementAddedEvent(index, jsonArray.get(index++), jsonConfig);
          }
          else
          {
            String params = JSONUtils.getFunctionParams((String)v);
            
            int i = 0;
            StringBuffer sb = new StringBuffer();
            for (;;)
            {
              char ch = tokener.next();
              if (ch == 0) {
                break;
              }
              if (ch == '{') {
                i++;
              }
              if (ch == '}') {
                i--;
              }
              sb.append(ch);
              if (i == 0) {
                break;
              }
            }
            if (i != 0) {
              throw tokener.syntaxError("Unbalanced '{' or '}' on prop: " + v);
            }
            String text = sb.toString();
            text = text.substring(1, text.length() - 1).trim();
            
            jsonArray.addValue(new JSONFunction(params != null ? StringUtils.split(params, ",") : null, text), jsonConfig);
            
            fireElementAddedEvent(index, jsonArray.get(index++), jsonConfig);
          }
        }
        switch (tokener.nextClean())
        {
        case ',': 
        case ';': 
          if (tokener.nextClean() == ']')
          {
            fireArrayEndEvent(jsonConfig);
            return jsonArray;
          }
          tokener.back();
        }
      }
      fireArrayEndEvent(jsonConfig);
      return jsonArray;
      
      throw tokener.syntaxError("Expected a ',' or ']'");
    }
    catch (JSONException jsone)
    {
      fireErrorEvent(jsone, jsonConfig);
      throw jsone;
    }
  }
  
  private static JSONArray _fromString(String string, JsonConfig jsonConfig)
  {
    return _fromJSONTokener(new JSONTokener(string), jsonConfig);
  }
  
  private static void processArrayDimensions(JSONArray jsonArray, List dims, int index)
  {
    if (dims.size() <= index)
    {
      dims.add(new Integer(jsonArray.size()));
    }
    else
    {
      int i = ((Integer)dims.get(index)).intValue();
      if (jsonArray.size() > i) {
        dims.set(index, new Integer(jsonArray.size()));
      }
    }
    for (Iterator i = jsonArray.iterator(); i.hasNext();)
    {
      Object item = i.next();
      if ((item instanceof JSONArray)) {
        processArrayDimensions((JSONArray)item, dims, index + 1);
      }
    }
  }
  
  public JSONArray()
  {
    elements = new ArrayList();
  }
  
  public void add(int index, Object value)
  {
    add(index, value, new JsonConfig());
  }
  
  public void add(int index, Object value, JsonConfig jsonConfig)
  {
    elements.add(index, processValue(value, jsonConfig));
  }
  
  public boolean add(Object value)
  {
    return add(value, new JsonConfig());
  }
  
  public boolean add(Object value, JsonConfig jsonConfig)
  {
    element(value, jsonConfig);
    return true;
  }
  
  public boolean addAll(Collection collection)
  {
    return addAll(collection, new JsonConfig());
  }
  
  public boolean addAll(Collection collection, JsonConfig jsonConfig)
  {
    if ((collection == null) || (collection.size() == 0)) {
      return false;
    }
    for (Iterator i = collection.iterator(); i.hasNext();) {
      element(i.next(), jsonConfig);
    }
    return true;
  }
  
  public boolean addAll(int index, Collection collection)
  {
    return addAll(index, collection, new JsonConfig());
  }
  
  public boolean addAll(int index, Collection collection, JsonConfig jsonConfig)
  {
    if ((collection == null) || (collection.size() == 0)) {
      return false;
    }
    int offset = 0;
    for (Iterator i = collection.iterator(); i.hasNext();) {
      elements.add(index + offset++, processValue(i.next(), jsonConfig));
    }
    return true;
  }
  
  public void clear()
  {
    elements.clear();
  }
  
  public int compareTo(Object obj)
  {
    if ((obj != null) && ((obj instanceof JSONArray)))
    {
      JSONArray other = (JSONArray)obj;
      int size1 = size();
      int size2 = other.size();
      if (size1 < size2) {
        return -1;
      }
      if (size1 > size2) {
        return 1;
      }
      if (equals(other)) {
        return 0;
      }
    }
    return -1;
  }
  
  public boolean contains(Object o)
  {
    return contains(o, new JsonConfig());
  }
  
  public boolean contains(Object o, JsonConfig jsonConfig)
  {
    return elements.contains(processValue(o, jsonConfig));
  }
  
  public boolean containsAll(Collection collection)
  {
    return containsAll(collection, new JsonConfig());
  }
  
  public boolean containsAll(Collection collection, JsonConfig jsonConfig)
  {
    return elements.containsAll(fromObject(collection, jsonConfig));
1 2 3 4 5 6 7 8 9 10

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd