Logo Search packages:      
Sourcecode: libjson-java version File versions

static Collection net::sf::json::JSONArray::toCollection ( JSONArray  jsonArray,
JsonConfig  jsonConfig 
) [inline, static]

Returns a List or a Set taking generics into account.
Contributed by [Matt Small @ WaveMaker].

Definition at line 416 of file JSONArray.java.

References net::sf::json::JsonConfig::copy(), get(), net::sf::json::JsonConfig::getClassMap(), net::sf::json::JsonConfig::getCollectionType(), net::sf::json::JsonConfig::getRootClass(), net::sf::json::JsonConfig::setClassMap(), net::sf::json::JsonConfig::setRootClass(), size(), and toCollection().

                                                                                       {
      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;
   }


Generated by  Doxygen 1.6.0   Back to index