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

static JSONObject net::sf::json::JSONObject::_fromBean ( Object  bean,
JsonConfig  jsonConfig 
) [inline, static, private]

Creates a JSONObject from a POJO.
Supports nested maps, POJOs, and arrays/collections.

Parameters:
bean An object with POJO conventions
Exceptions:
JSONException if the bean can not be converted to a proper JSONObject.

Definition at line 853 of file JSONObject.java.

References net::sf::json::AbstractJSON::addInstance(), net::sf::json::util::PropertyFilter::apply(), net::sf::json::JsonConfig::findDefaultValueProcessor(), net::sf::json::JsonConfig::findJsonBeanProcessor(), net::sf::json::JsonConfig::findJsonValueProcessor(), net::sf::json::AbstractJSON::fireErrorEvent(), net::sf::json::AbstractJSON::fireObjectEndEvent(), net::sf::json::AbstractJSON::fireObjectStartEvent(), net::sf::json::AbstractJSON::fireWarnEvent(), net::sf::json::JsonConfig::getCycleDetectionStrategy(), net::sf::json::JsonConfig::getJsonPropertyFilter(), net::sf::json::JsonConfig::getMergedExcludes(), net::sf::json::util::CycleDetectionStrategy::handleRepeatedReferenceAsObject(), net::sf::json::JsonConfig::isIgnoreJPATransient(), net::sf::json::JsonConfig::isIgnoreTransientFields(), JSONObject(), net::sf::json::processors::JsonBeanProcessor::processBean(), net::sf::json::processors::JsonValueProcessor::processObjectValue(), and net::sf::json::AbstractJSON::removeInstance().

Referenced by fromObject().

                                                                             {
      fireObjectStartEvent( jsonConfig );
      if( !addInstance( bean ) ){
         try{
            return jsonConfig.getCycleDetectionStrategy()
                  .handleRepeatedReferenceAsObject( bean );
         }catch( JSONException jsone ){
            removeInstance( bean );
            fireErrorEvent( jsone, jsonConfig );
            throw jsone;
         }catch( RuntimeException e ){
            removeInstance( bean );
            JSONException jsone = new JSONException( e );
            fireErrorEvent( jsone, jsonConfig );
            throw jsone;
         }
      }

      JsonBeanProcessor processor = jsonConfig.findJsonBeanProcessor( bean.getClass() );
      if( processor != null ){
         JSONObject json = null;
         try{
            json = processor.processBean( bean, jsonConfig );
            if( json == null ){
               json = (JSONObject) jsonConfig.findDefaultValueProcessor( bean.getClass() )
                     .getDefaultValue( bean.getClass() );
               if( json == null ){
                  json = new JSONObject( true );
               }
            }
            removeInstance( bean );
            fireObjectEndEvent( jsonConfig );
         }catch( JSONException jsone ){
            removeInstance( bean );
            fireErrorEvent( jsone, jsonConfig );
            throw jsone;
         }catch( RuntimeException e ){
            removeInstance( bean );
            JSONException jsone = new JSONException( e );
            fireErrorEvent( jsone, jsonConfig );
            throw jsone;
         }
         return json;
      }

      Collection exclusions = jsonConfig.getMergedExcludes();
      JSONObject jsonObject = new JSONObject();
      try{
         PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors( bean );
         PropertyFilter jsonPropertyFilter = jsonConfig.getJsonPropertyFilter();
         Class beanClass = bean.getClass();
         for( int i = 0; i < pds.length; i++ ){
            String key = pds[i].getName();
            if( exclusions.contains( key ) ){
               continue;
            }

            if( jsonConfig.isIgnoreTransientFields() && isTransientField( key, beanClass ) ){
               continue;
            }

            Class type = pds[i].getPropertyType();
            if( pds[i].getReadMethod() != null ){
               if( jsonConfig.isIgnoreJPATransient() ){
                  try{
                     Class transientClass = Class.forName( "javax.persistence.Transient" );
                     if( pds[i].getReadMethod()
                           .getAnnotation( transientClass ) != null ){
                        continue;
                     }
                  }catch( ClassNotFoundException cnfe ){
                     // ignore
                  }
               }

               Object value = PropertyUtils.getProperty( bean, key );
               if( jsonPropertyFilter != null && jsonPropertyFilter.apply( bean, key, value ) ){
                  continue;
               }
               JsonValueProcessor jsonValueProcessor = jsonConfig.findJsonValueProcessor(
                     beanClass, type, key );
               if( jsonValueProcessor != null ){
                  value = jsonValueProcessor.processObjectValue( key, value, jsonConfig );
                  if( !JsonVerifier.isValidJsonValue( value ) ){
                     throw new JSONException( "Value is not a valid JSON value. " + value );
                  }
               }
               setValue( jsonObject, key, value, type, jsonConfig );
            }else{
               String warning = "Property '" + key + "' has no read method. SKIPPED";
               fireWarnEvent( warning, jsonConfig );
               log.warn( warning );
            }
         }
      }catch( JSONException jsone ){
         removeInstance( bean );
         fireErrorEvent( jsone, jsonConfig );
         throw jsone;
      }catch( Exception e ){
         removeInstance( bean );
         JSONException jsone = new JSONException( e );
         fireErrorEvent( jsone, jsonConfig );
         throw jsone;
      }

      removeInstance( bean );
      fireObjectEndEvent( jsonConfig );
      return jsonObject;
   }


Generated by  Doxygen 1.6.0   Back to index