Search in sources :

Example 1 with JsonConfig

use of net.sf.json.JsonConfig in project topcom-cloud by 545314690.

the class JsonUtil method getNoNullConfig.

public static JsonConfig getNoNullConfig() {
    JsonConfig config = new JsonConfig();
    config.setJsonPropertyFilter(new PropertyFilter() {

        @Override
        public boolean apply(Object o, String s, Object o1) {
            return o1 == null || o1.equals(JSONNull.getInstance());
        }
    });
    return config;
}
Also used : JsonConfig(net.sf.json.JsonConfig) PropertyFilter(net.sf.json.util.PropertyFilter) JSONObject(net.sf.json.JSONObject)

Example 2 with JsonConfig

use of net.sf.json.JsonConfig in project jaffa-framework by jaffa-projects.

the class ExcelExportService method createJsonConfig.

/**
 * Creates a JsonConfig with the rootClass set to the input. Adds
 * custom-support for handling FlexCriteriaBean.
 */
private static JsonConfig createJsonConfig(Class rootClass) {
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setRootClass(rootClass);
    jsonConfig.setNewBeanInstanceStrategy(new NewBeanInstanceStrategy() {

        public Object newInstance(Class target, JSONObject source) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, InvocationTargetException {
            if (target == FlexCriteriaBean.class) {
                try {
                    // Determine the name of the associated dynaClass and
                    // use that to instantiate the FlexCriteriaBean
                    JSONObject dynaClassObject = source.getJSONObject("dynaClass");
                    String dynaClassName = dynaClassObject.getString("name");
                    FlexCriteriaBean bean = FlexCriteriaBean.instance(FlexClass.instance(dynaClassName));
                    // Add the criteria elements
                    source.remove("dynaClass");
                    for (Iterator i = source.keys(); i.hasNext(); ) {
                        String key = (String) i.next();
                        Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
                        propType = findCriteriaFieldClass(propType);
                        Object propValue = JSONObject.toBean(source.getJSONObject(key), propType);
                        bean.set(key, propValue);
                    }
                    source.clear();
                    return bean;
                } catch (Exception e) {
                    String s = "Exception thrown while instantiating FlexCriteriaBean from " + source;
                    log.error(s, e);
                    throw new InvocationTargetException(e, s);
                }
            }
            return target.newInstance();
        }
    });
    return jsonConfig;
}
Also used : JsonConfig(net.sf.json.JsonConfig) NewBeanInstanceStrategy(net.sf.json.util.NewBeanInstanceStrategy) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(net.sf.json.JSONObject) Iterator(java.util.Iterator) FlexClass(org.jaffa.flexfields.FlexClass) JSONObject(net.sf.json.JSONObject) FlexCriteriaBean(org.jaffa.flexfields.FlexCriteriaBean)

Example 3 with JsonConfig

use of net.sf.json.JsonConfig in project jaffa-framework by jaffa-projects.

the class ExcelExportService method createJsonConfig.

/**
 * Creates a JsonConfig with the rootClass set to the input. Adds
 * custom-support for handling FlexCriteriaBean.
 */
private static JsonConfig createJsonConfig(Class rootClass) {
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setRootClass(rootClass);
    jsonConfig.setNewBeanInstanceStrategy(new NewBeanInstanceStrategy() {

        public Object newInstance(Class target, JSONObject source) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, InvocationTargetException {
            if (target == FlexCriteriaBean.class) {
                try {
                    // Determine the name of the associated dynaClass and
                    // use that to instantiate the FlexCriteriaBean
                    JSONObject dynaClassObject = source.getJSONObject("dynaClass");
                    String dynaClassName = dynaClassObject.getString("name");
                    FlexCriteriaBean bean = FlexCriteriaBean.instance(FlexClass.instance(dynaClassName));
                    // Add the criteria elements
                    source.remove("dynaClass");
                    for (Iterator i = source.keys(); i.hasNext(); ) {
                        String key = (String) i.next();
                        Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
                        propType = findCriteriaFieldClass(propType);
                        Object propValue = JSONObject.toBean(source.getJSONObject(key), propType);
                        bean.set(key, propValue);
                    }
                    source.clear();
                    return bean;
                } catch (Exception e) {
                    String s = "Exception thrown while instantiating FlexCriteriaBean from " + source;
                    log.error(s, e);
                    throw new InvocationTargetException(e, s);
                }
            }
            return target.newInstance();
        }
    });
    return jsonConfig;
}
Also used : JsonConfig(net.sf.json.JsonConfig) NewBeanInstanceStrategy(net.sf.json.util.NewBeanInstanceStrategy) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(net.sf.json.JSONObject) Iterator(java.util.Iterator) FlexClass(org.jaffa.flexfields.FlexClass) JSONObject(net.sf.json.JSONObject) FlexCriteriaBean(org.jaffa.flexfields.FlexCriteriaBean)

Example 4 with JsonConfig

use of net.sf.json.JsonConfig in project jaffa-framework by jaffa-projects.

the class JaffaComponentHelper method validateJSONObject.

/**
 * Validates the JSONObject class name.
 *
 * @param className <code>String</code> the class name.
 * @param value <code>String</code> the request parameter value.
 * @param error <code>String</code> the error message.
 * @throws <code>InvalidParameterException</code> the exception, designed
 *         for use by the JCA/JCE engine classes, is thrown when an invalid
 *         parameter is passed to a method.
 */
private void validateJSONObject(final String className, final String value, final String error) throws InvalidParameterException {
    try {
        final Class<?> clazz = Class.forName(className);
        clazz.newInstance();
        try {
            final JSONObject json = JSONObject.fromObject(value);
            final JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.setRootClass(clazz);
            JSONSerializer.toJava(json, jsonConfig);
        } catch (Exception e) {
            throw new InvalidParameterException(error);
        }
    } catch (ClassNotFoundException e) {
        throw new InvalidParameterException(error);
    } catch (InstantiationException e1) {
        throw new InvalidParameterException(error);
    } catch (IllegalAccessException e1) {
        throw new InvalidParameterException(error);
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) JSONObject(net.sf.json.JSONObject) JsonConfig(net.sf.json.JsonConfig) InvalidParameterException(java.security.InvalidParameterException) AccessControlException(java.security.AccessControlException)

Example 5 with JsonConfig

use of net.sf.json.JsonConfig in project ngtesting-platform by aaronchen2k.

the class JsonUtils method configJson.

/**
 * json 配置属性,并转化时间格式
 *
 * @param datePattern 时间格式
 * @return 返回JsonConfig
 */
public static JsonConfig configJson(String datePattern) {
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setIgnoreDefaultExcludes(false);
    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
    jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor(datePattern));
    return jsonConfig;
}
Also used : JsonConfig(net.sf.json.JsonConfig)

Aggregations

JsonConfig (net.sf.json.JsonConfig)28 JSONObject (net.sf.json.JSONObject)17 File (java.io.File)10 HashMap (java.util.HashMap)10 JSONArray (net.sf.json.JSONArray)10 Test (org.junit.Test)10 URL (java.net.URL)7 PrintWriter (java.io.PrintWriter)5 JSON (net.sf.json.JSON)5 Writer (java.io.Writer)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 NewBeanInstanceStrategy (net.sf.json.util.NewBeanInstanceStrategy)2 FlexClass (org.jaffa.flexfields.FlexClass)2 FlexCriteriaBean (org.jaffa.flexfields.FlexCriteriaBean)2 Status (io.milton.http.Response.Status)1 NameAndError (io.milton.http.webdav.PropFindResponse.NameAndError)1 CollectionResource (io.milton.resource.CollectionResource)1