Search in sources :

Example 1 with IGxJSONSerializable

use of com.genexus.internet.IGxJSONSerializable in project JavaClasses by genexuslabs.

the class GXWebPanel method convertparm.

protected Object convertparm(Class<?>[] pars, int i, Object value) {
    try {
        String parmTypeName = pars[i].getName();
        if (parmTypeName.equals("java.util.Date")) {
            String strVal = value.toString();
            if (strVal.length() > 8)
                return localUtil.parseDTimeParm(strVal);
            else
                return localUtil.parseDateParm(strVal);
        }
        if (IGxJSONSerializable.class.isAssignableFrom(pars[i])) {
            IGxJSONSerializable parmObj = null;
            if (com.genexus.xml.GXXMLSerializable.class.isAssignableFrom(pars[i])) {
                parmObj = (com.genexus.xml.GXXMLSerializable) pars[i].getConstructor(new Class<?>[] { ModelContext.class }).newInstance(new Object[] { context });
            } else {
                parmObj = (IGxJSONSerializable) pars[i].getConstructor(new Class<?>[] {}).newInstance(new Object[] {});
            }
            parmObj.fromJSonString(value.toString());
            return parmObj;
        }
        // Control Properties values (ServerSide: int, clientSide: bool)
        if ((parmTypeName.equals("int") || parmTypeName.equals("java.lang.Integer")) && value != null) {
            if (value.toString().equalsIgnoreCase("true"))
                return 1;
            if (value.toString().equalsIgnoreCase("false"))
                return 0;
        }
        return com.genexus.GXutil.convertObjectTo(value, pars[i], false);
    } catch (Exception e) {
        return value;
    }
}
Also used : IGxJSONSerializable(com.genexus.internet.IGxJSONSerializable) JSONException(json.org.json.JSONException) IOException(java.io.IOException)

Example 2 with IGxJSONSerializable

use of com.genexus.internet.IGxJSONSerializable in project JavaClasses by genexuslabs.

the class GXSimpleCollection method FromJSONObject.

public void FromJSONObject(IJsonFormattable obj) {
    this.clear();
    JSONArray jsonArr = (JSONArray) obj;
    for (int i = 0; i < jsonArr.length(); i++) {
        try {
            Object jsonObj = jsonArr.get(i);
            Object currObj = jsonObj;
            Class[] parTypes = new Class[] {};
            Object[] arglist = new Object[] {};
            if (elementsType == null) {
                addObject(currObj);
            } else {
                if (elementsType.getSuperclass().getName().equals("com.genexus.GxSilentTrnSdt")) {
                    parTypes = new Class[] { int.class };
                    arglist = new Object[] { new Integer(remoteHandle) };
                }
                if (IGxJSONAble.class.isAssignableFrom(elementsType)) {
                    Constructor constructor = elementsType.getConstructor(parTypes);
                    currObj = constructor.newInstance(arglist);
                    ((IGxJSONAble) currObj).FromJSONObject((IJsonFormattable) jsonObj);
                }
                if (IGxJSONSerializable.class.isAssignableFrom(elementsType)) {
                    Constructor constructor = elementsType.getConstructor(parTypes);
                    currObj = constructor.newInstance(arglist);
                    ((IGxJSONSerializable) currObj).fromJSonString(jsonObj.toString());
                }
                if (elementsType == Integer.class) {
                    currObj = new Integer(jsonArr.getInt(i));
                } else if (elementsType == Short.class) {
                    currObj = new Short((short) jsonArr.getInt(i));
                } else if (elementsType == Boolean.class) {
                    currObj = new Boolean(jsonArr.getBoolean(i));
                } else if (elementsType == Double.class) {
                    currObj = new Double(jsonArr.getDouble(i));
                } else if (elementsType == java.math.BigDecimal.class) {
                    currObj = java.math.BigDecimal.valueOf(jsonArr.getDouble(i));
                } else if (elementsType == Long.class) {
                    currObj = new Long(jsonArr.getLong(i));
                } else if (elementsType == Date.class) {
                    currObj = SpecificImplementation.GXutil.charToTimeREST(jsonArr.getString(i));
                } else if (elementsType == java.util.UUID.class) {
                    currObj = CommonUtil.strToGuid(jsonArr.getString(i));
                } else if (elementsType == String.class && currObj instanceof Number) {
                    currObj = new String().valueOf(currObj);
                }
                addObject(currObj);
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }
}
Also used : IGxJSONAble(com.genexus.internet.IGxJSONAble) Constructor(java.lang.reflect.Constructor) JSONArray(json.org.json.JSONArray) JSONException(json.org.json.JSONException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IGxJSONSerializable(com.genexus.internet.IGxJSONSerializable)

Aggregations

IGxJSONSerializable (com.genexus.internet.IGxJSONSerializable)2 JSONException (json.org.json.JSONException)2 IGxJSONAble (com.genexus.internet.IGxJSONAble)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 JSONArray (json.org.json.JSONArray)1