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

static Object net::sf::json::JSONArray::toArray ( JSONArray  jsonArray,
JsonConfig  jsonConfig 
) [inline, static]

Creates a java array from a JSONArray.

Definition at line 296 of file JSONArray.java.

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

                                                                              {
      Class objectClass = jsonConfig.getRootClass();
      Map classMap = jsonConfig.getClassMap();

      if( jsonArray.size() == 0 ){
         return Array.newInstance( objectClass == null ? Object.class : objectClass, 0 );
      }

      int[] dimensions = JSONArray.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;
   }


Generated by  Doxygen 1.6.0   Back to index