Open Source Repository

Home /json/json-lib-2.4-jdk13 | Repository Home



net/sf/json/util/PropertySetStrategy.java
/*
 * Copyright 2002-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sf.json.util;

import java.lang.reflect.Field;
import java.util.Map;

import net.sf.json.JSONException;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.PropertyUtils;

/**
 * Defines a custom setter to be used when setting object values.<br>
 * Specify with JsonConfig.setJsonPropertySetter().
 *
 @author Gino Miceli <[email protected]>
 @author Andres Almiray <[email protected]>
 */
public abstract class PropertySetStrategy {
   public static final PropertySetStrategy DEFAULT = new DefaultPropertySetStrategy();

   public abstract void setPropertyObject bean, String key, Object value throws JSONException;
   
   public void setPropertyObject bean, String key, Object value, JsonConfig jsonConfig throws JSONException {
      setPropertybean, key, value );
   }

   private static final class DefaultPropertySetStrategy extends PropertySetStrategy {
      public void setPropertyObject bean, String key, Object value throws JSONException {
         setPropertybean, key, value, new JsonConfig());
      }
      
      public void setPropertyObject bean, String key, Object value, JsonConfig jsonConfig throws JSONException {
         ifbean instanceof Map ){
            ((Mapbean).putkey, value );
         else {
            if!jsonConfig.isIgnorePublicFields() ) {
               try {
                  Field field = bean.getClass().getFieldkey );
                  iffield != null field.setbean, value );
               catchException e ){
                  _setPropertybean, key, value );
               }
            else {
               _setPropertybean, key, value );
            }
         }
      }
      
      private void _setPropertyObject bean, String key, Object value ) {
         try {
            PropertyUtils.setSimplePropertybean, key, value );
         catchException e ) {
            throw new JSONException);
         }
      }
      
   }
}