Code example for JSONArray

Methods: getJSONObject, getString

0
  } 
 
  private String getOperationAction( JSONArray operation ) {
    String result;
    try { 
      result = operation.getString( 0 );
    } catch( JSONException e ) {
      throw new IllegalArgumentException( "Could not find action for operation " + operation );
    } 
    return result;
  } 
 
  @SuppressWarnings( "unchecked" ) 
  private <T> T[] getOperations( Class<T> opClass, String target, String property ) {
    List<T> result = new ArrayList<T>();
    List<Operation> operations = operationsMap.get( target );
    if( operations != null ) {
      for( Operation operation : operations ) {
        if(    opClass.isInstance( operation )
            && ( property == null || operation.getPropertyNames().contains( property ) ) ) {
          result.add( ( T )operation );
        } 
      } 
    } 
    return result.toArray( ( T[] )Array.newInstance( opClass, 0 ) );
  } 
 
  public abstract class Operation { 
 
    private final String target;
 
    private Operation( JSONArray operation ) {
      try { 
        target = operation.getString( 1 );
      } catch( JSONException e ) {
        throw new IllegalArgumentException( "Invalid operation target", e );
      } 
    } 
 
    public String getTarget() {
      return target;
    } 
 
    @SuppressWarnings( "unchecked" ) 
    public List<String> getPropertyNames() {
      JSONObject properties = getProperties();
      String[] names = JSONObject.getNames( properties );
      return names == null ? Collections.EMPTY_LIST : Arrays.asList( names );
    } 
 
    public Object getProperty( String key ) {
      Object result = null;
      JSONObject properties = getProperties();
      try { 
        Object value = properties.get( key );
        if( value instanceof JSONObject ) {
          result = JsonUtil.jsonToJava( ( JSONObject )value );
        } else if( value instanceof JSONArray ) {
          result = JsonUtil.jsonToJava( ( JSONArray )value );
        } else { 
          result = value;
        } 
      } catch( JSONException exception ) {
        // do nothing 
      } 
      return result;
    } 
 
    abstract protected JSONObject getProperties();
 
  } 
 
  public final class SetOperation extends Operation { 
 
    private final JSONObject properties;
 
    private SetOperation( JSONArray operation ) {
      super( operation );
      try { 
        properties = operation.getJSONObject( 2 );
      } catch( JSONException e ) {
        throw new IllegalArgumentException( "Properties object missing in operation", e );
      } 
    } 
 
    @Override 
    protected JSONObject getProperties() {
      return properties;
    } 
 
  } 
 
  public final class NotifyOperation extends Operation { 
 
    private final String eventName;
    private final JSONObject properties;
 
    private NotifyOperation( JSONArray operation ) {
      super( operation );
      try { 
        eventName = operation.getString( 2 );
      } catch( JSONException e ) {
        throw new IllegalArgumentException( "Event type missing in operation", e );
      } 
      try { 
        properties = operation.getJSONObject( 3 );
      } catch( JSONException e ) {
        throw new IllegalArgumentException( "Properties object missing in operation", e );
      } 
    } 
 
    public String getEventName() {
      return eventName;
    } 
 
    @Override 
    protected JSONObject getProperties() {
      return properties;
    } 
 
  } 
 
  public final class CallOperation extends Operation { 
 
    private final String methodName;
    private final JSONObject properties;
 
    private CallOperation( JSONArray operation ) {
      super( operation );
      try { 
        methodName = operation.getString( 2 );
      } catch( JSONException e ) {
        throw new IllegalArgumentException( "Method name missing in operation", e );
      } 
      try { 
        properties = operation.getJSONObject( 3 );
      } catch( JSONException e ) {
        throw new IllegalArgumentException( "Properties object missing in operation", e );
      } 
    }